Passed
Pull Request — master (#32)
by Anatoly
09:33
created

Route::assertValidMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
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\Annotation;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Server\MiddlewareInterface;
18
use InvalidArgumentException;
19
20
/**
21
 * Import functions
22
 */
23
use function class_exists;
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
31
 * @Target({"CLASS"})
32
 */
33
final class Route
34
{
35
36
    /**
37
     * @var object
38
     */
39
    public $source;
40
41
    /**
42
     * @var string
43
     */
44
    public $name;
45
46
    /**
47
     * @var string
48
     */
49
    public $path;
50
51
    /**
52
     * @var array
53
     */
54
    public $methods;
55
56
    /**
57
     * @var array
58
     */
59
    public $middlewares;
60
61
    /**
62
     * @var array
63
     */
64
    public $attributes;
65
66
    /**
67
     * @var int
68
     */
69
    public $priority;
70
71
    /**
72
     * @param array $params
73
     */
74 17
    public function __construct(array $params)
75
    {
76
        $params += [
77 17
            'middlewares' => [],
78
            'attributes' => [],
79
            'priority' => 0,
80
        ];
81
82 17
        $this->assertValidName($params);
83 14
        $this->assertValidPath($params);
84 11
        $this->assertValidMethods($params);
85 7
        $this->assertValidMiddlewares($params);
86 3
        $this->assertValidAttributes($params);
87 2
        $this->assertValidPriority($params);
88
89 1
        $this->name = $params['name'];
90 1
        $this->path = $params['path'];
91 1
        $this->methods = $params['methods'];
92 1
        $this->middlewares = $params['middlewares'];
93 1
        $this->attributes = $params['attributes'];
94 1
        $this->priority = $params['priority'];
95 1
    }
96
97
    /**
98
     * @param array $params
99
     * @return void
100
     * @throws InvalidArgumentException
101
     */
102 17
    private function assertValidName(array $params) : void
103
    {
104 17
        if (empty($params['name']) || !is_string($params['name'])) {
105 3
            throw new InvalidArgumentException('@Route.name must be not an empty string.');
106
        }
107 14
    }
108
109
    /**
110
     * @param array $params
111
     * @return void
112
     * @throws InvalidArgumentException
113
     */
114 14
    private function assertValidPath(array $params) : void
115
    {
116 14
        if (empty($params['path']) || !is_string($params['path'])) {
117 3
            throw new InvalidArgumentException('@Route.path must be not an empty string.');
118
        }
119 11
    }
120
121
    /**
122
     * @param array $params
123
     * @return void
124
     * @throws InvalidArgumentException
125
     */
126 11
    private function assertValidMethods(array $params) : void
127
    {
128 11
        if (empty($params['methods']) || !is_array($params['methods'])) {
129 3
            throw new InvalidArgumentException('@Route.methods must be not an empty array.');
130
        }
131
132 8
        foreach ($params['methods'] as $method) {
133 8
            $this->assertValidMethod($method);
134
        }
135 7
    }
136
137
    /**
138
     * @param array $params
139
     * @return void
140
     * @throws InvalidArgumentException
141
     */
142 7
    private function assertValidMiddlewares(array $params) : void
143
    {
144 7
        if (!is_array($params['middlewares'])) {
145 1
            throw new InvalidArgumentException('@Route.middlewares must be an array.');
146
        }
147
148 6
        foreach ($params['middlewares'] as $middleware) {
149 4
            $this->assertValidMiddleware($middleware);
150
        }
151 3
    }
152
153
    /**
154
     * @param array $params
155
     * @return void
156
     * @throws InvalidArgumentException
157
     */
158 3
    private function assertValidAttributes(array $params) : void
159
    {
160 3
        if (!is_array($params['attributes'])) {
161 1
            throw new InvalidArgumentException('@Route.attributes must be an array.');
162
        }
163 2
    }
164
165
    /**
166
     * @param array $params
167
     * @return void
168
     * @throws InvalidArgumentException
169
     */
170 2
    private function assertValidPriority(array $params) : void
171
    {
172 2
        if (!is_int($params['priority'])) {
173 1
            throw new InvalidArgumentException('@Route.priority must be an integer.');
174
        }
175 1
    }
176
177
    /**
178
     * @param mixed $method
179
     * @return void
180
     * @throws InvalidArgumentException
181
     */
182 8
    private function assertValidMethod($method) : void
183
    {
184 8
        if (!is_string($method)) {
185 1
            throw new InvalidArgumentException('@Route.methods must contain only strings.');
186
        }
187 8
    }
188
189
    /**
190
     * @param mixed $middleware
191
     * @return void
192
     * @throws InvalidArgumentException
193
     */
194 4
    private function assertValidMiddleware($middleware) : void
195
    {
196 4
        if (!is_string($middleware)) {
197 1
            throw new InvalidArgumentException('@Route.middlewares must contain only strings.');
198
        }
199
200 4
        if (!class_exists($middleware)) {
201 1
            throw new InvalidArgumentException('@Route.middlewares contains nonexistent class.');
202
        }
203
204 3
        if (!is_subclass_of($middleware, MiddlewareInterface::class)) {
205 1
            throw new InvalidArgumentException('@Route.middlewares contains non middleware class.');
206
        }
207 2
    }
208
}
209