Router::addRoute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Mosaic\Routing\Adapters;
4
5
use Mosaic\Routing\Route;
6
use Mosaic\Routing\RouteCollection;
7
use Mosaic\Routing\Router as RouterContract;
8
9
class Router implements RouterContract
10
{
11
    /**
12
     * @var RouteCollection|Route[]
13
     */
14
    protected $routes;
15
16
    /**
17
     * All of the verbs supported by the router.
18
     *
19
     * @var array
20
     */
21
    public static $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'];
22
23
    /**
24
     * Router constructor.
25
     */
26 9
    public function __construct()
27
    {
28 9
        $this->routes = new RouteCollection;
29 9
    }
30
31
    /**
32
     * @return RouteCollection|Route[]
33
     */
34 8
    public function all() : RouteCollection
35
    {
36 8
        return $this->routes;
37
    }
38
39
    /**
40
     * Register a new GET route with the router.
41
     *
42
     * @param string                $uri
43
     * @param \Closure|array|string $action
44
     *
45
     * @return Route
46
     */
47 2
    public function get($uri, $action)
48
    {
49 2
        return $this->addRoute(['GET', 'HEAD'], $uri, $action);
50
    }
51
52
    /**
53
     * Register a new POST route with the router.
54
     *
55
     * @param string                $uri
56
     * @param \Closure|array|string $action
57
     *
58
     * @return Route
59
     */
60 1
    public function post($uri, $action)
61
    {
62 1
        return $this->addRoute('POST', $uri, $action);
63
    }
64
65
    /**
66
     * Register a new PUT route with the router.
67
     *
68
     * @param string                $uri
69
     * @param \Closure|array|string $action
70
     *
71
     * @return Route
72
     */
73 1
    public function put($uri, $action)
74
    {
75 1
        return $this->addRoute('PUT', $uri, $action);
76
    }
77
78
    /**
79
     * Register a new PATCH route with the router.
80
     *
81
     * @param string                $uri
82
     * @param \Closure|array|string $action
83
     *
84
     * @return Route
85
     */
86 1
    public function patch($uri, $action)
87
    {
88 1
        return $this->addRoute('PATCH', $uri, $action);
89
    }
90
91
    /**
92
     * Register a new DELETE route with the router.
93
     *
94
     * @param string                $uri
95
     * @param \Closure|array|string $action
96
     *
97
     * @return Route
98
     */
99 1
    public function delete($uri, $action)
100
    {
101 1
        return $this->addRoute('DELETE', $uri, $action);
102
    }
103
104
    /**
105
     * Register a new OPTIONS route with the router.
106
     *
107
     * @param string                $uri
108
     * @param \Closure|array|string $action
109
     *
110
     * @return Route
111
     */
112 1
    public function options($uri, $action)
113
    {
114 1
        return $this->addRoute('OPTIONS', $uri, $action);
115
    }
116
117
    /**
118
     * Register a new route responding to all verbs.
119
     *
120
     * @param string                $uri
121
     * @param \Closure|array|string $action
122
     *
123
     * @return Route
124
     */
125 1
    public function any($uri, $action)
126
    {
127 1
        $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'];
128
129 1
        return $this->addRoute($verbs, $uri, $action);
130
    }
131
132
    /**
133
     * Register a new route with the given verbs.
134
     *
135
     * @param array|string          $methods
136
     * @param string                $uri
137
     * @param \Closure|array|string $action
138
     *
139
     * @return Route
140
     */
141 1
    public function match($methods, $uri, $action)
142
    {
143 1
        return $this->addRoute(array_map('strtoupper', (array)$methods), $uri, $action);
144
    }
145
146
    /**
147
     * Add a route to the underlying route collection.
148
     *
149
     * @param array|string          $methods
150
     * @param string                $uri
151
     * @param \Closure|array|string $action
152
     *
153
     * @return Route
154
     */
155 9
    protected function addRoute($methods, $uri, $action)
156
    {
157 9
        return $this->routes->add(
158 9
            new Route($methods, $uri, $action)
159
        );
160
    }
161
}
162