Passed
Push — master ( 8c1354...374c5c )
by Alexander
01:49
created

Route::patch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 2
b 0
f 0
eloc 1
nc 1
nop 3
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
    /**
24
     * @var array[]|callable[]|string[]
25
     */
26
    private array $middlewareDefinitions = [];
27
    private array $defaults = [];
28
29 29
    private function __construct(?MiddlewareDispatcher $dispatcher = null)
30
    {
31 29
        $this->dispatcher = $dispatcher;
32 29
    }
33
34 5
    public function injectDispatcher(MiddlewareDispatcher $dispatcher): void
35
    {
36 5
        $this->dispatcher = $dispatcher;
37 5
    }
38
39 2
    public function withDispatcher(MiddlewareDispatcher $dispatcher): self
40
    {
41 2
        $route = clone $this;
42 2
        $route->dispatcher = $dispatcher;
43 2
        return $route;
44
    }
45
46 6
    public function getDispatcherWithMiddlewares(): MiddlewareDispatcher
47
    {
48 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

48
        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...
49
            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...
50
        }
51
52 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...
53
    }
54
55 10
    public function hasDispatcher(): bool
56
    {
57 10
        return $this->dispatcher !== null;
58
    }
59
60
    /**
61
     * @param string $pattern
62
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
63
     * @param MiddlewareDispatcher|null $dispatcher
64
     *
65
     * @return self
66
     */
67 19
    public static function get(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
68
    {
69 19
        return self::methods([Method::GET], $pattern, $middlewareDefinition, $dispatcher);
70
    }
71
72
    /**
73
     * @param string $pattern
74
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
75
     * @param MiddlewareDispatcher|null $dispatcher
76
     *
77
     * @return self
78
     */
79 4
    public static function post(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
80
    {
81 4
        return self::methods([Method::POST], $pattern, $middlewareDefinition, $dispatcher);
82
    }
83
84
    /**
85
     * @param string $pattern
86
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
87
     * @param MiddlewareDispatcher|null $dispatcher
88
     *
89
     * @return self
90
     */
91 1
    public static function put(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
92
    {
93 1
        return self::methods([Method::PUT], $pattern, $middlewareDefinition, $dispatcher);
94
    }
95
96
    /**
97
     * @param string $pattern
98
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
99
     * @param MiddlewareDispatcher|null $dispatcher
100
     *
101
     * @return self
102
     */
103 1
    public static function delete(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
104
    {
105 1
        return self::methods([Method::DELETE], $pattern, $middlewareDefinition, $dispatcher);
106
    }
107
108
    /**
109
     * @param string $pattern
110
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
111
     * @param MiddlewareDispatcher|null $dispatcher
112
     *
113
     * @return self
114
     */
115 1
    public static function patch(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
116
    {
117 1
        return self::methods([Method::PATCH], $pattern, $middlewareDefinition, $dispatcher);
118
    }
119
120
    /**
121
     * @param string $pattern
122
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
123
     * @param MiddlewareDispatcher|null $dispatcher
124
     *
125
     * @return self
126
     */
127 1
    public static function head(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
128
    {
129 1
        return self::methods([Method::HEAD], $pattern, $middlewareDefinition, $dispatcher);
130
    }
131
132
    /**
133
     * @param string $pattern
134
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
135
     * @param MiddlewareDispatcher|null $dispatcher
136
     *
137
     * @return self
138
     */
139 1
    public static function options(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
140
    {
141 1
        return self::methods([Method::OPTIONS], $pattern, $middlewareDefinition, $dispatcher);
142
    }
143
144
    /**
145
     * @param array $methods
146
     * @param string $pattern
147
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
148
     * @param MiddlewareDispatcher|null $dispatcher
149
     *
150
     * @return self
151
     */
152 29
    public static function methods(
153
        array $methods,
154
        string $pattern,
155
        $middlewareDefinition = null,
156
        ?MiddlewareDispatcher $dispatcher = null
157
    ): self {
158 29
        $route = new self($dispatcher);
159 29
        $route->methods = $methods;
160 29
        $route->pattern = $pattern;
161 29
        if ($middlewareDefinition !== null) {
162 1
            $route->middlewareDefinitions[] = $middlewareDefinition;
163
        }
164 29
        return $route;
165
    }
166
167
    /**
168
     * @param string $pattern
169
     * @param array|callable|string|null $middlewareDefinition primary route handler {@see addMiddleware()}
170
     * @param MiddlewareDispatcher|null $dispatcher
171
     *
172
     * @return self
173
     */
174 1
    public static function anyMethod(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
175
    {
176 1
        return self::methods(Method::ANY, $pattern, $middlewareDefinition, $dispatcher);
0 ignored issues
show
Deprecated Code introduced by
The constant Yiisoft\Http\Method::ANY has been deprecated: Use {@see Method::ALL} instead. ( Ignorable by Annotation )

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

176
        return self::methods(/** @scrutinizer ignore-deprecated */ Method::ANY, $pattern, $middlewareDefinition, $dispatcher);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
177
    }
178
179 8
    public function name(string $name): self
180
    {
181 8
        $route = clone $this;
182 8
        $route->name = $name;
183 8
        return $route;
184
    }
185
186 6
    public function pattern(string $pattern): self
187
    {
188 6
        $new = clone $this;
189 6
        $new->pattern = $pattern;
190 6
        return $new;
191
    }
192
193 2
    public function host(string $host): self
194
    {
195 2
        $route = clone $this;
196 2
        $route->host = rtrim($host, '/');
197 2
        return $route;
198
    }
199
200 1
    public function override(): self
201
    {
202 1
        $route = clone $this;
203 1
        $route->override = true;
204 1
        return $route;
205
    }
206
207
    /**
208
     * Parameter default values indexed by parameter names
209
     *
210
     * @param array $defaults
211
     *
212
     * @return self
213
     */
214 1
    public function defaults(array $defaults): self
215
    {
216 1
        $route = clone $this;
217 1
        $route->defaults = $defaults;
218 1
        return $route;
219
    }
220
221
    /**
222
     * Adds a handler middleware that should be invoked for a matched route.
223
     * Last added handler will be executed first.
224
     *
225
     * @param array|callable|string $middlewareDefinition A PSR-15 middleware class name, handler action
226
     * (an array of [handlerClass, handlerMethod]) or a callable with
227
     * `function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface` signature.
228
     * For handler action and callable typed parameters are automatically injected using dependency
229
     * injection container passed to the route. Current request and handler could be obtained by
230
     * type-hinting for {@see ServerRequestInterface} and {@see RequestHandlerInterface}.
231
     *
232
     * @return self
233
     */
234 5
    public function addMiddleware($middlewareDefinition): self
235
    {
236 5
        $route = clone $this;
237 5
        $route->middlewareDefinitions[] = $middlewareDefinition;
238 5
        return $route;
239
    }
240
241 2
    public function __toString(): string
242
    {
243 2
        $result = '';
244
245 2
        if ($this->name !== null) {
246 1
            $result .= '[' . $this->name . '] ';
247
        }
248
249 2
        if ($this->methods !== []) {
250 2
            $result .= implode(',', $this->methods) . ' ';
251
        }
252 2
        if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
253 1
            $result .= $this->host;
254
        }
255 2
        $result .= $this->pattern;
256
257 2
        return $result;
258
    }
259
260 7
    public function getName(): string
261
    {
262 7
        return $this->name ?? (implode(', ', $this->methods) . ' ' . $this->host . $this->pattern);
263
    }
264
265 9
    public function getMethods(): array
266
    {
267 9
        return $this->methods;
268
    }
269
270 6
    public function getPattern(): string
271
    {
272 6
        return $this->pattern;
273
    }
274
275 1
    public function getHost(): ?string
276
    {
277 1
        return $this->host;
278
    }
279
280 2
    public function isOverride(): bool
281
282
    {
283 2
        return $this->override;
284
    }
285
286 1
    public function getDefaults(): array
287
    {
288 1
        return $this->defaults;
289
    }
290
}
291