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

RouteCollector::resolveRequestHandler()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 8.8333
cc 7
nc 5
nop 2
crap 7
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 Psr\Container\ContainerInterface;
18
use Sunrise\Http\Router\Exception\UnresolvableReferenceException;
19
20
/**
21
 * RouteCollector
22
 */
23
class RouteCollector
24
{
25
26
    /**
27
     * Route collection factory of the collector
28
     *
29
     * @var RouteCollectionFactoryInterface
30
     */
31
    private $collectionFactory;
32
33
    /**
34
     * Route factory of the collector
35
     *
36
     * @var RouteFactoryInterface
37
     */
38
    private $routeFactory;
39
40
    /**
41
     * Reference resolver of the collector
42
     *
43
     * @var ReferenceResolverInterface
44
     */
45
    private $referenceResolver;
46
47
    /**
48
     * Route collection of the collector
49
     *
50
     * @var RouteCollectionInterface
51
     */
52
    private $collection;
53
54
    /**
55
     * Constructor of the class
56
     *
57
     * @param RouteCollectionFactoryInterface|null $collectionFactory
58
     * @param RouteFactoryInterface|null $routeFactory
59
     * @param ReferenceResolverInterface|null $referenceResolver
60
     */
61 14
    public function __construct(
62
        ?RouteCollectionFactoryInterface $collectionFactory = null,
63
        ?RouteFactoryInterface $routeFactory = null,
64
        ?ReferenceResolverInterface $referenceResolver = null
65
    ) {
66 14
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
67 14
        $this->routeFactory = $routeFactory ?? new RouteFactory();
68 14
        $this->referenceResolver = $referenceResolver ?? new ReferenceResolver();
69
70 14
        $this->collection = $this->collectionFactory->createCollection();
71 14
    }
72
73
    /**
74
     * Gets the collector collection
75
     *
76
     * @return RouteCollectionInterface
77
     */
78 13
    public function getCollection() : RouteCollectionInterface
79
    {
80 13
        return $this->collection;
81
    }
82
83
    /**
84
     * Gets the collector container
85
     *
86
     * @return ContainerInterface|null
87
     *
88
     * @since 2.9.0
89
     */
90 1
    public function getContainer() : ?ContainerInterface
91
    {
92 1
        return $this->referenceResolver->getContainer();
93
    }
94
95
    /**
96
     * Sets the given container to the collector
97
     *
98
     * @param ContainerInterface|null $container
99
     *
100
     * @return void
101
     *
102
     * @since 2.9.0
103
     */
104 1
    public function setContainer(?ContainerInterface $container) : void
105
    {
106 1
        $this->referenceResolver->setContainer($container);
107 1
    }
108
109
    /**
110
     * Makes a new route from the given parameters
111
     *
112
     * @param string $name
113
     * @param string $path
114
     * @param string[] $methods
115
     * @param mixed $requestHandler
116
     * @param array $middlewares
117
     * @param array $attributes
118
     *
119
     * @return RouteInterface
120
     *
121
     * @throws UnresolvableReferenceException
122
     *         If the given request handler or one of the given middlewares cannot be resolved.
123
     */
124 13
    public function route(
125
        string $name,
126
        string $path,
127
        array $methods,
128
        $requestHandler,
129
        array $middlewares = [],
130
        array $attributes = []
131
    ) : RouteInterface {
132 13
        foreach ($middlewares as &$middleware) {
133 8
            $middleware = $this->referenceResolver->toMiddleware($middleware);
134
        }
135
136 13
        $route = $this->routeFactory->createRoute(
137 13
            $name,
138
            $path,
139
            $methods,
140 13
            $this->referenceResolver->toRequestHandler($requestHandler),
141
            $middlewares,
142
            $attributes
143
        );
144
145 13
        $this->collection->add($route);
146
147 13
        return $route;
148
    }
149
150
    /**
151
     * Makes a new route that will respond to HEAD requests
152
     *
153
     * @param string $name
154
     * @param string $path
155
     * @param mixed $requestHandler
156
     * @param array $middlewares
157
     * @param array $attributes
158
     *
159
     * @return RouteInterface
160
     *
161
     * @throws UnresolvableReferenceException
162
     *         If the given request handler or one of the given middlewares cannot be resolved.
163
     */
164 1
    public function head(
165
        string $name,
166
        string $path,
167
        $requestHandler,
168
        array $middlewares = [],
169
        array $attributes = []
170
    ) : RouteInterface {
171 1
        return $this->route(
172 1
            $name,
173
            $path,
174 1
            [Router::METHOD_HEAD],
175
            $requestHandler,
176
            $middlewares,
177
            $attributes
178
        );
179
    }
180
181
    /**
182
     * Makes a new route that will respond to GET requests
183
     *
184
     * @param string $name
185
     * @param string $path
186
     * @param mixed $requestHandler
187
     * @param array $middlewares
188
     * @param array $attributes
189
     *
190
     * @return RouteInterface
191
     *
192
     * @throws UnresolvableReferenceException
193
     *         If the given request handler or one of the given middlewares cannot be resolved.
194
     */
195 6
    public function get(
196
        string $name,
197
        string $path,
198
        $requestHandler,
199
        array $middlewares = [],
200
        array $attributes = []
201
    ) : RouteInterface {
202 6
        return $this->route(
203 6
            $name,
204
            $path,
205 6
            [Router::METHOD_GET],
206
            $requestHandler,
207
            $middlewares,
208
            $attributes
209
        );
210
    }
211
212
    /**
213
     * Makes a new route that will respond to POST requests
214
     *
215
     * @param string $name
216
     * @param string $path
217
     * @param mixed $requestHandler
218
     * @param array $middlewares
219
     * @param array $attributes
220
     *
221
     * @return RouteInterface
222
     *
223
     * @throws UnresolvableReferenceException
224
     *         If the given request handler or one of the given middlewares cannot be resolved.
225
     */
226 1
    public function post(
227
        string $name,
228
        string $path,
229
        $requestHandler,
230
        array $middlewares = [],
231
        array $attributes = []
232
    ) : RouteInterface {
233 1
        return $this->route(
234 1
            $name,
235
            $path,
236 1
            [Router::METHOD_POST],
237
            $requestHandler,
238
            $middlewares,
239
            $attributes
240
        );
241
    }
242
243
    /**
244
     * Makes a new route that will respond to PUT requests
245
     *
246
     * @param string $name
247
     * @param string $path
248
     * @param mixed $requestHandler
249
     * @param array $middlewares
250
     * @param array $attributes
251
     *
252
     * @return RouteInterface
253
     *
254
     * @throws UnresolvableReferenceException
255
     *         If the given request handler or one of the given middlewares cannot be resolved.
256
     */
257 1
    public function put(
258
        string $name,
259
        string $path,
260
        $requestHandler,
261
        array $middlewares = [],
262
        array $attributes = []
263
    ) : RouteInterface {
264 1
        return $this->route(
265 1
            $name,
266
            $path,
267 1
            [Router::METHOD_PUT],
268
            $requestHandler,
269
            $middlewares,
270
            $attributes
271
        );
272
    }
273
274
    /**
275
     * Makes a new route that will respond to PATCH requests
276
     *
277
     * @param string $name
278
     * @param string $path
279
     * @param mixed $requestHandler
280
     * @param array $middlewares
281
     * @param array $attributes
282
     *
283
     * @return RouteInterface
284
     *
285
     * @throws UnresolvableReferenceException
286
     *         If the given request handler or one of the given middlewares cannot be resolved.
287
     */
288 1
    public function patch(
289
        string $name,
290
        string $path,
291
        $requestHandler,
292
        array $middlewares = [],
293
        array $attributes = []
294
    ) : RouteInterface {
295 1
        return $this->route(
296 1
            $name,
297
            $path,
298 1
            [Router::METHOD_PATCH],
299
            $requestHandler,
300
            $middlewares,
301
            $attributes
302
        );
303
    }
304
305
    /**
306
     * Makes a new route that will respond to DELETE requests
307
     *
308
     * @param string $name
309
     * @param string $path
310
     * @param mixed $requestHandler
311
     * @param array $middlewares
312
     * @param array $attributes
313
     *
314
     * @return RouteInterface
315
     *
316
     * @throws UnresolvableReferenceException
317
     *         If the given request handler or one of the given middlewares cannot be resolved.
318
     */
319 1
    public function delete(
320
        string $name,
321
        string $path,
322
        $requestHandler,
323
        array $middlewares = [],
324
        array $attributes = []
325
    ) : RouteInterface {
326 1
        return $this->route(
327 1
            $name,
328
            $path,
329 1
            [Router::METHOD_DELETE],
330
            $requestHandler,
331
            $middlewares,
332
            $attributes
333
        );
334
    }
335
336
    /**
337
     * Makes a new route that will respond to PURGE requests
338
     *
339
     * @param string $name
340
     * @param string $path
341
     * @param mixed $requestHandler
342
     * @param array $middlewares
343
     * @param array $attributes
344
     *
345
     * @return RouteInterface
346
     *
347
     * @throws UnresolvableReferenceException
348
     *         If the given request handler or one of the given middlewares cannot be resolved.
349
     */
350 1
    public function purge(
351
        string $name,
352
        string $path,
353
        $requestHandler,
354
        array $middlewares = [],
355
        array $attributes = []
356
    ) : RouteInterface {
357 1
        return $this->route(
358 1
            $name,
359
            $path,
360 1
            [Router::METHOD_PURGE],
361
            $requestHandler,
362
            $middlewares,
363
            $attributes
364
        );
365
    }
366
367
    /**
368
     * Route grouping logic
369
     *
370
     * @param callable $callback
371
     * @param array $middlewares
372
     *
373
     * @return RouteCollectionInterface
374
     *
375
     * @throws UnresolvableReferenceException
376
     *         If one of the given middlewares cannot be resolved.
377
     */
378 1
    public function group(callable $callback, array $middlewares = []) : RouteCollectionInterface
379
    {
380 1
        foreach ($middlewares as &$middleware) {
381 1
            $middleware = $this->referenceResolver->toMiddleware($middleware);
382
        }
383
384 1
        $collector = new self(
385 1
            $this->collectionFactory,
386 1
            $this->routeFactory,
387 1
            $this->referenceResolver
388
        );
389
390 1
        $callback($collector);
391
392 1
        $collector->collection->prependMiddleware(...$middlewares);
393
394 1
        $this->collection->add(...$collector->collection->all());
395
396 1
        return $collector->collection;
397
    }
398
}
399