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

Route::assertParamsContainValidDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
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\Annotation;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Server\MiddlewareInterface;
18
use Psr\Http\Server\RequestHandlerInterface;
19
use Sunrise\Http\Router\Exception\InvalidAnnotationParameterException;
20
use Sunrise\Http\Router\Exception\InvalidAnnotationSourceException;
21
22
/**
23
 * Import functions
24
 */
25
use function is_array;
26
use function is_int;
27
use function is_string;
28
use function is_subclass_of;
29
30
/**
31
 * @Annotation
32
 *
33
 * @Target({"CLASS"})
34
 */
35
final class Route
36
{
37
38
    /**
39
     * @var string
40
     */
41
    public $name;
42
43
    /**
44
     * @var string
45
     */
46
    public $path;
47
48
    /**
49
     * @var array
50
     */
51
    public $methods;
52
53
    /**
54
     * @var array
55
     */
56
    public $middlewares;
57
58
    /**
59
     * @var array
60
     */
61
    public $attributes;
62
63
    /**
64
     * @var string
65
     *
66
     * @since 2.4.0
67
     */
68
    public $summary;
69
70
    /**
71
     * @var string
72
     *
73
     * @since 2.4.0
74
     */
75
    public $description;
76
77
    /**
78
     * @var array
79
     *
80
     * @since 2.4.0
81
     */
82
    public $tags;
83
84
    /**
85
     * @var int
86
     */
87
    public $priority;
88
89
    /**
90
     * @param array $params
91
     */
92 139
    public function __construct(array $params)
93
    {
94
        $params += [
95 139
            'middlewares' => [],
96
            'attributes' => [],
97
            'summary' => '',
98
            'description' => '',
99
            'tags' => [],
100
            'priority' => 0,
101
        ];
102
103 139
        $this->assertParamsContainValidName($params);
104 125
        $this->assertParamsContainValidPath($params);
105 111
        $this->assertParamsContainValidMethods($params);
106 87
        $this->assertParamsContainValidMiddlewares($params);
107 63
        $this->assertParamsContainValidAttributes($params);
108 53
        $this->assertParamsContainValidSummary($params);
109 44
        $this->assertParamsContainValidDescription($params);
110 35
        $this->assertParamsContainValidTags($params);
111 17
        $this->assertParamsContainValidPriority($params);
112
113 7
        $this->name = $params['name'];
114 7
        $this->path = $params['path'];
115 7
        $this->methods = $params['methods'];
116 7
        $this->middlewares = $params['middlewares'];
117 7
        $this->attributes = $params['attributes'];
118 7
        $this->summary = $params['summary'];
119 7
        $this->description = $params['description'];
120 7
        $this->tags = $params['tags'];
121 7
        $this->priority = $params['priority'];
122 7
    }
123
124
    /**
125
     * @param string $source
126
     *
127
     * @return void
128
     *
129
     * @throws InvalidAnnotationSourceException
130
     */
131 5
    public static function assertValidSource(string $source) : void
132
    {
133 5
        if (!is_subclass_of($source, RequestHandlerInterface::class)) {
134 2
            throw new InvalidAnnotationSourceException(
135 2
                sprintf('@Route annotation source %s is not a request handler.', $source)
136
            );
137
        }
138 4
    }
139
140
    /**
141
     * @param array $params
142
     *
143
     * @return void
144
     *
145
     * @throws InvalidAnnotationParameterException
146
     */
147 139
    private function assertParamsContainValidName(array $params) : void
148
    {
149 139
        if (empty($params['name']) || !is_string($params['name'])) {
150 14
            throw new InvalidAnnotationParameterException(
151 14
                '@Route.name must be not an empty string.'
152
            );
153
        }
154 125
    }
155
156
    /**
157
     * @param array $params
158
     *
159
     * @return void
160
     *
161
     * @throws InvalidAnnotationParameterException
162
     */
163 125
    private function assertParamsContainValidPath(array $params) : void
164
    {
165 125
        if (empty($params['path']) || !is_string($params['path'])) {
166 14
            throw new InvalidAnnotationParameterException(
167 14
                '@Route.path must be not an empty string.'
168
            );
169
        }
170 111
    }
171
172
    /**
173
     * @param array $params
174
     *
175
     * @return void
176
     *
177
     * @throws InvalidAnnotationParameterException
178
     */
179 111
    private function assertParamsContainValidMethods(array $params) : void
180
    {
181 111
        if (empty($params['methods']) || !is_array($params['methods'])) {
182 14
            throw new InvalidAnnotationParameterException(
183 14
                '@Route.methods must be not an empty array.'
184
            );
185
        }
186
187 97
        foreach ($params['methods'] as $method) {
188 97
            if (!is_string($method)) {
189 10
                throw new InvalidAnnotationParameterException(
190 97
                    '@Route.methods must contain only strings.'
191
                );
192
            }
193
        }
194 87
    }
195
196
    /**
197
     * @param array $params
198
     *
199
     * @return void
200
     *
201
     * @throws InvalidAnnotationParameterException
202
     */
203 87
    private function assertParamsContainValidMiddlewares(array $params) : void
204
    {
205 87
        if (!is_array($params['middlewares'])) {
206 10
            throw new InvalidAnnotationParameterException(
207 10
                '@Route.middlewares must be an array.'
208
            );
209
        }
210
211 77
        foreach ($params['middlewares'] as $middleware) {
212 18
            if (!is_string($middleware)) {
213 10
                throw new InvalidAnnotationParameterException(
214 10
                    '@Route.middlewares must contain only strings.'
215
                );
216
            }
217
218 9
            if (!is_subclass_of($middleware, MiddlewareInterface::class)) {
219 4
                throw new InvalidAnnotationParameterException(
220 9
                    '@Route.middlewares contains a nonexistent or non-middleware class.'
221
                );
222
            }
223
        }
224 63
    }
225
226
    /**
227
     * @param array $params
228
     *
229
     * @return void
230
     *
231
     * @throws InvalidAnnotationParameterException
232
     */
233 63
    private function assertParamsContainValidAttributes(array $params) : void
234
    {
235 63
        if (!is_array($params['attributes'])) {
236 10
            throw new InvalidAnnotationParameterException(
237 10
                '@Route.attributes must be an array.'
238
            );
239
        }
240 53
    }
241
242
    /**
243
     * @param array $params
244
     *
245
     * @return void
246
     *
247
     * @throws InvalidAnnotationParameterException
248
     */
249 53
    private function assertParamsContainValidSummary(array $params) : void
250
    {
251 53
        if (!is_string($params['summary'])) {
252 9
            throw new InvalidAnnotationParameterException(
253 9
                '@Route.summary must be a string.'
254
            );
255
        }
256 44
    }
257
258
    /**
259
     * @param array $params
260
     *
261
     * @return void
262
     *
263
     * @throws InvalidAnnotationParameterException
264
     */
265 44
    private function assertParamsContainValidDescription(array $params) : void
266
    {
267 44
        if (!is_string($params['description'])) {
268 9
            throw new InvalidAnnotationParameterException(
269 9
                '@Route.description must be a string.'
270
            );
271
        }
272 35
    }
273
274
    /**
275
     * @param array $params
276
     *
277
     * @return void
278
     *
279
     * @throws InvalidAnnotationParameterException
280
     */
281 35
    private function assertParamsContainValidTags(array $params) : void
282
    {
283 35
        if (!is_array($params['tags'])) {
284 9
            throw new InvalidAnnotationParameterException(
285 9
                '@Route.tags must be an array.'
286
            );
287
        }
288
289 26
        foreach ($params['tags'] as $middleware) {
290 12
            if (!is_string($middleware)) {
291 9
                throw new InvalidAnnotationParameterException(
292 12
                    '@Route.tags must contain only strings.'
293
                );
294
            }
295
        }
296 17
    }
297
298
    /**
299
     * @param array $params
300
     *
301
     * @return void
302
     *
303
     * @throws InvalidAnnotationParameterException
304
     */
305 17
    private function assertParamsContainValidPriority(array $params) : void
306
    {
307 17
        if (!is_int($params['priority'])) {
308 10
            throw new InvalidAnnotationParameterException(
309 10
                '@Route.priority must be an integer.'
310
            );
311
        }
312 7
    }
313
}
314