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

RouteCollector   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 299
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 72
c 1
b 0
f 0
dl 0
loc 299
ccs 80
cts 80
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A put() 0 14 1
A getCollection() 0 3 1
A purge() 0 14 1
A delete() 0 14 1
A get() 0 14 1
A route() 0 20 1
A post() 0 14 1
A group() 0 12 1
A patch() 0 14 1
A head() 0 14 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 Psr\Http\Server\MiddlewareInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
20
/**
21
 * RouteCollector
22
 */
23
class RouteCollector
24
{
25
26
    /**
27
     * The collector a route collection factory
28
     *
29
     * @var RouteCollectionFactoryInterface
30
     */
31
    private $collectionFactory;
32
33
    /**
34
     * The collector a route factory
35
     *
36
     * @var RouteFactoryInterface
37
     */
38
    private $routeFactory;
39
40
    /**
41
     * The collector a route collection
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 30
    public function __construct(
54
        RouteCollectionFactoryInterface $collectionFactory = null,
55
        RouteFactoryInterface $routeFactory = null
56
    ) {
57 30
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
58 30
        $this->routeFactory = $routeFactory ?? new RouteFactory();
59
60 30
        $this->collection = $this->collectionFactory->createCollection();
61 30
    }
62
63
    /**
64
     * Gets the collector collection
65
     *
66
     * @return RouteCollectionInterface
67
     */
68 5
    public function getCollection() : RouteCollectionInterface
69
    {
70 5
        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 28
    public function route(
86
        string $name,
87
        string $path,
88
        array $methods,
89
        RequestHandlerInterface $requestHandler,
90
        array $middlewares = [],
91
        array $attributes = []
92
    ) : RouteInterface {
93 28
        $route = $this->routeFactory->createRoute(
94 28
            $name,
95 28
            $path,
96 28
            $methods,
97 28
            $requestHandler,
98 28
            $middlewares,
99 28
            $attributes
100
        );
101
102 28
        $this->collection->add($route);
103
104 28
        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 3
    public function head(
119
        string $name,
120
        string $path,
121
        RequestHandlerInterface $requestHandler,
122
        array $middlewares = [],
123
        array $attributes = []
124
    ) : RouteInterface {
125 3
        return $this->route(
126 3
            $name,
127 3
            $path,
128 3
            [Router::METHOD_HEAD],
129 3
            $requestHandler,
130 3
            $middlewares,
131 3
            $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 6
    public function get(
147
        string $name,
148
        string $path,
149
        RequestHandlerInterface $requestHandler,
150
        array $middlewares = [],
151
        array $attributes = []
152
    ) : RouteInterface {
153 6
        return $this->route(
154 6
            $name,
155 6
            $path,
156 6
            [Router::METHOD_GET],
157 6
            $requestHandler,
158 6
            $middlewares,
159 6
            $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 5
    public function post(
175
        string $name,
176
        string $path,
177
        RequestHandlerInterface $requestHandler,
178
        array $middlewares = [],
179
        array $attributes = []
180
    ) : RouteInterface {
181 5
        return $this->route(
182 5
            $name,
183 5
            $path,
184 5
            [Router::METHOD_POST],
185 5
            $requestHandler,
186 5
            $middlewares,
187 5
            $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 3
    public function put(
203
        string $name,
204
        string $path,
205
        RequestHandlerInterface $requestHandler,
206
        array $middlewares = [],
207
        array $attributes = []
208
    ) : RouteInterface {
209 3
        return $this->route(
210 3
            $name,
211 3
            $path,
212 3
            [Router::METHOD_PUT],
213 3
            $requestHandler,
214 3
            $middlewares,
215 3
            $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 5
    public function patch(
231
        string $name,
232
        string $path,
233
        RequestHandlerInterface $requestHandler,
234
        array $middlewares = [],
235
        array $attributes = []
236
    ) : RouteInterface {
237 5
        return $this->route(
238 5
            $name,
239 5
            $path,
240 5
            [Router::METHOD_PATCH],
241 5
            $requestHandler,
242 5
            $middlewares,
243 5
            $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 3
    public function delete(
259
        string $name,
260
        string $path,
261
        RequestHandlerInterface $requestHandler,
262
        array $middlewares = [],
263
        array $attributes = []
264
    ) : RouteInterface {
265 3
        return $this->route(
266 3
            $name,
267 3
            $path,
268 3
            [Router::METHOD_DELETE],
269 3
            $requestHandler,
270 3
            $middlewares,
271 3
            $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 3
    public function purge(
287
        string $name,
288
        string $path,
289
        RequestHandlerInterface $requestHandler,
290
        array $middlewares = [],
291
        array $attributes = []
292
    ) : RouteInterface {
293 3
        return $this->route(
294 3
            $name,
295 3
            $path,
296 3
            [Router::METHOD_PURGE],
297 3
            $requestHandler,
298 3
            $middlewares,
299 3
            $attributes
300
        );
301
    }
302
303
    /**
304
     * Route grouping logic
305
     *
306
     * @param callable $callback
307
     *
308
     * @return RouteCollectionGroupActionInterface
309
     */
310 3
    public function group(callable $callback) : RouteCollectionGroupActionInterface
311
    {
312 3
        $collector = new self(
313 3
            $this->collectionFactory,
314 3
            $this->routeFactory
315
        );
316
317 3
        $callback($collector);
318
319 3
        $this->collection->add(...$collector->collection->all());
320
321 3
        return new RouteCollectionGroupAction($collector->collection);
322
    }
323
}
324