Passed
Push — master ( b0ffba...d5ae6f )
by Maxime
38s queued 10s
created

Router   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 30
dl 0
loc 107
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A put() 0 6 1
A post() 0 6 1
A match() 0 13 4
A delete() 0 6 1
A options() 0 6 1
A patch() 0 6 1
A get() 0 6 1
A getRoutes() 0 3 1
A generate() 0 3 1
A createRoute() 0 3 1
1
<?php
2
3
namespace Piface\Router;
4
5
use Piface\Router\Exception\MethodNotAllowedException;
6
use Psr\Http\Message\ServerRequestInterface;
7
8
class Router implements RouterInterface
9
{
10
    /**
11
     * @var RouterContainer
12
     */
13
    private $routeContainer;
14
15
    public function __construct()
16
    {
17
        $this->routeContainer = new RouterContainer();
18
    }
19
20
    /**
21
     * Register a GET route.
22
     *
23
     * @param callable|string $action
24
     */
25
    public function get(string $path, string $name, $action): Route
26
    {
27
        $route = $this->createRoute($path, $name, $action);
28
        $route->allows('GET');
29
30
        return $this->routeContainer->addRoute($route);
31
    }
32
33
    public function post(string $path, string $name, $action): Route
34
    {
35
        $route = $this->createRoute($path, $name, $action);
36
        $route->allows('POST');
37
38
        return $this->routeContainer->addRoute($route);
39
    }
40
41
    public function put(string $path, string $name, $action): Route
42
    {
43
        $route = $this->createRoute($path, $name, $action);
44
        $route->allows('PUT');
45
46
        return $this->routeContainer->addRoute($route);
47
    }
48
49
    public function delete(string $path, string $name, $action): Route
50
    {
51
        $route = $this->createRoute($path, $name, $action);
52
        $route->allows('DELETE');
53
54
        return $this->routeContainer->addRoute($route);
55
    }
56
57
    public function patch(string $path, string $name, $action): Route
58
    {
59
        $route = $this->createRoute($path, $name, $action);
60
        $route->allows('PATCH');
61
62
        return $this->routeContainer->addRoute($route);
63
    }
64
65
    public function options(string $path, string $name, $action): Route
66
    {
67
        $route = $this->createRoute($path, $name, $action);
68
        $route->allows('OPTIONS');
69
70
        return $this->routeContainer->addRoute($route);
71
    }
72
73
    /**
74
     * Compare the given request with routes in the routerContainer.
75
     */
76
    public function match(ServerRequestInterface $request): ?Route
77
    {
78
        foreach ($this->routeContainer->getRoutes() as $route) {
79
            if ($this->routeContainer->match($request, $route)) {
80
                if (!in_array($request->getMethod(), $route->getAllows())) {
81
                    throw new MethodNotAllowedException($route->getAllows(), $request->getUri()->getPath());
82
                }
83
84
                return $route;
85
            }
86
        }
87
88
        return null;
89
    }
90
91
    /**
92
     * @return Route[]
93
     */
94
    public function getRoutes(): array
95
    {
96
        return $this->routeContainer->getRoutes();
97
    }
98
99
    /**
100
     * Allow to generate a path for an existing route by its name.
101
     */
102
    public function generate(string $name, array $params = []): string
103
    {
104
        return $this->routeContainer->generatePath($name, $params);
105
    }
106
107
    /**
108
     * Create a new route.
109
     *
110
     * @param callable|string $action
111
     */
112
    private function createRoute(string $path, string $name, $action): Route
113
    {
114
        return new Route($path, $name, $action);
115
    }
116
}
117