Passed
Branch release/v2.0.0 (7950fb)
by Anatoly
15:11
created

Route::setAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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