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

SimpleRouter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Equip\Queue\Router;
4
5
use Equip\Queue\Exception\HandlerException;
6
use Equip\Queue\Exception\RouterException;
7
8
class SimpleRouter implements RouterInterface
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 RouterException::routeNotFound($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