Test Failed
Pull Request — master (#32)
by Anatoly
04:53
created

Route::withAddedAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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