Passed
Push — master ( 72a17c...7e1b76 )
by Alexander
04:45
created

Route::methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Yiisoft\Router;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
10
/**
11
 * Route defines a mapping from URL to callback / name and vice versa
12
 */
13
class Route implements MiddlewareInterface
14
{
15
    /**
16
     * @var string
17
     */
18
    private $name;
19
20
    /**
21
     * @var array
22
     */
23
    private $methods;
24
25
    /**
26
     * @var string
27
     */
28
    private $pattern;
29
30
    /**
31
     * @var string
32
     */
33
    private $host;
34
35
    /**
36
     * @var MiddlewareInterface
37
     */
38
    private $middleware;
39
40
    /**
41
     * @var array
42
     */
43
    private $parameters = [];
44
45
    /**
46
     * @var array
47
     */
48
    private $defaults = [];
49
50
    private function __construct()
51
    {
52
    }
53
54
    public static function get(string $pattern): self
55
    {
56
        $route = new static();
57
        $route->methods = [Method::GET];
58
        $route->pattern = $pattern;
59
        return $route;
60
    }
61
62
    public static function post(string $pattern): self
63
    {
64
        $route = new static();
65
        $route->methods = [Method::POST];
66
        $route->pattern = $pattern;
67
        return $route;
68
    }
69
70
    public static function put(string $pattern): self
71
    {
72
        $route = new static();
73
        $route->methods = [Method::PUT];
74
        $route->pattern = $pattern;
75
        return $route;
76
    }
77
78
    public static function delete(string $pattern): self
79
    {
80
        $route = new static();
81
        $route->methods = [Method::DELETE];
82
        $route->pattern = $pattern;
83
        return $route;
84
    }
85
86
    public static function patch(string $pattern): self
87
    {
88
        $route = new static();
89
        $route->methods = [Method::PATCH];
90
        $route->pattern = $pattern;
91
        return $route;
92
    }
93
94
    public static function head(string $pattern): self
95
    {
96
        $route = new static();
97
        $route->methods = [Method::HEAD];
98
        $route->pattern = $pattern;
99
        return $route;
100
    }
101
102
    public static function options(string $pattern): self
103
    {
104
        $route = new static();
105
        $route->methods = [Method::OPTIONS];
106
        $route->pattern = $pattern;
107
        return $route;
108
    }
109
110
    public static function methods(array $methods, string $pattern): self
111
    {
112
        $route = new static();
113
        $route->methods = $methods;
114
        $route->pattern = $pattern;
115
        return $route;
116
    }
117
118
    public static function anyMethod(string $pattern): self
119
    {
120
        $route = new static();
121
        $route->methods = Method::ANY;
122
        $route->pattern = $pattern;
123
        return $route;
124
    }
125
126
    public function name(string $name): self
127
    {
128
        $route = clone $this;
129
        $route->name = $name;
130
        return $route;
131
    }
132
133
    public function host(string $host): self
134
    {
135
        $route = clone $this;
136
        $route->host = rtrim($host, '/');
137
        return $route;
138
    }
139
140
    /**
141
     * Parameter validation rules indexed by parameter names
142
     *
143
     * @param array $parameters
144
     * @return Route
145
     */
146
    public function parameters(array $parameters): self
147
    {
148
        $route = clone $this;
149
        $route->parameters = $parameters;
150
        return $route;
151
    }
152
153
    /**
154
     * Parameter default values indexed by parameter names
155
     *
156
     * @param array $defaults
157
     * @return Route
158
     */
159
    public function defaults(array $defaults): self
160
    {
161
        $route = clone $this;
162
        $route->defaults = $defaults;
163
        return $route;
164
    }
165
166
    /**
167
     * Speicifes a handler that should be invoked for a matching route.
168
     * It can be either a PSR middleware or a callable with the following signature:
169
     *
170
     * ```
171
     * function (ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {
172
     * ```
173
     *
174
     * @param MiddlewareInterface|callable $middleware
175
     * @return Route
176
     */
177
    public function to($middleware): self
178
    {
179
        $route = clone $this;
180
        if (\is_callable($middleware)) {
181
            $middleware = $this->wrapCallable($middleware);
0 ignored issues
show
Bug introduced by
It seems like $middleware can also be of type Psr\Http\Server\MiddlewareInterface; however, parameter $callback of Yiisoft\Router\Route::wrapCallable() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

181
            $middleware = $this->wrapCallable(/** @scrutinizer ignore-type */ $middleware);
Loading history...
182
        }
183
184
        if (!$middleware instanceof MiddlewareInterface) {
185
            throw new \InvalidArgumentException('Parameter should be either a PSR middleware or a callable.');
186
        }
187
188
        $route->middleware = $middleware;
189
        return $route;
190
    }
191
192 1
    private function wrapCallable(callable $callback): MiddlewareInterface
193
    {
194
        return new class($callback) implements MiddlewareInterface {
195
            private $callback;
196
197
            public function __construct(callable $callback)
198
            {
199 1
                $this->callback = $callback;
200
            }
201
202
            public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
203
            {
204 1
                return \call_user_func($this->callback, $request, $handler);
205
            }
206
        };
207
    }
208
209 1
    public function __toString()
210
    {
211 1
        $result = '';
212
213 1
        if ($this->name !== null) {
214 1
            $result .= '[' . $this->name . '] ';
215
        }
216
217 1
        if ($this->methods !== null) {
218 1
            $result .= implode(',', $this->methods) . ' ';
219
        }
220 1
        if ($this->host !== null && strrpos($this->pattern, $this->host) === false) {
221 1
            $result .= $this->host;
222
        }
223 1
        $result .= $this->pattern;
224
225 1
        return $result;
226
    }
227
228 2
    public function getName(): string
229
    {
230 2
        return $this->name ?? implode(', ', $this->methods) . ' ' . $this->pattern;
231
    }
232
233 9
    public function getMethods(): array
234
    {
235 9
        return $this->methods;
236
    }
237
238 1
    public function getPattern(): string
239
    {
240 1
        return $this->pattern;
241
    }
242
243 1
    public function getHost(): ?string
244
    {
245 1
        return $this->host;
246
    }
247
248 1
    public function getParameters(): array
249
    {
250 1
        return $this->parameters;
251
    }
252
253 1
    public function getDefaults(): array
254
    {
255 1
        return $this->defaults;
256
    }
257
258
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
259
    {
260
        return $this->middleware->process($request, $handler);
261
    }
262
}
263