Test Failed
Pull Request — master (#11)
by Joao
01:49
created

RouteList   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 57.89%

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 49
ccs 11
cts 19
cp 0.5789
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setRoutes() 0 6 2
A getRoutes() 0 3 1
A addRoute() 0 7 2
A getDispatcher() 0 12 2
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 4
    public function getRoutes()
15
    {
16 4
        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 8
    public function addRoute(Route $route)
36
    {
37 8
        if (is_null($this->routes)) {
38 8
            $this->routes = [];
39
        }
40 8
        $this->routes[] = $route;
41 8
        return $this;
42
    }
43
44
    /**
45
     * @return Dispatcher
46
     */
47
    public function getDispatcher()
48
    {
49
        // Generic Dispatcher for RestServer
50
        return simpleDispatcher(function (RouteCollector $r) {
51
52
            foreach ($this->getRoutes() as $route) {
53
                $r->addRoute(
54
                    $route->getMethod(),
55
                    $route->getPath(),
56
                    [
57
                        "output_processor" => $route->getOutputProcessor(),
58
                        "class" => $route->getClass(),
59
                    ]
60
                );
61
            }
62
        });
63
    }
64
65
}