Passed
Pull Request — master (#97)
by Dmitriy
07:54
created

Route::disableMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Yiisoft\Http\Method;
8
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
9
10
/**
11
 * Route defines a mapping from URL to callback / name and vice versa.
12
 */
13
final class Route
14
{
15
    private ?string $name = null;
16
    /** @var string[] */
17
    private array $methods;
18
    private string $pattern;
19
    private ?string $host = null;
20
    private bool $override = false;
21
    private ?MiddlewareDispatcher $dispatcher = null;
22
23
    private array $middlewareDefinitions = [];
24
    private array $disabledMiddlewareDefinitions = [];
25
    private array $defaults = [];
26
27 29
    private function __construct(?MiddlewareDispatcher $dispatcher = null)
28
    {
29 29
        $this->dispatcher = $dispatcher;
30 29
    }
31
32 5
    public function injectDispatcher(MiddlewareDispatcher $dispatcher): void
33
    {
34 5
        $this->dispatcher = $dispatcher;
35 5
    }
36
37 2
    public function withDispatcher(MiddlewareDispatcher $dispatcher): self
38
    {
39 2
        $route = clone $this;
40 2
        $route->dispatcher = $dispatcher;
41 2
        return $route;
42
    }
43
44 6
    public function getDispatcherWithMiddlewares(): MiddlewareDispatcher
45
    {
46 6
        if ($this->dispatcher->hasMiddlewares()) {
0 ignored issues
show
Bug introduced by
The method hasMiddlewares() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        if ($this->dispatcher->/** @scrutinizer ignore-call */ hasMiddlewares()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
            return $this->dispatcher;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dispatcher could return the type null which is incompatible with the type-hinted return Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher. Consider adding an additional type-check to rule them out.
Loading history...
48
        }
49
50 6
        foreach ($this->middlewareDefinitions as $index => $definition) {
51 6
            if (in_array($definition, $this->disabledMiddlewareDefinitions)) {
52
                unset($this->middlewareDefinitions[$index]);
53
            }
54
        }
55
56 6
        return $this->dispatcher = $this->dispatcher->withMiddlewares($this->middlewareDefinitions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->dispatcher...>middlewareDefinitions) could return the type null which is incompatible with the type-hinted return Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher. Consider adding an additional type-check to rule them out.
Loading history...
57
    }
58
59 10
    public function hasDispatcher(): bool
60
    {
61 10
        return $this->dispatcher !== null;
62
    }
63
64
    /**
65
     * @param string $pattern
66
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
67
     * @param MiddlewareDispatcher|null $dispatcher
68
     *
69
     * @return self
70
     */
71 19
    public static function get(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
72
    {
73 19
        return self::methods([Method::GET], $pattern, $middlewareDefinition, $dispatcher);
74
    }
75
76
    /**
77
     * @param string $pattern
78
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
79
     * @param MiddlewareDispatcher|null $dispatcher
80
     *
81
     * @return self
82
     */
83 4
    public static function post(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
84
    {
85 4
        return self::methods([Method::POST], $pattern, $middlewareDefinition, $dispatcher);
86
    }
87
88
    /**
89
     * @param string $pattern
90
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
91
     * @param MiddlewareDispatcher|null $dispatcher
92
     *
93
     * @return self
94
     */
95 1
    public static function put(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
96
    {
97 1
        return self::methods([Method::PUT], $pattern, $middlewareDefinition, $dispatcher);
98
    }
99
100
    /**
101
     * @param string $pattern
102
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
103
     * @param MiddlewareDispatcher|null $dispatcher
104
     *
105
     * @return self
106
     */
107 1
    public static function delete(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
108
    {
109 1
        return self::methods([Method::DELETE], $pattern, $middlewareDefinition, $dispatcher);
110
    }
111
112
    /**
113
     * @param string $pattern
114
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
115
     * @param MiddlewareDispatcher|null $dispatcher
116
     *
117
     * @return self
118
     */
119 1
    public static function patch(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
120
    {
121 1
        return self::methods([Method::PATCH], $pattern, $middlewareDefinition, $dispatcher);
122
    }
123
124
    /**
125
     * @param string $pattern
126
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
127
     * @param MiddlewareDispatcher|null $dispatcher
128
     *
129
     * @return self
130
     */
131 1
    public static function head(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
132
    {
133 1
        return self::methods([Method::HEAD], $pattern, $middlewareDefinition, $dispatcher);
134
    }
135
136
    /**
137
     * @param string $pattern
138
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
139
     * @param MiddlewareDispatcher|null $dispatcher
140
     *
141
     * @return self
142
     */
143 1
    public static function options(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
144
    {
145 1
        return self::methods([Method::OPTIONS], $pattern, $middlewareDefinition, $dispatcher);
146
    }
147
148
    /**
149
     * @param array $methods
150
     * @param string $pattern
151
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
152
     * @param MiddlewareDispatcher|null $dispatcher
153
     *
154
     * @return self
155
     */
156 29
    public static function methods(
157
        array $methods,
158
        string $pattern,
159
        $middlewareDefinition = null,
160
        ?MiddlewareDispatcher $dispatcher = null
161
    ): self
162
    {
163 29
        $route = new self($dispatcher);
164 29
        $route->methods = $methods;
165 29
        $route->pattern = $pattern;
166 29
        if ($middlewareDefinition !== null) {
167 1
            $route->middlewareDefinitions[] = $middlewareDefinition;
168
        }
169 29
        return $route;
170
    }
171
172
    /**
173
     * @param string $pattern
174
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
175
     * @param MiddlewareDispatcher|null $dispatcher
176
     *
177
     * @return self
178
     */
179 1
    public static function anyMethod(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
180
    {
181 1
        return self::methods(Method::ALL, $pattern, $middlewareDefinition, $dispatcher);
182
    }
183
184 8
    public function name(string $name): self
185
    {
186 8
        $route = clone $this;
187 8
        $route->name = $name;
188 8
        return $route;
189
    }
190
191 6
    public function pattern(string $pattern): self
192
    {
193 6
        $new = clone $this;
194 6
        $new->pattern = $pattern;
195 6
        return $new;
196
    }
197
198 2
    public function host(string $host): self
199
    {
200 2
        $route = clone $this;
201 2
        $route->host = rtrim($host, '/');
202 2
        return $route;
203
    }
204
205
    /**
206
     * Marks route as override. When added it will replace existing route with the same name.
207
     *
208
     * @return self
209
     */
210 1
    public function override(): self
211
    {
212 1
        $route = clone $this;
213 1
        $route->override = true;
214 1
        return $route;
215
    }
216
217
    /**
218
     * Parameter default values indexed by parameter names.
219
     *
220
     * @param array $defaults
221
     *
222
     * @return self
223
     */
224 1
    public function defaults(array $defaults): self
225
    {
226 1
        $route = clone $this;
227 1
        $route->defaults = $defaults;
228 1
        return $route;
229
    }
230
231
    /**
232
     * Adds a handler middleware definition that should be invoked for a matched route.
233
     * Last added handler will be executed first.
234
     *
235
     * @param $middlewareDefinition mixed
236
     * @return self
237
     */
238 5
    public function addMiddleware($middlewareDefinition): self
239
    {
240 5
        $route = clone $this;
241 5
        $route->middlewareDefinitions[] = $middlewareDefinition;
242 5
        return $route;
243
    }
244
245
    public function disableMiddleware($middlewareDefinition): self
246
    {
247
        $route = clone $this;
248
        $route->disabledMiddlewareDefinitions[] = $middlewareDefinition;
249
        return $route;
250
    }
251
252 2
    public function __toString(): string
253
    {
254 2
        $result = '';
255
256 2
        if ($this->name !== null) {
257 1
            $result .= '[' . $this->name . '] ';
258
        }
259
260 2
        if ($this->methods !== []) {
261 2
            $result .= implode(',', $this->methods) . ' ';
262
        }
263 2
        if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
264 1
            $result .= $this->host;
265
        }
266 2
        $result .= $this->pattern;
267
268 2
        return $result;
269
    }
270
271 7
    public function getName(): string
272
    {
273 7
        return $this->name ?? (implode(', ', $this->methods) . ' ' . $this->host . $this->pattern);
274
    }
275
276 9
    public function getMethods(): array
277
    {
278 9
        return $this->methods;
279
    }
280
281 6
    public function getPattern(): string
282
    {
283 6
        return $this->pattern;
284
    }
285
286 1
    public function getHost(): ?string
287
    {
288 1
        return $this->host;
289
    }
290
291 2
    public function isOverride(): bool
292
    {
293 2
        return $this->override;
294
    }
295
296 1
    public function getDefaults(): array
297
    {
298 1
        return $this->defaults;
299
    }
300
}
301