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

Route::validateMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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\Attribute;
13
14
/**
15
 * Import classes
16
 */
17
use Attribute;
18
use Psr\Http\Server\MiddlewareInterface;
19
use Sunrise\Http\Router\Exception\InvalidDescriptorArgumentException;
20
use Sunrise\Http\Router\RouteDescriptorInterface;
21
22
/**
23
 * Attribute for a route description
24
 *
25
 * @since 2.6.0
26
 */
27
#[Attribute(Attribute::TARGET_CLASS)]
28 1
final class Route implements RouteDescriptorInterface
29
{
30
31
    /**
32
     * A route name
33
     *
34
     * @var string
35
     */
36
    private $name;
37
38
    /**
39
     * A route host
40
     *
41
     * @var null|string
42
     */
43
    private $host;
44
45
    /**
46
     * A route path
47
     *
48
     * @var string
49
     */
50
    private $path;
51
52
    /**
53
     * A route methods
54
     *
55
     * @var string[]
56
     */
57
    private $methods;
58
59
    /**
60
     * A route middlewares
61
     *
62
     * @var string[]
63
     */
64
    private $middlewares;
65
66
    /**
67
     * A route attributes
68
     *
69
     * @var array
70
     */
71
    private $attributes;
72
73
    /**
74
     * A route summary
75
     *
76
     * @var string
77
     */
78
    private $summary;
79
80
    /**
81
     * A route description
82
     *
83
     * @var string
84
     */
85
    private $description;
86
87
    /**
88
     * A route tags
89
     *
90
     * @var string[]
91
     */
92
    private $tags;
93
94
    /**
95
     * A route priority
96
     *
97
     * @var int
98
     */
99
    private $priority;
100
101
    /**
102
     * Constructor of the attribute
103
     *
104
     * @param  string       $name         A route name
105
     * @param  null|string  $host         A route host
106
     * @param  string       $path         A route path
107
     * @param  string[]     $methods      A route methods
108
     * @param  string[]     $middlewares  A route middlewares
109
     * @param  array        $attributes   A route attributes
110
     * @param  string       $summary      A route summary
111
     * @param  string       $description  A route description
112
     * @param  string[]     $tags         A route tags
113
     * @param  int          $priority     A route priority
114
     *
115
     * @throws InvalidDescriptorArgumentException
116
     */
117 40
    public function __construct(
118
        string $name,
119
        ?string $host = null,
120
        string $path,
121
        array $methods,
122
        array $middlewares = [],
123
        array $attributes = [],
124
        string $summary = '',
125
        string $description = '',
126
        array $tags = [],
127
        int $priority = 0
128
    ) {
129 40
        $this->name = $name;
130 40
        $this->host = $host;
131 40
        $this->path = $path;
132 40
        $this->methods = $methods;
133 40
        $this->middlewares = $middlewares;
134 40
        $this->attributes = $attributes;
135 40
        $this->summary = $summary;
136 40
        $this->description = $description;
137 40
        $this->tags = $tags;
138 40
        $this->priority = $priority;
139
140 40
        $this->validateName();
141 39
        $this->validateHost();
142 38
        $this->validatePath();
143 37
        $this->validateMethods();
144 26
        $this->validateMiddlewares();
145 14
        $this->validateTags();
146 4
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 4
    public function getName() : string
152
    {
153 4
        return $this->name;
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 4
    public function getHost() : ?string
160
    {
161 4
        return $this->host;
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167 4
    public function getPath() : string
168
    {
169 4
        return $this->path;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 4
    public function getMethods() : array
176
    {
177 4
        return $this->methods;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183 4
    public function getMiddlewares() : array
184
    {
185 4
        return $this->middlewares;
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 4
    public function getAttributes() : array
192
    {
193 4
        return $this->attributes;
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199 4
    public function getSummary() : string
200
    {
201 4
        return $this->summary;
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207 4
    public function getDescription() : string
208
    {
209 4
        return $this->description;
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215 4
    public function getTags() : array
216
    {
217 4
        return $this->tags;
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223 4
    public function getPriority() : int
224
    {
225 4
        return $this->priority;
226
    }
227
228
    /**
229
     * Throws an exception if the attribute contains an invalid route name
230
     *
231
     * @return void
232
     *
233
     * @throws InvalidDescriptorArgumentException
234
     *         If the attribute contains an invalid route name.
235
     */
236 40
    private function validateName() : void
237
    {
238 40
        InvalidDescriptorArgumentException::throwIfEmpty(
239 40
            $this->name,
240 40
            '#[Route.name] must contain a non-empty string.'
241
        );
242 39
    }
243
244
    /**
245
     * Throws an exception if the attribute contains an invalid route host
246
     *
247
     * @return void
248
     *
249
     * @throws InvalidDescriptorArgumentException
250
     *         If the attribute contains an invalid route host.
251
     */
252 39
    private function validateHost() : void
253
    {
254 39
        InvalidDescriptorArgumentException::throwIfEmpty(
255 39
            $this->host,
256 39
            '#[Route.host] must contain a non-empty string or null.'
257
        );
258 38
    }
259
260
    /**
261
     * Throws an exception if the attribute contains an invalid route path
262
     *
263
     * @return void
264
     *
265
     * @throws InvalidDescriptorArgumentException
266
     *         If the attribute contains an invalid route path.
267
     */
268 38
    private function validatePath() : void
269
    {
270 38
        InvalidDescriptorArgumentException::throwIfEmpty(
271 38
            $this->path,
272 38
            '#[Route.path] must contain a non-empty string.'
273
        );
274 37
    }
275
276
    /**
277
     * Throws an exception if the attribute contains an invalid route methods
278
     *
279
     * @return void
280
     *
281
     * @throws InvalidDescriptorArgumentException
282
     *         If the attribute contains an invalid route methods.
283
     */
284 37
    private function validateMethods() : void
285
    {
286 37
        InvalidDescriptorArgumentException::throwIfEmpty(
287 37
            $this->methods,
288 37
            '#[Route.methods] must contain at least one element.'
289
        );
290
291 36
        foreach ($this->methods as $method) {
292 36
            InvalidDescriptorArgumentException::throwIfEmptyString(
293 36
                $method,
294 36
                '#[Route.methods] must contain non-empty strings.'
295
            );
296
        }
297 26
    }
298
299
    /**
300
     * Throws an exception if the attribute contains an invalid route middlewares
301
     *
302
     * @return void
303
     *
304
     * @throws InvalidDescriptorArgumentException
305
     *         If the attribute contains an invalid route middlewares.
306
     */
307 26
    private function validateMiddlewares() : void
308
    {
309 26
        if ([] === $this->middlewares) {
310 13
            return;
311
        }
312
313 14
        foreach ($this->middlewares as $middleware) {
314 14
            InvalidDescriptorArgumentException::throwIfNotImplemented(
315 14
                $middleware,
316 14
                MiddlewareInterface::class,
317 14
                '#[Route.middlewares] must contain middlewares.'
318
            );
319
        }
320 2
    }
321
322
    /**
323
     * Throws an exception if the attribute contains an invalid route tags
324
     *
325
     * @return void
326
     *
327
     * @throws InvalidDescriptorArgumentException
328
     *         If the attribute contains an invalid route tags.
329
     */
330 14
    private function validateTags() : void
331
    {
332 14
        if ([] === $this->tags) {
333 3
            return;
334
        }
335
336 12
        foreach ($this->tags as $tag) {
337 12
            InvalidDescriptorArgumentException::throwIfEmptyString(
338 12
                $tag,
339 12
                '#[Route.tags] must contain non-empty strings.'
340
            );
341
        }
342 2
    }
343
}
344