Passed
Push — master ( e1642a...3c4285 )
by Anatoly
01:03 queued 12s
created

Route   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 453
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 32
eloc 109
c 10
b 0
f 0
dl 0
loc 453
ccs 121
cts 121
cp 1
rs 9.84

21 Methods

Rating   Name   Duplication   Size   Complexity  
A extractPath() 0 10 1
A getMethods() 0 3 1
A extractPriority() 0 15 2
A extractHost() 0 15 2
A getDescription() 0 3 1
A extractMethods() 0 21 3
A getTags() 0 3 1
A getSummary() 0 3 1
A extractMiddlewares() 0 23 3
A getName() 0 3 1
A getPriority() 0 3 1
A __construct() 0 12 1
A extractSummary() 0 15 2
A getHost() 0 3 1
A extractTags() 0 22 3
A getMiddlewares() 0 3 1
A extractDescription() 0 15 2
A getPath() 0 3 1
A extractAttributes() 0 15 2
A getAttributes() 0 3 1
A extractName() 0 10 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\Annotation;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Server\MiddlewareInterface;
18
use Sunrise\Http\Router\Exception\InvalidDescriptorArgumentException;
19
use Sunrise\Http\Router\RouteDescriptorInterface;
20
21
/**
22
 * Import functions
23
 */
24
use function array_key_exists;
25
26
/**
27
 * Annotation for a route description
28
 *
29
 * @Annotation
30
 *
31
 * @Target({"CLASS"})
32
 */
33
final class Route implements RouteDescriptorInterface
34
{
35
36
    /**
37
     * A route name
38
     *
39
     * @var string
40
     */
41
    private $name;
42
43
    /**
44
     * A route host
45
     *
46
     * @var null|string
47
     */
48
    private $host;
49
50
    /**
51
     * A route path
52
     *
53
     * @var string
54
     */
55
    private $path;
56
57
    /**
58
     * A route methods
59
     *
60
     * @var string[]
61
     */
62
    private $methods;
63
64
    /**
65
     * A route middlewares
66
     *
67
     * @var string[]
68
     */
69
    private $middlewares;
70
71
    /**
72
     * A route attributes
73
     *
74
     * @var array
75
     */
76
    private $attributes;
77
78
    /**
79
     * A route summary
80
     *
81
     * @var string
82
     */
83
    private $summary;
84
85
    /**
86
     * A route description
87
     *
88
     * @var string
89
     */
90
    private $description;
91
92
    /**
93
     * A route tags
94
     *
95
     * @var string[]
96
     */
97
    private $tags;
98
99
    /**
100
     * A route priority
101
     *
102
     * @var int
103
     */
104
    private $priority;
105
106
    /**
107
     * Constructor of the annotation
108
     *
109
     * @param array $params
110
     *
111
     * @throws InvalidDescriptorArgumentException
112
     */
113 138
    public function __construct(array $params)
114
    {
115 138
        $this->name = $this->extractName($params);
116 125
        $this->host = $this->extractHost($params);
117 115
        $this->path = $this->extractPath($params);
118 102
        $this->methods = $this->extractMethods($params);
119 80
        $this->middlewares = $this->extractMiddlewares($params);
120 58
        $this->attributes = $this->extractAttributes($params);
121 49
        $this->summary = $this->extractSummary($params);
122 41
        $this->description = $this->extractDescription($params);
123 33
        $this->tags = $this->extractTags($params);
124 17
        $this->priority = $this->extractPriority($params);
125 8
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130 8
    public function getName() : string
131
    {
132 8
        return $this->name;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 8
    public function getHost() : ?string
139
    {
140 8
        return $this->host;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 8
    public function getPath() : string
147
    {
148 8
        return $this->path;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 8
    public function getMethods() : array
155
    {
156 8
        return $this->methods;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 7
    public function getMiddlewares() : array
163
    {
164 7
        return $this->middlewares;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170 7
    public function getAttributes() : array
171
    {
172 7
        return $this->attributes;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 7
    public function getSummary() : string
179
    {
180 7
        return $this->summary;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186 7
    public function getDescription() : string
187
    {
188 7
        return $this->description;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194 7
    public function getTags() : array
195
    {
196 7
        return $this->tags;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202 5
    public function getPriority() : int
203
    {
204 5
        return $this->priority;
205
    }
206
207
    /**
208
     * Extracts a route name from the given parameters
209
     *
210
     * @param array $params
211
     *
212
     * @return string
213
     *
214
     * @throws InvalidDescriptorArgumentException
215
     *         If the given parameters contain an invalid route name.
216
     */
217 138
    private function extractName(array $params) : string
218
    {
219 138
        $name = $params['name'] ?? '';
220
221 138
        InvalidDescriptorArgumentException::throwIfEmptyString(
222 138
            $name,
223 138
            '@Route.name must contain a non-empty string.'
224
        );
225
226 125
        return $name;
227
    }
228
229
    /**
230
     * Extracts a route host from the given parameters
231
     *
232
     * @param array $params
233
     *
234
     * @return null|string
235
     *
236
     * @throws InvalidDescriptorArgumentException
237
     *         If the given parameters contain an invalid route host.
238
     */
239 125
    private function extractHost(array $params) : ?string
240
    {
241 125
        $host = $params['host'] ?? null;
242
243
        // isn't required parameter...
244 125
        if (null === $host) {
245 114
            return null;
246
        }
247
248 14
        InvalidDescriptorArgumentException::throwIfEmptyString(
249 14
            $host,
250 14
            '@Route.host must contain a non-empty string.'
251
        );
252
253 4
        return $host;
254
    }
255
256
    /**
257
     * Extracts a route path from the given parameters
258
     *
259
     * @param array $params
260
     *
261
     * @return string
262
     *
263
     * @throws InvalidDescriptorArgumentException
264
     *         If the given parameters contain an invalid route path.
265
     */
266 115
    private function extractPath(array $params) : string
267
    {
268 115
        $path = $params['path'] ?? '';
269
270 115
        InvalidDescriptorArgumentException::throwIfEmptyString(
271 115
            $path,
272 115
            '@Route.path must contain a non-empty string.'
273
        );
274
275 102
        return $path;
276
    }
277
278
    /**
279
     * Extracts a route method(s) from the given parameters
280
     *
281
     * @param array $params
282
     *
283
     * @return string[]
284
     *
285
     * @throws InvalidDescriptorArgumentException
286
     *         If the given parameters contain an invalid route method(s).
287
     */
288 102
    private function extractMethods(array $params) : array
289
    {
290 102
        if (array_key_exists('method', $params)) {
291 2
            $params['methods'][] = $params['method'];
292
        }
293
294 102
        $methods = $params['methods'] ?? [];
295
296 102
        InvalidDescriptorArgumentException::throwIfEmptyArray(
297 102
            $methods,
298 102
            '@Route.methods must contain a non-empty array.'
299
        );
300
301 89
        foreach ($methods as $method) {
302 89
            InvalidDescriptorArgumentException::throwIfEmptyString(
303 89
                $method,
304 89
                '@Route.methods must contain non-empty strings.'
305
            );
306
        }
307
308 80
        return $methods;
309
    }
310
311
    /**
312
     * Extracts a route middlewares from the given parameters
313
     *
314
     * @param array $params
315
     *
316
     * @return string[]
317
     *
318
     * @throws InvalidDescriptorArgumentException
319
     *         If the given parameters contain an invalid route middlewares.
320
     */
321 80
    private function extractMiddlewares(array $params) : array
322
    {
323 80
        $middlewares = $params['middlewares'] ?? null;
324
325
        // isn't required parameter...
326 80
        if (null === $middlewares) {
327 53
            return [];
328
        }
329
330 27
        InvalidDescriptorArgumentException::throwIfNotArray(
331 27
            $middlewares,
332 27
            '@Route.middlewares must contain an array.'
333
        );
334
335 18
        foreach ($middlewares as $middleware) {
336 18
            InvalidDescriptorArgumentException::throwIfNotImplemented(
337 18
                $middleware,
338 18
                MiddlewareInterface::class,
339 18
                '@Route.middlewares must contain middlewares.'
340
            );
341
        }
342
343 5
        return $middlewares;
344
    }
345
346
    /**
347
     * Extracts a route attributes from the given parameters
348
     *
349
     * @param array $params
350
     *
351
     * @return array
352
     *
353
     * @throws InvalidDescriptorArgumentException
354
     *         If the given parameters contain an invalid route attributes.
355
     */
356 58
    private function extractAttributes(array $params) : array
357
    {
358 58
        $attributes = $params['attributes'] ?? null;
359
360
        // isn't required parameter...
361 58
        if (null === $attributes) {
362 46
            return [];
363
        }
364
365 13
        InvalidDescriptorArgumentException::throwIfNotArray(
366 13
            $attributes,
367 13
            '@Route.attributes must contain an array.'
368
        );
369
370 4
        return $attributes;
371
    }
372
373
    /**
374
     * Extracts a route summary from the given parameters
375
     *
376
     * @param array $params
377
     *
378
     * @return string
379
     *
380
     * @throws InvalidDescriptorArgumentException
381
     *         If the given parameters contain an invalid route summary.
382
     */
383 49
    private function extractSummary(array $params) : string
384
    {
385 49
        $summary = $params['summary'] ?? null;
386
387
        // isn't required parameter...
388 49
        if (null === $summary) {
389 40
            return '';
390
        }
391
392 12
        InvalidDescriptorArgumentException::throwIfNotString(
393 12
            $summary,
394 12
            '@Route.summary must contain a string.'
395
        );
396
397 4
        return $summary;
398
    }
399
400
    /**
401
     * Extracts a route description from the given parameters
402
     *
403
     * @param array $params
404
     *
405
     * @return string
406
     *
407
     * @throws InvalidDescriptorArgumentException
408
     *         If the given parameters contain an invalid route description.
409
     */
410 41
    private function extractDescription(array $params) : string
411
    {
412 41
        $description = $params['description'] ?? null;
413
414
        // isn't required parameter...
415 41
        if (null === $description) {
416 32
            return '';
417
        }
418
419 12
        InvalidDescriptorArgumentException::throwIfNotString(
420 12
            $description,
421 12
            '@Route.description must contain a string.'
422
        );
423
424 4
        return $description;
425
    }
426
427
    /**
428
     * Extracts a route tags from the given parameters
429
     *
430
     * @param array $params
431
     *
432
     * @return string[]
433
     *
434
     * @throws InvalidDescriptorArgumentException
435
     *         If the given parameters contain an invalid route tags.
436
     */
437 33
    private function extractTags(array $params) : array
438
    {
439 33
        $tags = $params['tags'] ?? null;
440
441
        // isn't required parameter...
442 33
        if (null === $tags) {
443 16
            return [];
444
        }
445
446 20
        InvalidDescriptorArgumentException::throwIfNotArray(
447 20
            $tags,
448 20
            '@Route.tags must contain an array.'
449
        );
450
451 12
        foreach ($tags as $tag) {
452 12
            InvalidDescriptorArgumentException::throwIfEmptyString(
453 12
                $tag,
454 12
                '@Route.tags must contain non-empty strings.'
455
            );
456
        }
457
458 4
        return $tags;
459
    }
460
461
    /**
462
     * Extracts a route priority from the given parameters
463
     *
464
     * @param array $params
465
     *
466
     * @return int
467
     *
468
     * @throws InvalidDescriptorArgumentException
469
     *         If the given parameters contain an invalid route priority.
470
     */
471 17
    private function extractPriority(array $params) : int
472
    {
473 17
        $priority = $params['priority'] ?? null;
474
475
        // isn't required parameter...
476 17
        if (null === $priority) {
477 5
            return 0;
478
        }
479
480 13
        InvalidDescriptorArgumentException::throwIfNotInteger(
481 13
            $priority,
482 13
            '@Route.priority must contain an integer.'
483
        );
484
485 4
        return $priority;
486
    }
487
}
488