Passed
Pull Request — master (#63)
by Anatoly
02:11
created

Route::extractMiddlewaresFromParams()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 5
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 7
rs 8.8333
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\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 is_array;
25
use function is_int;
26
use function is_string;
27
use function is_subclass_of;
28
29
/**
30
 * Annotation for a route description
31
 *
32
 * @Annotation
33
 *
34
 * @Target({"CLASS"})
35
 */
36
final class Route implements RouteDescriptorInterface
37
{
38
39
    /**
40
     * A route name
41
     *
42
     * @var string
43
     */
44
    private $name;
45
46
    /**
47
     * A route host
48
     *
49
     * @var null|string
50
     */
51
    private $host;
52
53
    /**
54
     * A route path
55
     *
56
     * @var string
57
     */
58
    private $path;
59
60
    /**
61
     * A route methods
62
     *
63
     * @var string[]
64
     */
65
    private $methods;
66
67
    /**
68
     * A route middlewares
69
     *
70
     * @var string[]
71
     */
72
    private $middlewares;
73
74
    /**
75
     * A route attributes
76
     *
77
     * @var array
78
     */
79
    private $attributes;
80
81
    /**
82
     * A route summary
83
     *
84
     * @var string
85
     */
86
    private $summary;
87
88
    /**
89
     * A route description
90
     *
91
     * @var string
92
     */
93
    private $description;
94
95
    /**
96
     * A route tags
97
     *
98
     * @var string[]
99
     */
100
    private $tags;
101
102
    /**
103
     * A route priority
104
     *
105
     * @var int
106
     */
107
    private $priority;
108
109
    /**
110
     * Constructor of the annotation
111
     *
112
     * @param array $params
113
     *
114
     * @throws InvalidDescriptorArgumentException
115
     */
116 136
    public function __construct(array $params)
117
    {
118 136
        $this->name = $this->extractNameFromParams($params);
119 123
        $this->host = $this->extractHostFromParams($params);
120 113
        $this->path = $this->extractPathFromParams($params);
121 100
        $this->methods = $this->extractMethodsFromParams($params);
122 78
        $this->middlewares = $this->extractMiddlewaresFromParams($params);
123 56
        $this->attributes = $this->extractAttributesFromParams($params);
124 47
        $this->summary = $this->extractSummaryFromParams($params);
125 39
        $this->description = $this->extractDescriptionFromParams($params);
126 32
        $this->tags = $this->extractTagsFromParams($params);
127 16
        $this->priority = $this->extractPriorityFromParams($params);
128 7
    }
129
130
    /**
131
     * {@inheritDoc}
132
     */
133 7
    public function getName() : string
134
    {
135 7
        return $this->name;
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141 7
    public function getHost() : ?string
142
    {
143 7
        return $this->host;
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 7
    public function getPath() : string
150
    {
151 7
        return $this->path;
152
    }
153
154
    /**
155
     * {@inheritDoc}
156
     */
157 7
    public function getMethods() : array
158
    {
159 7
        return $this->methods;
160
    }
161
162
    /**
163
     * {@inheritDoc}
164
     */
165 6
    public function getMiddlewares() : array
166
    {
167 6
        return $this->middlewares;
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173 6
    public function getAttributes() : array
174
    {
175 6
        return $this->attributes;
176
    }
177
178
    /**
179
     * {@inheritDoc}
180
     */
181 6
    public function getSummary() : string
182
    {
183 6
        return $this->summary;
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     */
189 6
    public function getDescription() : string
190
    {
191 6
        return $this->description;
192
    }
193
194
    /**
195
     * {@inheritDoc}
196
     */
197 6
    public function getTags() : array
198
    {
199 6
        return $this->tags;
200
    }
201
202
    /**
203
     * {@inheritDoc}
204
     */
205 4
    public function getPriority() : int
206
    {
207 4
        return $this->priority;
208
    }
209
210
    /**
211
     * @param array $params
212
     *
213
     * @return string
214
     *
215
     * @throws InvalidDescriptorArgumentException
216
     */
217 136
    private function extractNameFromParams(array $params) : string
218
    {
219 136
        if (!isset($params['name']) || '' === $params['name'] || !is_string($params['name'])) {
220 13
            throw new InvalidDescriptorArgumentException('@Route.name must contain a non-empty string.');
221
        }
222
223 123
        return $params['name'];
224
    }
225
226
    /**
227
     * @param array $params
228
     *
229
     * @return null|string
230
     *
231
     * @throws InvalidDescriptorArgumentException
232
     */
233 123
    private function extractHostFromParams(array $params) : ?string
234
    {
235 123
        if (isset($params['host']) && ('' === $params['host'] || !is_string($params['host']))) {
236 10
            throw new InvalidDescriptorArgumentException('@Route.host must contain a non-empty string.');
237
        }
238
239 113
        return $params['host'] ?? null;
240
    }
241
242
    /**
243
     * @param array $params
244
     *
245
     * @return string
246
     *
247
     * @throws InvalidDescriptorArgumentException
248
     */
249 113
    private function extractPathFromParams(array $params) : string
250
    {
251 113
        if (!isset($params['path']) || '' === $params['path'] || !is_string($params['path'])) {
252 13
            throw new InvalidDescriptorArgumentException('@Route.path must contain a non-empty string.');
253
        }
254
255 100
        return $params['path'];
256
    }
257
258
    /**
259
     * @param array $params
260
     *
261
     * @return string[]
262
     *
263
     * @throws InvalidDescriptorArgumentException
264
     */
265 100
    private function extractMethodsFromParams(array $params) : array
266
    {
267 100
        if (!isset($params['methods']) || [] === $params['methods'] || !is_array($params['methods'])) {
268 13
            throw new InvalidDescriptorArgumentException('@Route.methods must contain a non-empty array.');
269
        }
270
271 87
        foreach ($params['methods'] as $value) {
272 87
            if ('' === $value || !is_string($value)) {
273 9
                throw new InvalidDescriptorArgumentException('@Route.methods must contain non-empty strings.');
274
            }
275
        }
276
277 78
        return $params['methods'];
278
    }
279
280
    /**
281
     * @param array $params
282
     *
283
     * @return string[]
284
     *
285
     * @throws InvalidDescriptorArgumentException
286
     */
287 78
    private function extractMiddlewaresFromParams(array $params) : array
288
    {
289 78
        if (!isset($params['middlewares'])) {
290 52
            return [];
291
        }
292
293 26
        if (!is_array($params['middlewares'])) {
294 9
            throw new InvalidDescriptorArgumentException('@Route.middlewares must contain an array.');
295
        }
296
297 17
        foreach ($params['middlewares'] as $value) {
298 17
            if ('' === $value || !is_string($value) || !is_subclass_of($value, MiddlewareInterface::class)) {
299 13
                throw new InvalidDescriptorArgumentException(
300 13
                    '@Route.middlewares must contain the class names of existing middlewares.'
301
                );
302
            }
303
        }
304
305 4
        return $params['middlewares'];
306
    }
307
308
    /**
309
     * @param array $params
310
     *
311
     * @return array
312
     *
313
     * @throws InvalidDescriptorArgumentException
314
     */
315 56
    private function extractAttributesFromParams(array $params) : array
316
    {
317 56
        if (!isset($params['attributes'])) {
318 45
            return [];
319
        }
320
321 12
        if (!is_array($params['attributes'])) {
322 9
            throw new InvalidDescriptorArgumentException('@Route.attributes must contain an array.');
323
        }
324
325 3
        return $params['attributes'];
326
    }
327
328
    /**
329
     * @param array $params
330
     *
331
     * @return string
332
     *
333
     * @throws InvalidDescriptorArgumentException
334
     */
335 47
    private function extractSummaryFromParams(array $params) : string
336
    {
337 47
        if (!isset($params['summary'])) {
338 38
            return '';
339
        }
340
341 11
        if (!is_string($params['summary'])) {
342 8
            throw new InvalidDescriptorArgumentException('@Route.summary must contain a string.');
343
        }
344
345 3
        return $params['summary'];
346
    }
347
348
    /**
349
     * @param array $params
350
     *
351
     * @return string
352
     *
353
     * @throws InvalidDescriptorArgumentException
354
     */
355 39
    private function extractDescriptionFromParams(array $params) : string
356
    {
357 39
        if (!isset($params['description'])) {
358 31
            return '';
359
        }
360
361 10
        if (!is_string($params['description'])) {
362 7
            throw new InvalidDescriptorArgumentException('@Route.description must contain a string.');
363
        }
364
365 3
        return $params['description'];
366
    }
367
368
    /**
369
     * @param array $params
370
     *
371
     * @return string[]
372
     *
373
     * @throws InvalidDescriptorArgumentException
374
     */
375 32
    private function extractTagsFromParams(array $params) : array
376
    {
377 32
        if (!isset($params['tags'])) {
378 15
            return [];
379
        }
380
381 19
        if (!is_array($params['tags'])) {
382 8
            throw new InvalidDescriptorArgumentException('@Route.tags must contain an array.');
383
        }
384
385 11
        foreach ($params['tags'] as $value) {
386 11
            if ('' === $value || !is_string($value)) {
387 8
                throw new InvalidDescriptorArgumentException('@Route.tags must contain non-empty strings.');
388
            }
389
        }
390
391 3
        return $params['tags'];
392
    }
393
394
    /**
395
     * @param array $params
396
     *
397
     * @return int
398
     *
399
     * @throws InvalidDescriptorArgumentException
400
     */
401 16
    private function extractPriorityFromParams(array $params) : int
402
    {
403 16
        if (!isset($params['priority'])) {
404 5
            return 0;
405
        }
406
407 12
        if (!is_int($params['priority'])) {
408 9
            throw new InvalidDescriptorArgumentException('@Route.priority must contain an integer.');
409
        }
410
411 3
        return $params['priority'];
412
    }
413
}
414