RouteList::setRoutes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace ByJG\RestServer\Route;
5
6
use FastRoute\Dispatcher;
7
use FastRoute\RouteCollector;
8
use function FastRoute\simpleDispatcher;
9
10
class RouteList implements RouteListInterface
11
{
12
    protected $routes = null;
13
14 16
    public function getRoutes()
15
    {
16 16
        return $this->routes;
17
    }
18
19
    /**
20
     * @param Route[] $routes
21
     * @return RouteList
22
     */
23 4
    public function setRoutes($routes)
24
    {
25 4
        foreach ((array)$routes as $route) {
26 4
            $this->addRoute($route);
27
        }
28 4
        return $this;
29
    }
30
31
    /**
32
     * @param Route $route
33
     * @return RouteList
34
     */
35 20
    public function addRoute(Route $route)
36
    {
37 20
        if (is_null($this->routes)) {
38 20
            $this->routes = [];
39
        }
40 20
        $this->routes[] = $route;
41 20
        return $this;
42
    }
43
44
    /**
45
     * @return Dispatcher
46
     */
47 12
    public function getDispatcher()
48
    {
49
        // Generic Dispatcher for RestServer
50 12
        return simpleDispatcher(function (RouteCollector $r) {
51
52 12
            foreach ($this->getRoutes() as $route) {
53 12
                $r->addRoute(
54 12
                    $route->getMethod(),
55 12
                    $route->getPath(),
56 12
                    [
57 12
                        "output_processor" => $route->getOutputProcessor(),
58 12
                        "class" => $route->getClass(),
59 12
                    ]
60 12
                );
61
            }
62 12
        });
63
    }
64
65
}