Completed
Push — master ( 9ce6e7...ad5412 )
by Joschi
03:20
created

Route::getAccepts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * apparat-server
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Server
8
 * @subpackage  Apparat\Server\Ports
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Server\Ports\Route;
38
39
use Apparat\Server\Ports\Action\ActionInterface;
40
use Apparat\Server\Ports\Contract\RouteInterface;
41
42
/**
43
 * Route
44
 *
45
 * @package Apparat\Server
46
 * @subpackage Apparat\Server\Ports
47
 */
48
class Route implements RouteInterface
49
{
50
    /**
51
     * GET request
52
     *
53
     * @var string
54
     */
55
    const GET = 'GET';
56
    /**
57
     * POST request
58
     *
59
     * @var string
60
     */
61
    const POST = 'POST';
62
    /**
63
     * PATCH request
64
     *
65
     * @var string
66
     */
67
    const PATCH = 'PATCH';
68
    /**
69
     * DELETE request
70
     *
71
     * @var string
72
     */
73
    const DELETE = 'DELETE';
74
    /**
75
     * OPTIONS request
76
     *
77
     * @var string
78
     */
79
    const OPTIONS = 'OPTIONS';
80
    /**
81
     * HEAD request
82
     *
83
     * @var string
84
     */
85
    const HEAD = 'HEAD';
86
87
    /**
88
     * Allowed HTTP verbs
89
     *
90
     * @var array
91
     */
92
    protected static $validVerbs = [
93
        self::GET => true,
94
        self::POST => true,
95
        self::PATCH => true,
96
        self::DELETE => true,
97
        self::OPTIONS => true,
98
        self::HEAD => true
99
    ];
100
    /**
101
     * Route name
102
     *
103
     * @var string
104
     */
105
    protected $name;
106
    /**
107
     * Route path
108
     *
109
     * @var string
110
     */
111
    protected $path;
112
    /**
113
     * Route action
114
     *
115
     * @var string|callable
116
     */
117
    protected $action;
118
    /**
119
     * Route tokens
120
     *
121
     * @var array
122
     */
123
    protected $tokens = [];
124
    /**
125
     * Route default values
126
     *
127
     * @var array
128
     */
129
    protected $defaults = [];
130
    /**
131
     * Route wildcard name
132
     *
133
     * @var string|null
134
     */
135
    protected $wildcard = null;
136
    /**
137
     * Route host
138
     *
139
     * @var string|null
140
     */
141
    protected $host = null;
142
    /**
143
     * Allowed accept headers
144
     *
145
     * @var array
146
     */
147
    protected $accepts = [];
148
    /**
149
     * Allowed HTTP verbs
150
     *
151
     * @var array
152
     */
153
    protected $verbs = [];
154
    /**
155
     * Secure protocol behaviour
156
     *
157
     * @var boolean|null
158
     */
159
    protected $secure = false;
160
    /**
161
     * Authentication parameters
162
     *
163
     * @var mixed
164
     */
165
    protected $auth = null;
166
    /**
167
     * Custom extra parameters
168
     *
169
     * @var array
170
     */
171
    protected $extras = [];
172
    /**
173
     * Object route
174
     *
175
     * @var bool
176
     */
177
    protected $object = false;
178
179
    /**
180
     * Route constructor
181
     *
182
     * @param string|array $verbs Allowed HTTP verbs
183
     * @param string $name Route name
184
     * @param string $path Route path
185
     * @param string|\Callable|array $action Route action
186
     */
187 15
    public function __construct($verbs, $name, $path, $action)
188
    {
189
        // Set whether this is an object route
190 15
        $this->object = is_array($action);
191
192
        // Set and validate the allowed HTTP verbs
193 15
        $this->setAndValidateVerbs($verbs);
194
195
        // Set and validate the route name
196 13
        $this->setAndValidateName($name);
197
198
        // Set and validate the route path
199 12
        $this->setAndValidatePath($path);
200
201
        // Set and validate the route action
202 11
        $this->setAndValidateActionList($action);
203 7
    }
204
205
    /**
206
     * Validate the allowed HTTP verbs
207
     *
208
     * @param string|array $verbs Allowed HTTP verbs
209
     * @throws InvalidArgumentException If the HTTP verb list is empty
210
     * @throws InvalidArgumentException If the HTTP verb is invalid
211
     */
212 15
    protected function setAndValidateVerbs($verbs)
213
    {
214 15
        $this->verbs = array_map('strtoupper', array_filter((array)$verbs));
215
216
        // If the HTTP verb list is empty
217 15
        if (!count($this->verbs)) {
218 1
            throw new InvalidArgumentException(
219 1
                'Empty route HTTP verbs',
220 1
                InvalidArgumentException::EMPTY_ROUTE_HTTP_VERBS
221
            );
222
        }
223
224
        // Run through all registered HTTP verbs
225 14
        foreach ($this->verbs as $verb) {
226
            // If the HTTP verb is invalid
227 14
            if (!array_key_exists($verb, self::$validVerbs)) {
228 1
                throw new InvalidArgumentException(
229 1
                    sprintf('Invalid route HTTP verb "%s"', $verb),
230 14
                    InvalidArgumentException::INVALID_ROUTE_HTTP_VERB
231
                );
232
            }
233
        }
234 13
    }
235
236
    /**
237
     * Set and validate the route name
238
     *
239
     * @param string $name Route name
240
     * @throws InvalidArgumentException If the route name is empty
241
     */
242 13
    protected function setAndValidateName($name)
243
    {
244 13
        $this->name = trim($name);
245
        // If the route name is empty
246 13
        if (!strlen($this->name)) {
247 1
            throw new InvalidArgumentException(
248 1
                'Route name must not be empty',
249 1
                InvalidArgumentException::EMPTY_ROUTE_NAME
250
            );
251
        }
252 12
    }
253
254
    /**
255
     * Set and validate the route path
256
     *
257
     * @param string $path Route path
258
     * @throws InvalidArgumentException If the route path is empty
259
     */
260 12
    protected function setAndValidatePath($path)
261
    {
262 12
        $this->path = trim($path);
263
        // If the route path is empty
264 12
        if (!strlen($this->path)) {
265 1
            throw new InvalidArgumentException(
266 1
                'Route path must not be empty',
267 1
                InvalidArgumentException::EMPTY_ROUTE_PATH
268
            );
269
        }
270 11
    }
271
272
    /**
273
     * Set and validate the route action
274
     *
275
     * @param string|\Callable|array $actions Route actions
276
     */
277 11
    protected function setAndValidateActionList($actions)
278
    {
279 11
        array_map([$this, 'setAndValidateAction'], (array)$actions);
280 7
        $this->action = $actions;
281 7
    }
282
283
    /**
284
     * Get the route name
285
     *
286
     * @return string Route name
287
     */
288 6
    public function getName()
289
    {
290 6
        return $this->name;
291
    }
292
293
    /**
294
     * Get the route path
295
     *
296
     * @return string Route path
297
     */
298 6
    public function getPath()
299
    {
300 6
        return $this->path;
301
    }
302
303
    /**
304
     * Get the route action
305
     *
306
     * @return callable|string Route action
307
     */
308 6
    public function getAction()
309
    {
310 6
        return $this->action;
311
    }
312
313
    /**
314
     * Get the route tokens
315
     *
316
     * @return array Route tokens
317
     */
318 6
    public function getTokens()
319
    {
320 6
        return $this->tokens;
321
    }
322
323
    /**
324
     * Set the route tokens
325
     *
326
     * @param array $tokens Route tokens
327
     * @return Route Self reference
328
     */
329 2
    public function setTokens(array $tokens)
330
    {
331 2
        $this->tokens = $tokens;
332 2
        return $this;
333
    }
334
335
    /**
336
     * Get the route defaults
337
     *
338
     * @return array Route defaults
339
     */
340 6
    public function getDefaults()
341
    {
342 6
        return $this->defaults;
343
    }
344
345
    /**
346
     * Set the route defaults
347
     *
348
     * @param array $defaults Route defaults
349
     * @return Route Self reference
350
     */
351 1
    public function setDefaults(array $defaults)
352
    {
353 1
        $this->defaults = $defaults;
354 1
        return $this;
355
    }
356
357
    /**
358
     * Set the route wildcard name
359
     *
360
     * @return null|string
361
     */
362 6
    public function getWildcard()
363
    {
364 6
        return $this->wildcard;
365
    }
366
367
    /**
368
     * Get the route wildcard name
369
     *
370
     * @param null|string $wildcard Wildcard name
371
     * @return Route Self reference
372
     */
373 1
    public function setWildcard($wildcard)
374
    {
375 1
        $this->wildcard = $wildcard;
376 1
        return $this;
377
    }
378
379
    /**
380
     * Get the route host
381
     *
382
     * @return null|string Route host
383
     */
384 6
    public function getHost()
385
    {
386 6
        return $this->host;
387
    }
388
389
    /**
390
     * Set the route host
391
     *
392
     * @param null|string $host Route host
393
     * @return Route Self reference
394
     */
395 1
    public function setHost($host)
396
    {
397 1
        $this->host = $host;
398 1
        return $this;
399
    }
400
401
    /**
402
     * Get the allowed Accept headers
403
     *
404
     * @return array Allowed Accept headers
405
     */
406 6
    public function getAccepts()
407
    {
408 6
        return $this->accepts;
409
    }
410
411
    /**
412
     * Set the allowed Accept headers
413
     *
414
     * @param array $accepts Allowed Accept headers
415
     * @return Route Self reference
416
     */
417 1
    public function setAccepts(array $accepts)
418
    {
419 1
        $this->accepts = $accepts;
420 1
        return $this;
421
    }
422
423
    /**
424
     * Get the allowed HTTP verbs
425
     *
426
     * @return array Allowed HTTP verbs
427
     */
428 6
    public function getVerbs()
429
    {
430 6
        return $this->verbs;
431
    }
432
433
    /**
434
     * Set the allowed HTTP verbs
435
     *
436
     * @param array $verbs Allowed HTTP verbs
437
     * @return Route Self reference
438
     */
439 1
    public function setVerbs(array $verbs)
440
    {
441
        // Set and validate the allowed HTTP verbs
442 1
        $this->setAndValidateVerbs($verbs);
443
444 1
        return $this;
445
    }
446
447
    /**
448
     * Get the secure protocol mode
449
     *
450
     * @return boolean|null Secure protocol mode
451
     */
452 6
    public function getSecure()
453
    {
454 6
        return $this->secure;
455
    }
456
457
    /**
458
     * Set the secure protocol mode
459
     *
460
     * @param boolean|null $secure Secure protocol mode
461
     * @return Route Self reference
462
     */
463 1
    public function setSecure($secure)
464
    {
465 1
        $this->secure = $secure;
466 1
        return $this;
467
    }
468
469
    /**
470
     * Get the authentication information
471
     *
472
     * @return mixed Authentication information
473
     */
474 6
    public function getAuth()
475
    {
476 6
        return $this->auth;
477
    }
478
479
    /**
480
     * Set the authentication information
481
     *
482
     * @param mixed $auth Authentication information
483
     * @return Route Self reference
484
     */
485 1
    public function setAuth($auth)
486
    {
487 1
        $this->auth = $auth;
488 1
        return $this;
489
    }
490
491
    /**
492
     * Get the custom extra information
493
     *
494
     * @return array Custom extra information
495
     */
496 6
    public function getExtras()
497
    {
498 6
        return $this->extras;
499
    }
500
501
    /**
502
     * Set the custom extra information
503
     *
504
     * @param array $extras Custom extra information
505
     * @return Route Self reference
506
     */
507 1
    public function setExtras(array $extras)
508
    {
509 1
        $this->extras = $extras;
510 1
        return $this;
511
    }
512
513
    /**
514
     * Return whether this is an object route
515
     *
516
     * @return boolean Object route
517
     */
518 6
    public function isObject()
519
    {
520 6
        return $this->object;
521
    }
522
523
    /**
524
     * Set and validate the route action
525
     *
526
     * @param string $action Route action
527
     * @throws InvalidArgumentException If the route action is empty
528
     * @throws InvalidArgumentException If the route action is not a class name
529
     * @throws InvalidArgumentException If the route action is neither a callable nor an ActionInterface
530
     */
531 11
    protected function setAndValidateAction($action)
532
    {
533
        // If the action is given as string
534 11
        if (is_string($action)) {
535 8
            $this->action = trim($action);
536
537
            // If the route action is empty
538 8
            if (!strlen($this->action)) {
539 1
                throw new InvalidArgumentException(
540 1
                    'Route action must not be empty',
541 1
                    InvalidArgumentException::EMPTY_ROUTE_ACTION
542
                );
543
            }
544
545
            // If the route action is not a class name
546 7
            if (!class_exists($this->action)) {
547 1
                throw new InvalidArgumentException(
548 1
                    'Route action must be an existing class name',
549 1
                    InvalidArgumentException::ROUTE_ACTION_MUST_BE_CLASSNAME
550
                );
551
            }
552
553
            // If the route action doesn't implement the ActionInterface
554 6
            $actionReflection = new \ReflectionClass($this->action);
555 6
            if (!$actionReflection->implementsInterface(ActionInterface::class)) {
556 1
                throw new InvalidArgumentException(
557 1
                    'Route action must implement '.ActionInterface::class,
558 1
                    InvalidArgumentException::ROUTE_ACTION_MUST_IMPLEMENT_ACTION_INTERFACE
559
                );
560
            }
561
562 5
            return;
563
        }
564
565
        // If the action is given as callable
566 3
        if (is_callable($action)) {
567 2
            return;
568
        }
569
570
        // If the route action is neither a callable nor an ActionInterface
571 1
        throw new InvalidArgumentException(
572 1
            'Route action must be a callable or '.ActionInterface::class,
573 1
            InvalidArgumentException::ROUTE_ACTION_NOT_CALLABLE_OR_ACTION_INTERFACE
574
        );
575
    }
576
}
577