Completed
Push — master ( 5fbdf9...7206c9 )
by Maxime
01:44
created

Router   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 29
dl 0
loc 96
rs 10
c 0
b 0
f 0

10 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 createRoute() 0 3 1
A get() 0 6 1
A getRoutes() 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
    public function getRoutes()
92
    {
93
        return $this->routeContainer->getRoutes();
94
    }
95
96
    /**
97
     * Create a new route.
98
     *
99
     * @param callable|string $action
100
     */
101
    private function createRoute(string $path, string $name, $action): Route
102
    {
103
        return new Route($path, $name, $action);
104
    }
105
}
106