Passed
Pull Request — master (#32)
by Anatoly
02:32
created

RouteCollection::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 14
rs 10
ccs 8
cts 8
cp 1
cc 1
nc 1
nop 5
crap 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router;
13
14
/**
15
 * Import classes
16
 */
17
use Fig\Http\Message\RequestMethodInterface;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Psr\Http\Server\RequestHandlerInterface;
20
use Sunrise\Http\Router\Exception\RouteAlreadyExistsException;
21
use Sunrise\Http\Router\Exception\RouteNotFoundException;
22
23
/**
24
 * Import functions
25
 */
26
use function array_merge;
27
use function array_values;
28
use function rtrim;
29
30
/**
31
 * RouteCollection
32
 */
33
class RouteCollection implements RouteCollectionInterface
34
{
35
36
    /**
37
     * The collection prefix
38
     *
39
     * @var null|string
40
     */
41
    private $prefix;
42
43
    /**
44
     * The collection middlewares
45
     *
46
     * @var MiddlewareInterface[]
47
     */
48
    private $middlewares = [];
49
50
    /**
51
     * The collection routes
52
     *
53
     * @var RouteInterface[]
54
     */
55
    private $routes = [];
56
57
    /**
58
     * {@inheritDoc}
59
     */
60 2
    public function getPrefix() : ?string
61
    {
62 2
        return $this->prefix;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68 2
    public function getMiddlewares() : array
69
    {
70 2
        return $this->middlewares;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76 28
    public function getRoutes() : array
77
    {
78 28
        return array_values($this->routes);
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function getRoute(string $name) : RouteInterface
85
    {
86
        if (isset($this->routes[$name])) {
87
            return $this->routes[$name];
88
        }
89
90
        throw new RouteNotFoundException();
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96 10
    public function setPrefix(string $prefix) : RouteCollectionInterface
97
    {
98
        // https://github.com/sunrise-php/http-router/issues/26
99 10
        $prefix = rtrim($prefix, '/');
100
101 10
        $this->prefix = $prefix;
102
103 10
        return $this;
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109 10
    public function addMiddlewares(MiddlewareInterface ...$middlewares) : RouteCollectionInterface
110
    {
111 10
        foreach ($middlewares as $middleware) {
112 10
            $this->middlewares[] = $middleware;
113
        }
114
115 10
        return $this;
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121 43
    public function addRoutes(RouteInterface ...$routes) : RouteCollectionInterface
122
    {
123 43
        foreach ($routes as $route) {
124 43
            $name = $route->getName();
125
126 43
            if (isset($this->routes[$name])) {
127
                throw new RouteAlreadyExistsException();
128
            }
129
130 43
            $this->routes[$name] = $route;
131
        }
132
133 43
        return $this;
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139 33
    public function route(
140
        string $name,
141
        string $path,
142
        array $methods,
143
        RequestHandlerInterface $requestHandler,
144
        array $middlewares = [],
145
        array $attributes = []
146
    ) : RouteInterface {
147 33
        $path = $this->prefix . $path;
148
149 33
        $middlewares = array_merge(
150 33
            $this->middlewares,
151 33
            $middlewares
152
        );
153
154 33
        $route = new Route(
155 33
            $name,
156 33
            $path,
157 33
            $methods,
158 33
            $requestHandler,
159 33
            $middlewares,
160 33
            $attributes
161
        );
162
163 33
        $this->addRoutes($route);
164
165 33
        return $route;
166
    }
167
168
    /**
169
     * {@inheritDoc}
170
     */
171 4
    public function head(
172
        string $name,
173
        string $path,
174
        RequestHandlerInterface $requestHandler,
175
        array $middlewares = [],
176
        array $attributes = []
177
    ) : RouteInterface {
178 4
        return $this->route(
179 4
            $name,
180 4
            $path,
181 4
            [RequestMethodInterface::METHOD_HEAD],
182 4
            $requestHandler,
183 4
            $middlewares,
184 4
            $attributes
185
        );
186
    }
187
188
    /**
189
     * {@inheritDoc}
190
     */
191 5
    public function get(
192
        string $name,
193
        string $path,
194
        RequestHandlerInterface $requestHandler,
195
        array $middlewares = [],
196
        array $attributes = []
197
    ) : RouteInterface {
198 5
        return $this->route(
199 5
            $name,
200 5
            $path,
201 5
            [RequestMethodInterface::METHOD_GET],
202 5
            $requestHandler,
203 5
            $middlewares,
204 5
            $attributes
205
        );
206
    }
207
208
    /**
209
     * {@inheritDoc}
210
     */
211 4
    public function post(
212
        string $name,
213
        string $path,
214
        RequestHandlerInterface $requestHandler,
215
        array $middlewares = [],
216
        array $attributes = []
217
    ) : RouteInterface {
218 4
        return $this->route(
219 4
            $name,
220 4
            $path,
221 4
            [RequestMethodInterface::METHOD_POST],
222 4
            $requestHandler,
223 4
            $middlewares,
224 4
            $attributes
225
        );
226
    }
227
228
    /**
229
     * {@inheritDoc}
230
     */
231 4
    public function put(
232
        string $name,
233
        string $path,
234
        RequestHandlerInterface $requestHandler,
235
        array $middlewares = [],
236
        array $attributes = []
237
    ) : RouteInterface {
238 4
        return $this->route(
239 4
            $name,
240 4
            $path,
241 4
            [RequestMethodInterface::METHOD_PUT],
242 4
            $requestHandler,
243 4
            $middlewares,
244 4
            $attributes
245
        );
246
    }
247
248
    /**
249
     * {@inheritDoc}
250
     */
251 4
    public function patch(
252
        string $name,
253
        string $path,
254
        RequestHandlerInterface $requestHandler,
255
        array $middlewares = [],
256
        array $attributes = []
257
    ) : RouteInterface {
258 4
        return $this->route(
259 4
            $name,
260 4
            $path,
261 4
            [RequestMethodInterface::METHOD_PATCH],
262 4
            $requestHandler,
263 4
            $middlewares,
264 4
            $attributes
265
        );
266
    }
267
268
    /**
269
     * {@inheritDoc}
270
     */
271 4
    public function delete(
272
        string $name,
273
        string $path,
274
        RequestHandlerInterface $requestHandler,
275
        array $middlewares = [],
276
        array $attributes = []
277
    ) : RouteInterface {
278 4
        return $this->route(
279 4
            $name,
280 4
            $path,
281 4
            [RequestMethodInterface::METHOD_DELETE],
282 4
            $requestHandler,
283 4
            $middlewares,
284 4
            $attributes
285
        );
286
    }
287
288
    /**
289
     * {@inheritDoc}
290
     */
291 4
    public function purge(
292
        string $name,
293
        string $path,
294
        RequestHandlerInterface $requestHandler,
295
        array $middlewares = [],
296
        array $attributes = []
297
    ) : RouteInterface {
298 4
        return $this->route(
299 4
            $name,
300 4
            $path,
301 4
            [RequestMethodInterface::METHOD_PURGE],
302 4
            $requestHandler,
303 4
            $middlewares,
304 4
            $attributes
305
        );
306
    }
307
308
    /**
309
     * {@inheritDoc}
310
     */
311 1
    public function group(string $prefix, callable $callback) : void
312
    {
313 1
        $children = new self;
314 1
        $children->setPrefix($this->prefix . $prefix);
315 1
        $children->addMiddlewares(...$this->middlewares);
316
317 1
        $callback($children);
318
319 1
        $this->addRoutes(...$children->getRoutes());
320 1
    }
321
}
322