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

SimpleHandlerFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 48
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B get() 0 17 5
A getRoute() 0 4 2
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