Passed
Push — master ( 90cce9...07b020 )
by Anatoly
40s queued 12s
created

Route::getHolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

402
        $request = $request->withAttribute(/** @scrutinizer ignore-deprecated */ self::ATTR_NAME_FOR_ROUTE_NAME, $this->name);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
403
404 8
        foreach ($this->attributes as $key => $value) {
405 1
            $request = $request->withAttribute($key, $value);
406
        }
407
408 8
        if (empty($this->middlewares)) {
409 6
            return $this->requestHandler->handle($request);
410
        }
411
412 2
        $handler = new QueueableRequestHandler($this->requestHandler);
413 2
        $handler->add(...$this->middlewares);
414
415 2
        return $handler->handle($request);
416
    }
417
}
418