Test Failed
Pull Request — master (#63)
by Anatoly
10:52
created

Route::addMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
use Sunrise\Http\Router\RequestHandler\QueueableRequestHandler;
22
23
/**
24
 * Import functions
25
 */
26
use function rtrim;
27
use function strtoupper;
28
29
/**
30
 * Route
31
 *
32
 * Use the factory to create this class.
33
 */
34
class Route implements RouteInterface
35
{
36
37
    /**
38
     * Server Request attribute name for the route instance
39
     *
40
     * @var string
41
     */
42
    public const ATTR_NAME_FOR_ROUTE = '@route';
43
44
    /**
45
     * Server Request attribute name for the route name
46
     *
47
     * @var string
48
     */
49
    public const ATTR_NAME_FOR_ROUTE_NAME = '@route-name';
50
51
    /**
52
     * The route name
53
     *
54
     * @var string
55
     */
56
    private $name;
57
58
    /**
59
     * The route host
60
     *
61
     * @var null|string
62
     */
63
    private $host = null;
64
65
    /**
66
     * The route path
67
     *
68
     * @var string
69
     */
70
    private $path;
71
72
    /**
73
     * The route methods
74
     *
75
     * @var string[]
76
     */
77
    private $methods;
78
79
    /**
80
     * The route request handler
81
     *
82
     * @var RequestHandlerInterface
83
     */
84
    private $requestHandler;
85
86
    /**
87
     * The route middlewares
88
     *
89
     * @var MiddlewareInterface[]
90
     */
91
    private $middlewares = [];
92
93
    /**
94
     * The route attributes
95
     *
96
     * @var array
97
     */
98
    private $attributes = [];
99
100
    /**
101
     * The route summary
102
     *
103
     * @var string
104
     */
105
    private $summary = '';
106
107
    /**
108
     * The route description
109
     *
110
     * @var string
111
     */
112
    private $description = '';
113
114
    /**
115
     * The route tags
116
     *
117
     * @var string[]
118
     */
119
    private $tags = [];
120
121
    /**
122
     * Constructor of the class
123
     *
124 83
     * @param string $name
125
     * @param string $path
126
     * @param string[] $methods
127
     * @param RequestHandlerInterface $requestHandler
128
     * @param MiddlewareInterface[] $middlewares
129
     * @param array $attributes
130
     */
131
    public function __construct(
132 83
        string $name,
133 83
        string $path,
134 83
        array $methods,
135 83
        RequestHandlerInterface $requestHandler,
136 83
        array $middlewares = [],
137 83
        array $attributes = []
138 83
    ) {
139
        $this->setName($name);
140
        $this->setPath($path);
141
        $this->setMethods(...$methods);
142
        $this->setRequestHandler($requestHandler);
143 44
        $this->setMiddlewares(...$middlewares);
144
        $this->setAttributes($attributes);
145 44
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150
    public function getName() : string
151 41
    {
152
        return $this->name;
153 41
    }
154
155
    /**
156
     * {@inheritDoc}
157
     */
158
    public function getHost() : ?string
159 40
    {
160
        return $this->host;
161 40
    }
162
163
    /**
164
     * {@inheritDoc}
165
     */
166
    public function getPath() : string
167 30
    {
168
        return $this->path;
169 30
    }
170
171
    /**
172
     * {@inheritDoc}
173
     */
174
    public function getMethods() : array
175 29
    {
176
        return $this->methods;
177 29
    }
178
179
    /**
180
     * {@inheritDoc}
181
     */
182
    public function getRequestHandler() : RequestHandlerInterface
183 27
    {
184
        return $this->requestHandler;
185 27
    }
186
187
    /**
188
     * {@inheritDoc}
189
     */
190
    public function getMiddlewares() : array
191 2
    {
192
        return $this->middlewares;
193 2
    }
194
195
    /**
196
     * {@inheritDoc}
197
     */
198
    public function getAttributes() : array
199 2
    {
200
        return $this->attributes;
201 2
    }
202
203
    /**
204
     * {@inheritDoc}
205
     */
206
    public function getSummary() : string
207 2
    {
208
        return $this->summary;
209 2
    }
210
211
    /**
212
     * {@inheritDoc}
213
     */
214
    public function getDescription() : string
215 83
    {
216
        return $this->description;
217 83
    }
218
219 83
    /**
220
     * {@inheritDoc}
221
     */
222
    public function getTags() : array
223
    {
224
        return $this->tags;
225 83
    }
226
227 83
    /**
228
     * {@inheritDoc}
229 83
     */
230
    public function setName(string $name) : RouteInterface
231
    {
232
        $this->name = $name;
233
234
        return $this;
235 83
    }
236
237 83
    /**
238 83
     * {@inheritDoc}
239
     */
240
    public function setHost(?string $host) : RouteInterface
241 83
    {
242
        $this->host = $host;
243 83
244
        return $this;
245
    }
246
247
    /**
248
     * {@inheritDoc}
249 83
     */
250
    public function setPath(string $path) : RouteInterface
251 83
    {
252
        $this->path = $path;
253 83
254
        return $this;
255
    }
256
257
    /**
258
     * {@inheritDoc}
259 83
     */
260
    public function setMethods(string ...$methods) : RouteInterface
261 83
    {
262
        foreach ($methods as &$method) {
263 83
            $method = strtoupper($method);
264
        }
265
266
        $this->methods = $methods;
267
268
        return $this;
269 83
    }
270
271 83
    /**
272
     * {@inheritDoc}
273 83
     */
274
    public function setRequestHandler(RequestHandlerInterface $requestHandler) : RouteInterface
275
    {
276
        $this->requestHandler = $requestHandler;
277
278
        return $this;
279 5
    }
280
281 5
    /**
282
     * {@inheritDoc}
283 5
     */
284
    public function setMiddlewares(MiddlewareInterface ...$middlewares) : RouteInterface
285
    {
286
        $this->middlewares = $middlewares;
287
288
        return $this;
289 5
    }
290
291 5
    /**
292
     * {@inheritDoc}
293 5
     */
294
    public function setAttributes(array $attributes) : RouteInterface
295
    {
296
        $this->attributes = $attributes;
297
298
        return $this;
299 5
    }
300
301 5
    /**
302
     * {@inheritDoc}
303 5
     */
304
    public function setSummary(string $summary) : RouteInterface
305
    {
306
        $this->summary = $summary;
307
308
        return $this;
309 4
    }
310
311
    /**
312 4
     * {@inheritDoc}
313
     */
314 4
    public function setDescription(string $description) : RouteInterface
315
    {
316 4
        $this->description = $description;
317
318
        return $this;
319
    }
320
321
    /**
322 2
     * {@inheritDoc}
323
     */
324 2
    public function setTags(string ...$tags) : RouteInterface
325
    {
326 2
        $this->tags = $tags;
327
328
        return $this;
329
    }
330
331
    /**
332 3
     * {@inheritDoc}
333
     */
334 3
    public function addPrefix(string $prefix) : RouteInterface
335 3
    {
336
        // https://github.com/sunrise-php/http-router/issues/26
337
        $prefix = rtrim($prefix, '/');
338 3
339
        $this->path = $prefix . $this->path;
340
341
        return $this;
342
    }
343
344 2
    /**
345
     * {@inheritDoc}
346 2
     */
347 2
    public function addSuffix(string $suffix) : RouteInterface
348
    {
349
        $this->path .= $suffix;
350 2
351
        return $this;
352
    }
353
354
    /**
355
     * {@inheritDoc}
356 6
     */
357
    public function addMethod(string ...$methods) : RouteInterface
358 6
    {
359
        foreach ($methods as $method) {
360 6
            $this->methods[] = strtoupper($method);
361 1
        }
362
363
        return $this;
364 6
    }
365
366
    /**
367
     * {@inheritDoc}
368
     */
369
    public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteInterface
370 5
    {
371
        foreach ($middlewares as $middleware) {
372 5
            $this->middlewares[] = $middleware;
373 5
        }
374
375 5
        return $this;
376 5
    }
377
378
    /**
379 5
     * {@inheritDoc}
380 5
     */
381
    public function withAddedAttributes(array $attributes) : RouteInterface
382 5
    {
383
        $clone = clone $this;
384
385
        foreach ($attributes as $key => $value) {
386
            $clone->attributes[$key] = $value;
387
        }
388
389
        return $clone;
390
    }
391
392
    /**
393
     * {@inheritDoc}
394
     */
395
    public function handle(ServerRequestInterface $request) : ResponseInterface
396
    {
397
        $request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTE, $this);
398
        $request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTE_NAME, $this->name);
399
400
        foreach ($this->attributes as $key => $value) {
401
            $request = $request->withAttribute($key, $value);
402
        }
403
404
        $handler = new QueueableRequestHandler($this->requestHandler);
405
        $handler->add(...$this->middlewares);
406
407
        return $handler->handle($request);
408
    }
409
}
410