Passed
Push — master ( f3ac5e...2b5089 )
by Anatoly
50s queued 10s
created

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