Passed
Pull Request — master (#34)
by Anatoly
03:15 queued 51s
created

Route::withAddedAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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