Completed
Pull Request — master (#8)
by Adam
02:32
created

SimpleHandlerFactory::get()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 9
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Equip\Queue\Handler;
4
5
use Equip\Queue\Exception\HandlerException;
6
use Equip\Queue\Exception\RouterException;
7
8
class SimpleHandlerFactory implements HandlerFactoryInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    private $routes;
14
15
    /**
16
     * @param array $routes
17
     */
18 4
    public function __construct(array $routes = [])
19
    {
20 4
        $this->routes = $routes;
21 4
    }
22
23
    /**
24
     * @inheritdoc
25
     */
26 4
    public function get($handler)
27
    {
28 4
        $route = $this->getRoute($handler);
29 4
        if (!$route) {
30 1
            throw HandlerException::notFound($handler);
31
        }
32
33 3
        if (is_string($route) && class_exists($route)) {
34 1
            $route = new $route;
35 1
        }
36
37 3
        if (is_callable($route)) {
38 2
            return $route;
39
        }
40
41 1
        throw HandlerException::invalidHandler($handler);
42
    }
43
44
    /**
45
     * Get the routes handler
46
     *
47
     * @param string $handler
48
     *
49
     * @return mixed|null
50
     */
51 4
    private function getRoute($handler)
52
    {
53 4
        return isset($this->routes[$handler]) ? $this->routes[$handler] : null;
54
    }
55
}
56