Passed
Pull Request — master (#68)
by Dmitriy
15:33
created

Route::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Yiisoft\Http\Method;
8
9
/**
10
 * Route defines a mapping from URL to callback / name and vice versa
11
 */
12
final class Route
13
{
14
    private ?string $name = null;
15
    /** @var string[] */
16
    private array $methods;
17
    private string $pattern;
18
    private ?string $host = null;
19
    private ?MiddlewareDispatcher $dispatcher = null;
20
21
    /**
22
     * @var callable[]|string[]|array[]
23
     */
24
    private array $middlewareDefinitions = [];
25
    private array $defaults = [];
26
27
    private function __construct(?MiddlewareDispatcher $dispatcher = null)
28
    {
29
        $this->dispatcher = $dispatcher;
30
    }
31
32
    public function injectDispatcher(MiddlewareDispatcher $dispatcher): void
33
    {
34
        $this->dispatcher = $dispatcher;
35
    }
36
37
    public function getDispatcherWithMiddlewares(): MiddlewareDispatcher
38 44
    {
39
        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

39
        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...
40 44
            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\Router\MiddlewareDispatcher. Consider adding an additional type-check to rule them out.
Loading history...
41 44
        }
42
43 7
        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\Router\MiddlewareDispatcher. Consider adding an additional type-check to rule them out.
Loading history...
44
    }
45 7
46 7
    public function hasDispatcher(): bool
47 7
    {
48
        return $this->dispatcher !== null;
49
    }
50 10
51
    /**
52 10
     * @param string $pattern
53
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
54
     * @param MiddlewareDispatcher|null $dispatcher
55
     * @return self
56
     */
57
    public static function get(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
58
    {
59
        return self::methods([Method::GET], $pattern, $middlewareDefinition, $dispatcher);
60
    }
61 34
62
    /**
63 34
     * @param string $pattern
64
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
65
     * @param MiddlewareDispatcher|null $dispatcher
66
     * @return self
67
     */
68
    public static function post(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
69
    {
70
        return self::methods([Method::POST], $pattern, $middlewareDefinition, $dispatcher);
71
    }
72 4
73
    /**
74 4
     * @param string $pattern
75
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
76
     * @param MiddlewareDispatcher|null $dispatcher
77
     * @return self
78
     */
79
    public static function put(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
80
    {
81
        return self::methods([Method::PUT], $pattern, $middlewareDefinition, $dispatcher);
82
    }
83 1
84
    /**
85 1
     * @param string $pattern
86
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
87
     * @param MiddlewareDispatcher|null $dispatcher
88
     * @return self
89
     */
90
    public static function delete(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
91
    {
92
        return self::methods([Method::DELETE], $pattern, $middlewareDefinition, $dispatcher);
93
    }
94 1
95
    /**
96 1
     * @param string $pattern
97
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
98
     * @param MiddlewareDispatcher|null $dispatcher
99
     * @return self
100
     */
101
    public static function patch(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
102
    {
103
        return self::methods([Method::PATCH], $pattern, $middlewareDefinition, $dispatcher);
104
    }
105 1
106
    /**
107 1
     * @param string $pattern
108
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
109
     * @param MiddlewareDispatcher|null $dispatcher
110
     * @return self
111
     */
112
    public static function head(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
113
    {
114
        return self::methods([Method::HEAD], $pattern, $middlewareDefinition, $dispatcher);
115
    }
116 1
117
    /**
118 1
     * @param string $pattern
119
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
120
     * @param MiddlewareDispatcher|null $dispatcher
121
     * @return self
122
     */
123
    public static function options(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
124
    {
125
        return self::methods([Method::OPTIONS], $pattern, $middlewareDefinition, $dispatcher);
126
    }
127 1
128
    /**
129 1
     * @param array $methods
130
     * @param string $pattern
131
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
132
     * @param MiddlewareDispatcher|null $dispatcher
133
     * @return self
134
     */
135
    public static function methods(
136
        array $methods,
137
        string $pattern,
138
        $middlewareDefinition = null,
139 44
        ?MiddlewareDispatcher $dispatcher = null
140
    ): self {
141
        $route = new self($dispatcher);
142
        $route->methods = $methods;
143
        $route->pattern = $pattern;
144
        if ($middlewareDefinition !== null) {
145 44
            $route->middlewareDefinitions[] = $middlewareDefinition;
146 44
        }
147 44
        return $route;
148 44
    }
149 10
150 4
    /**
151
     * @param string $pattern
152 38
     * @param callable|string|array|null $middlewareDefinition primary route handler {@see addMiddleware()}
153
     * @param MiddlewareDispatcher|null $dispatcher
154
     * @return self
155
     */
156
    public static function anyMethod(string $pattern, $middlewareDefinition = null, ?MiddlewareDispatcher $dispatcher = null): self
157
    {
158
        return self::methods(Method::ANY, $pattern, $middlewareDefinition, $dispatcher);
159
    }
160
161 1
    public function name(string $name): self
162
    {
163 1
        $route = clone $this;
164
        $route->name = $name;
165
        return $route;
166 8
    }
167
168 8
    public function pattern(string $pattern): self
169 8
    {
170 8
        $new = clone $this;
171
        $new->pattern = $pattern;
172
        return $new;
173 4
    }
174
175 4
    public function host(string $host): self
176 4
    {
177 4
        $route = clone $this;
178
        $route->host = rtrim($host, '/');
179
        return $route;
180 2
    }
181
182 2
    /**
183 2
     * Parameter default values indexed by parameter names
184 2
     *
185
     * @param array $defaults
186
     * @return self
187
     */
188
    public function defaults(array $defaults): self
189
    {
190
        $route = clone $this;
191
        $route->defaults = $defaults;
192
        return $route;
193 1
    }
194
195 1
    /**
196 1
     * Prepends a handler that should be invoked for a matching route.
197 1
     * Last added handler will be invoked first.
198
     *
199
     * Parameter can be a PSR middleware class name, handler action
200
     * (an array of [handlerClass, handlerMethod]) or a callable.
201
     *
202
     * For handler action and callable typed parameters are automatically injected using dependency
203 21
     * injection container passed to the route. Current request and handler could be obtained by
204
     * type-hinting for ServerRequestInterface and RequestHandlerInterface.
205
     *
206 21
     * @param callable|string|array $middlewareDefinition
207
     * @return Route
208 1
     */
209
    public function addMiddleware($middlewareDefinition): self
210
    {
211 20
        $route = clone $this;
212 13
        $route->middlewareDefinitions[] = $middlewareDefinition;
213
        return $route;
214
    }
215 7
216
    public function __toString(): string
217
    {
218
        $result = '';
219
220
        if ($this->name !== null) {
221
            $result .= '[' . $this->name . '] ';
222 12
        }
223
224 12
        if ($this->methods !== []) {
225
            $result .= implode(',', $this->methods) . ' ';
226
        }
227
        if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
228
            $result .= $this->host;
229
        }
230
        $result .= $this->pattern;
231 12
232 3
        return $result;
233
    }
234
235 3
    public function getName(): string
236
    {
237
        return $this->name ?? (implode(', ', $this->methods) . ' ' . $this->host . $this->pattern);
238 9
    }
239 9
240
    public function getMethods(): array
241
    {
242 9
        return $this->methods;
243
    }
244
245
    public function getPattern(): string
246
    {
247
        return $this->pattern;
248 20
    }
249
250 20
    public function getHost(): ?string
251 13
    {
252
        return $this->host;
253
    }
254 7
255
    public function getDefaults(): array
256
    {
257
        return $this->defaults;
258
    }
259
}
260