Test Failed
Pull Request — master (#34)
by Anatoly
02:07
created

RouteCollector::route()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 20
rs 9.9666
cc 1
nc 1
nop 6
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\Http\Server\MiddlewareInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
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
     * Route collection of the collector
42
     *
43
     * @var RouteCollectionInterface
44
     */
45
    private $collection;
46
47
    /**
48
     * Constructor of the class
49
     *
50
     * @param null|RouteCollectionFactoryInterface $collectionFactory
51
     * @param null|RouteFactoryInterface $routeFactory
52
     */
53
    public function __construct(
54
        RouteCollectionFactoryInterface $collectionFactory = null,
55
        RouteFactoryInterface $routeFactory = null
56
    ) {
57
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
58
        $this->routeFactory = $routeFactory ?? new RouteFactory();
59
60
        $this->collection = $this->collectionFactory->createCollection();
61
    }
62
63
    /**
64
     * Gets the collector collection
65
     *
66
     * @return RouteCollectionInterface
67
     */
68
    public function getCollection() : RouteCollectionInterface
69
    {
70
        return $this->collection;
71
    }
72
73
    /**
74
     * Makes a new route from the given parameters
75
     *
76
     * @param string $name
77
     * @param string $path
78
     * @param string[] $methods
79
     * @param RequestHandlerInterface $requestHandler
80
     * @param MiddlewareInterface[] $middlewares
81
     * @param array $attributes
82
     *
83
     * @return RouteInterface
84
     */
85
    public function route(
86
        string $name,
87
        string $path,
88
        array $methods,
89
        RequestHandlerInterface $requestHandler,
90
        array $middlewares = [],
91
        array $attributes = []
92
    ) : RouteInterface {
93
        $route = $this->routeFactory->createRoute(
94
            $name,
95
            $path,
96
            $methods,
97
            $requestHandler,
98
            $middlewares,
99
            $attributes
100
        );
101
102
        $this->collection->add($route);
103
104
        return $route;
105
    }
106
107
    /**
108
     * Makes a new route that will respond to HEAD requests
109
     *
110
     * @param string $name
111
     * @param string $path
112
     * @param RequestHandlerInterface $requestHandler
113
     * @param MiddlewareInterface[] $middlewares
114
     * @param array $attributes
115
     *
116
     * @return RouteInterface
117
     */
118
    public function head(
119
        string $name,
120
        string $path,
121
        RequestHandlerInterface $requestHandler,
122
        array $middlewares = [],
123
        array $attributes = []
124
    ) : RouteInterface {
125
        return $this->route(
126
            $name,
127
            $path,
128
            [Router::METHOD_HEAD],
129
            $requestHandler,
130
            $middlewares,
131
            $attributes
132
        );
133
    }
134
135
    /**
136
     * Makes a new route that will respond to GET requests
137
     *
138
     * @param string $name
139
     * @param string $path
140
     * @param RequestHandlerInterface $requestHandler
141
     * @param MiddlewareInterface[] $middlewares
142
     * @param array $attributes
143
     *
144
     * @return RouteInterface
145
     */
146
    public function get(
147
        string $name,
148
        string $path,
149
        RequestHandlerInterface $requestHandler,
150
        array $middlewares = [],
151
        array $attributes = []
152
    ) : RouteInterface {
153
        return $this->route(
154
            $name,
155
            $path,
156
            [Router::METHOD_GET],
157
            $requestHandler,
158
            $middlewares,
159
            $attributes
160
        );
161
    }
162
163
    /**
164
     * Makes a new route that will respond to POST requests
165
     *
166
     * @param string $name
167
     * @param string $path
168
     * @param RequestHandlerInterface $requestHandler
169
     * @param MiddlewareInterface[] $middlewares
170
     * @param array $attributes
171
     *
172
     * @return RouteInterface
173
     */
174
    public function post(
175
        string $name,
176
        string $path,
177
        RequestHandlerInterface $requestHandler,
178
        array $middlewares = [],
179
        array $attributes = []
180
    ) : RouteInterface {
181
        return $this->route(
182
            $name,
183
            $path,
184
            [Router::METHOD_POST],
185
            $requestHandler,
186
            $middlewares,
187
            $attributes
188
        );
189
    }
190
191
    /**
192
     * Makes a new route that will respond to PUT requests
193
     *
194
     * @param string $name
195
     * @param string $path
196
     * @param RequestHandlerInterface $requestHandler
197
     * @param MiddlewareInterface[] $middlewares
198
     * @param array $attributes
199
     *
200
     * @return RouteInterface
201
     */
202
    public function put(
203
        string $name,
204
        string $path,
205
        RequestHandlerInterface $requestHandler,
206
        array $middlewares = [],
207
        array $attributes = []
208
    ) : RouteInterface {
209
        return $this->route(
210
            $name,
211
            $path,
212
            [Router::METHOD_PUT],
213
            $requestHandler,
214
            $middlewares,
215
            $attributes
216
        );
217
    }
218
219
    /**
220
     * Makes a new route that will respond to PATCH requests
221
     *
222
     * @param string $name
223
     * @param string $path
224
     * @param RequestHandlerInterface $requestHandler
225
     * @param MiddlewareInterface[] $middlewares
226
     * @param array $attributes
227
     *
228
     * @return RouteInterface
229
     */
230
    public function patch(
231
        string $name,
232
        string $path,
233
        RequestHandlerInterface $requestHandler,
234
        array $middlewares = [],
235
        array $attributes = []
236
    ) : RouteInterface {
237
        return $this->route(
238
            $name,
239
            $path,
240
            [Router::METHOD_PATCH],
241
            $requestHandler,
242
            $middlewares,
243
            $attributes
244
        );
245
    }
246
247
    /**
248
     * Makes a new route that will respond to DELETE requests
249
     *
250
     * @param string $name
251
     * @param string $path
252
     * @param RequestHandlerInterface $requestHandler
253
     * @param MiddlewareInterface[] $middlewares
254
     * @param array $attributes
255
     *
256
     * @return RouteInterface
257
     */
258
    public function delete(
259
        string $name,
260
        string $path,
261
        RequestHandlerInterface $requestHandler,
262
        array $middlewares = [],
263
        array $attributes = []
264
    ) : RouteInterface {
265
        return $this->route(
266
            $name,
267
            $path,
268
            [Router::METHOD_DELETE],
269
            $requestHandler,
270
            $middlewares,
271
            $attributes
272
        );
273
    }
274
275
    /**
276
     * Makes a new route that will respond to PURGE requests
277
     *
278
     * @param string $name
279
     * @param string $path
280
     * @param RequestHandlerInterface $requestHandler
281
     * @param MiddlewareInterface[] $middlewares
282
     * @param array $attributes
283
     *
284
     * @return RouteInterface
285
     */
286
    public function purge(
287
        string $name,
288
        string $path,
289
        RequestHandlerInterface $requestHandler,
290
        array $middlewares = [],
291
        array $attributes = []
292
    ) : RouteInterface {
293
        return $this->route(
294
            $name,
295
            $path,
296
            [Router::METHOD_PURGE],
297
            $requestHandler,
298
            $middlewares,
299
            $attributes
300
        );
301
    }
302
303
    /**
304
     * Route grouping logic
305
     *
306
     * @param callable $callback
307
     *
308
     * @return RouteCollectorGroupAction
309
     */
310
    public function group(callable $callback) : RouteCollectorGroupAction
311
    {
312
        $collector = new self(
313
            $this->collectionFactory,
314
            $this->routeFactory
315
        );
316
317
        $callback($collector);
318
319
        $this->collection->add(...$collector->collection->all());
320
321
        return new RouteCollectorGroupAction($collector->collection);
322
    }
323
}
324