Passed
Push — master ( a5fb1c...659b35 )
by Anatoly
01:04 queued 10s
created

Route::assertValidName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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->assertValidName();
141 39
        $this->assertValidHost();
142 38
        $this->assertValidPath();
143 37
        $this->assertValidMethods();
144 26
        $this->assertValidMiddlewares();
145 14
        $this->assertValidTags();
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 invalid a route name
230
     *
231
     * @return void
232
     *
233
     * @throws InvalidDescriptorArgumentException
234
     */
235 40
    private function assertValidName() : void
236
    {
237 40
        InvalidDescriptorArgumentException::assertIsNotEmpty(
238 40
            $this->name,
239 40
            '#[Route.name] must contain a non-empty string.'
240
        );
241 39
    }
242
243
    /**
244
     * Throws an exception if the attribute contains invalid a route host
245
     *
246
     * @return void
247
     *
248
     * @throws InvalidDescriptorArgumentException
249
     */
250 39
    private function assertValidHost() : void
251
    {
252 39
        InvalidDescriptorArgumentException::assertIsNotEmpty(
253 39
            $this->host,
254 39
            '#[Route.host] must contain a non-empty string or null.'
255
        );
256 38
    }
257
258
    /**
259
     * Throws an exception if the attribute contains invalid a route path
260
     *
261
     * @return void
262
     *
263
     * @throws InvalidDescriptorArgumentException
264
     */
265 38
    private function assertValidPath() : void
266
    {
267 38
        InvalidDescriptorArgumentException::assertIsNotEmpty(
268 38
            $this->path,
269 38
            '#[Route.path] must contain a non-empty string.'
270
        );
271 37
    }
272
273
    /**
274
     * Throws an exception if the attribute contains invalid a route methods
275
     *
276
     * @return void
277
     *
278
     * @throws InvalidDescriptorArgumentException
279
     */
280 37
    private function assertValidMethods() : void
281
    {
282 37
        InvalidDescriptorArgumentException::assertIsNotEmpty(
283 37
            $this->methods,
284 37
            '#[Route.methods] must contain at least one element.'
285
        );
286
287 36
        foreach ($this->methods as $value) {
288 36
            InvalidDescriptorArgumentException::assertIsNotEmptyString(
289 36
                $value,
290 36
                '#[Route.methods] must contain non-empty strings.'
291
            );
292
        }
293 26
    }
294
295
    /**
296
     * Throws an exception if the attribute contains invalid a route middlewares
297
     *
298
     * @return void
299
     *
300
     * @throws InvalidDescriptorArgumentException
301
     */
302 26
    private function assertValidMiddlewares() : void
303
    {
304 26
        if ([] === $this->middlewares) {
305 13
            return;
306
        }
307
308 14
        foreach ($this->middlewares as $value) {
309 14
            InvalidDescriptorArgumentException::assertIsSubclassOf(
310 14
                $value,
311 14
                MiddlewareInterface::class,
312 14
                '#[Route.middlewares] must contain the class names of existing middlewares.'
313
            );
314
        }
315 2
    }
316
317
    /**
318
     * Throws an exception if the attribute contains invalid a route tags
319
     *
320
     * @return void
321
     *
322
     * @throws InvalidDescriptorArgumentException
323
     */
324 14
    private function assertValidTags() : void
325
    {
326 14
        if ([] === $this->tags) {
327 3
            return;
328
        }
329
330 12
        foreach ($this->tags as $value) {
331 12
            InvalidDescriptorArgumentException::assertIsNotEmptyString(
332 12
                $value,
333 12
                '#[Route.tags] must contain non-empty strings.'
334
            );
335
        }
336 2
    }
337
}
338