Completed
Push — feature/fixing_cost ( f58af6...414fbf )
by Laurent
01:41
created

RouteManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace FlightLog\Infrastructure\Common\Routes;
5
6
7
final class RouteManager
8
{
9
10
    /**
11
     * @var array|Route[]
12
     */
13
    private $routes;
14
15
    /**
16
     * @var \DoliDB
17
     */
18
    private $db;
19
20
    public function __construct(\DoliDB $db)
21
    {
22
        $this->routes = [];
23
        $this->db = $db;
24
    }
25
26
27
    /**
28
     * @param Route $route
29
     *
30
     * @throws \Exception
31
     */
32
    public function add(Route $route){
33
        if(isset($this->routes[$route->getName()])){
34
            throw new \Exception('Route exists');
35
        }
36
37
        $this->routes[$route->getName()] = $route;
38
    }
39
40
    /**
41
     * @param array $routes
42
     *
43
     * @throws \Exception
44
     */
45
    public function load(array $routes){
46
        foreach($routes as $currentRoute){
47
            $this->add($currentRoute);
48
        }
49
    }
50
51
    /**
52
     * @param string $name
53
     *
54
     * @throws \Exception
55
     */
56
    public function __invoke($name)
57
    {
58
        if(!isset($this->routes[$name])){
59
            throw new \Exception('Route not found');
60
        }
61
62
        $route = $this->routes[$name];
63
64
        $controllerName = $route->getController();
65
        $controller = new $controllerName($this->db);
66
67
        call_user_func([$controller, $route->getMethod()]);
68
    }
69
70
71
72
}