RouteList::getRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
}