Test Failed
Pull Request — master (#34)
by Anatoly
02:07
created

Route::getRequestHandler()   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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
use Sunrise\Http\Router\RequestHandler\QueueableRequestHandler;
22
23
/**
24
 * Import functions
25
 */
26
use function rtrim;
27
use function strtoupper;
28
29
/**
30
 * Route
31
 *
32
 * Use the factory to create this class.
33
 */
34
class Route implements RouteInterface
35
{
36
37
    /**
38
     * Server Request attribute name for the route name
39
     *
40
     * @var string
41
     */
42
    public const ATTR_NAME_FOR_ROUTE_NAME = '@route-name';
43
44
    /**
45
     * The route name
46
     *
47
     * @var string
48
     */
49
    private $name;
50
51
    /**
52
     * The route path
53
     *
54
     * @var string
55
     */
56
    private $path;
57
58
    /**
59
     * The route methods
60
     *
61
     * @var string[]
62
     */
63
    private $methods;
64
65
    /**
66
     * The route request handler
67
     *
68
     * @var RequestHandlerInterface
69
     */
70
    private $requestHandler;
71
72
    /**
73
     * The route middlewares
74 48
     *
75
     * @var MiddlewareInterface[]
76 48
     */
77
    private $middlewares = [];
78 48
79
    /**
80 48
     * The route attributes
81
     *
82 25
     * @var array
83
     */
84 48
    private $attributes = [];
85
86
    /**
87
     * Constructor of the class
88
     *
89 48
     * @param string $name
90
     * @param string $path
91 48
     * @param string[] $methods
92
     * @param RequestHandlerInterface $requestHandler
93 48
     * @param MiddlewareInterface[] $middlewares
94
     * @param array $attributes
95
     */
96
    public function __construct(
97
        string $name,
98
        string $path,
99 48
        array $methods,
100
        RequestHandlerInterface $requestHandler,
101 48
        array $middlewares = [],
102
        array $attributes = []
103 48
    ) {
104
        $this->setName($name);
105
        $this->setPath($path);
106
        $this->setMethods(...$methods);
107
        $this->setRequestHandler($requestHandler);
108
        $this->setMiddlewares(...$middlewares);
109 3
        $this->setAttributes($attributes);
110
    }
111 3
112
    /**
113 3
     * {@inheritDoc}
114
     */
115
    public function getName() : string
116
    {
117
        return $this->name;
118
    }
119 2
120
    /**
121 2
     * {@inheritDoc}
122
     */
123 2
    public function getPath() : string
124
    {
125
        return $this->path;
126
    }
127
128
    /**
129 25
     * {@inheritDoc}
130
     */
131 25
    public function getMethods() : array
132
    {
133 25
        return $this->methods;
134
    }
135
136
    /**
137
     * {@inheritDoc}
138
     */
139 3
    public function getRequestHandler() : RequestHandlerInterface
140
    {
141 3
        return $this->requestHandler;
142
    }
143 3
144
    /**
145
     * {@inheritDoc}
146
     */
147
    public function getMiddlewares() : array
148
    {
149 3
        return $this->middlewares;
150
    }
151 3
152
    /**
153 3
     * {@inheritDoc}
154
     */
155
    public function getAttributes() : array
156
    {
157
        return $this->attributes;
158
    }
159 13
160
    /**
161 13
     * {@inheritDoc}
162
     */
163
    public function setName(string $name) : RouteInterface
164
    {
165
        $this->name = $name;
166
167 26
        return $this;
168
    }
169 26
170
    /**
171
     * {@inheritDoc}
172
     */
173
    public function setPath(string $path) : RouteInterface
174
    {
175 22
        $this->path = $path;
176
177 22
        return $this;
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     */
183 12
    public function setMethods(string ...$methods) : RouteInterface
184
    {
185 12
        foreach ($methods as &$method) {
186
            $method = strtoupper($method);
187
        }
188
189
        $this->methods = $methods;
190
191 8
        return $this;
192
    }
193 8
194
    /**
195
     * {@inheritDoc}
196
     */
197
    public function setRequestHandler(RequestHandlerInterface $requestHandler) : RouteInterface
198
    {
199 4
        $this->requestHandler = $requestHandler;
200
201 4
        return $this;
202
    }
203
204
    /**
205
     * {@inheritDoc}
206
     */
207 9
    public function setMiddlewares(MiddlewareInterface ...$middlewares) : RouteInterface
208
    {
209 9
        $this->middlewares = $middlewares;
210
211 9
        return $this;
212
    }
213 9
214
    /**
215
     * {@inheritDoc}
216
     */
217
    public function setAttributes(array $attributes) : RouteInterface
218
    {
219
        $this->attributes = $attributes;
220
221
        return $this;
222
    }
223
224
    /**
225
     * {@inheritDoc}
226
     */
227
    public function addPrefix(string $prefix) : RouteInterface
228
    {
229
        // https://github.com/sunrise-php/http-router/issues/26
230
        $prefix = rtrim($prefix, '/');
231
232
        $this->path = $prefix . $this->path;
233
234
        return $this;
235
    }
236
237
    /**
238
     * {@inheritDoc}
239
     */
240
    public function addSuffix(string $suffix) : RouteInterface
241
    {
242
        $this->path .= $suffix;
243
244
        return $this;
245
    }
246
247
    /**
248
     * {@inheritDoc}
249
     */
250
    public function addMethod(string ...$methods) : RouteInterface
251
    {
252
        foreach ($methods as $method) {
253
            $this->methods[] = strtoupper($method);
254
        }
255
256
        return $this;
257
    }
258
259
    /**
260
     * {@inheritDoc}
261
     */
262
    public function addMiddleware(MiddlewareInterface ...$middlewares) : RouteInterface
263
    {
264
        foreach ($middlewares as $middleware) {
265
            $this->middlewares[] = $middleware;
266
        }
267
268
        return $this;
269
    }
270
271
    /**
272
     * {@inheritDoc}
273
     */
274
    public function withAddedAttributes(array $attributes) : RouteInterface
275
    {
276
        $clone = clone $this;
277
278
        foreach ($attributes as $key => $value) {
279
            $clone->attributes[$key] = $value;
280
        }
281
282
        return $clone;
283
    }
284
285
    /**
286
     * {@inheritDoc}
287
     */
288
    public function handle(ServerRequestInterface $request) : ResponseInterface
289
    {
290
        $request = $request->withAttribute(self::ATTR_NAME_FOR_ROUTE_NAME, $this->name);
291
292
        foreach ($this->attributes as $key => $value) {
293
            $request = $request->withAttribute($key, $value);
294
        }
295
296
        $handler = new QueueableRequestHandler($this->requestHandler);
297
        $handler->add(...$this->middlewares);
298
299
        return $handler->handle($request);
300
    }
301
}
302