Completed
Push — master ( 6d353a...9ce6e7 )
by Joschi
03:43
created

Route   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 527
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 57.52%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 38
c 4
b 0
f 1
lcom 2
cbo 1
dl 0
loc 527
ccs 65
cts 113
cp 0.5752
rs 8.3999

28 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
B setAndValidateVerbs() 0 23 4
A setAndValidateName() 0 11 2
A setAndValidatePath() 0 11 2
A setAndValidateActionList() 0 5 1
A getName() 0 4 1
A getPath() 0 4 1
A getAction() 0 4 1
A getTokens() 0 4 1
A setTokens() 0 5 1
A getDefaults() 0 4 1
A setDefaults() 0 5 1
A getWildcard() 0 4 1
A setWildcard() 0 5 1
A getHost() 0 4 1
A setHost() 0 5 1
A getAccepts() 0 4 1
A setAccepts() 0 5 1
A getVerbs() 0 4 1
A setVerbs() 0 5 1
A getSecure() 0 4 1
A setSecure() 0 5 1
A getAuth() 0 4 1
A setAuth() 0 5 1
A getExtras() 0 4 1
A setExtras() 0 5 1
A isObject() 0 4 1
B setAndValidateAction() 0 45 6
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 4
    public function __construct($verbs, $name, $path, $action)
188
    {
189
        // Set whether this is an object route
190 4
        $this->object = is_array($action);
191
192
        // Set and validate the allowed HTTP verbs
193 4
        $this->setAndValidateVerbs($verbs);
194
195
        // Set and validate the route name
196 4
        $this->setAndValidateName($name);
197
198
        // Set and validate the route path
199 4
        $this->setAndValidatePath($path);
200
201
        // Set and validate the route action
202 4
        $this->setAndValidateActionList($action);
203 4
    }
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 4
    protected function setAndValidateVerbs($verbs)
213
    {
214 4
        $this->verbs = array_map('strtoupper', array_filter((array)$verbs));
215
216
        // If the HTTP verb list is empty
217 4
        if (!count($this->verbs)) {
218
            throw new InvalidArgumentException(
219
                'Empty route HTTP verbs',
220
                InvalidArgumentException::EMPTY_ROUTE_HTTP_VERBS
221
            );
222
        }
223
224
        // Run through all registered HTTP verbs
225 4
        foreach ($this->verbs as $verb) {
226
            // If the HTTP verb is invalid
227 4
            if (!array_key_exists($verb, self::$validVerbs)) {
228
                throw new InvalidArgumentException(
229
                    sprintf('Invalid route HTTP verb "%s"', $verb),
230
                    InvalidArgumentException::INVALID_ROUTE_HTTP_VERB
231
                );
232
            }
233 4
        }
234 4
    }
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 4
    protected function setAndValidateName($name)
243
    {
244 4
        $this->name = trim($name);
245
        // If the route name is empty
246 4
        if (!strlen($this->name)) {
247
            throw new InvalidArgumentException(
248
                'Route name must not be empty',
249
                InvalidArgumentException::EMPTY_ROUTE_NAME
250
            );
251
        }
252 4
    }
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 4
    protected function setAndValidatePath($path)
261
    {
262 4
        $this->path = trim($path);
263
        // If the route path is empty
264 4
        if (!strlen($this->path)) {
265
            throw new InvalidArgumentException(
266
                'Route path must not be empty',
267
                InvalidArgumentException::EMPTY_ROUTE_NAME
268
            );
269
        }
270 4
    }
271
272
    /**
273
     * Set and validate the route action
274
     *
275
     * @param string|\Callable|array $actions Route actions
276
     */
277 4
    protected function setAndValidateActionList($actions)
278
    {
279 4
        array_map([$this, 'setAndValidateAction'], (array)$actions);
280 4
        $this->action = $actions;
281 4
    }
282
283
    /**
284
     * Get the route name
285
     *
286
     * @return string Route name
287
     */
288 4
    public function getName()
289
    {
290 4
        return $this->name;
291
    }
292
293
    /**
294
     * Get the route path
295
     *
296
     * @return string Route path
297
     */
298 4
    public function getPath()
299
    {
300 4
        return $this->path;
301
    }
302
303
    /**
304
     * Get the route action
305
     *
306
     * @return callable|string Route action
307
     */
308 4
    public function getAction()
309
    {
310 4
        return $this->action;
311
    }
312
313
    /**
314
     * Get the route tokens
315
     *
316
     * @return array Route tokens
317
     */
318 4
    public function getTokens()
319
    {
320 4
        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 4
    public function getDefaults()
341
    {
342 4
        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
    public function setDefaults(array $defaults)
352
    {
353
        $this->defaults = $defaults;
354
        return $this;
355
    }
356
357
    /**
358
     * Set the route wildcard name
359
     *
360
     * @return null|string
361
     */
362 4
    public function getWildcard()
363
    {
364 4
        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
    public function setWildcard($wildcard)
374
    {
375
        $this->wildcard = $wildcard;
376
        return $this;
377
    }
378
379
    /**
380
     * Get the route host
381
     *
382
     * @return null|string Route host
383
     */
384 4
    public function getHost()
385
    {
386 4
        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
    public function setHost($host)
396
    {
397
        $this->host = $host;
398
        return $this;
399
    }
400
401
    /**
402
     * Get the allowed Accept headers
403
     *
404
     * @return array Allowed Accept headers
405
     */
406 4
    public function getAccepts()
407
    {
408 4
        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
    public function setAccepts(array $accepts)
418
    {
419
        $this->accepts = $accepts;
420
        return $this;
421
    }
422
423
    /**
424
     * Get the allowed HTTP verbs
425
     *
426
     * @return array Allowed HTTP verbs
427
     */
428 4
    public function getVerbs()
429
    {
430 4
        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
    public function setVerbs(array $verbs)
440
    {
441
        $this->verbs = $verbs;
442
        return $this;
443
    }
444
445
    /**
446
     * Get the secure protocol mode
447
     *
448
     * @return boolean|null Secure protocol mode
449
     */
450 4
    public function getSecure()
451
    {
452 4
        return $this->secure;
453
    }
454
455
    /**
456
     * Set the secure protocol mode
457
     *
458
     * @param boolean|null $secure Secure protocol mode
459
     * @return Route Self reference
460
     */
461
    public function setSecure($secure)
462
    {
463
        $this->secure = $secure;
464
        return $this;
465
    }
466
467
    /**
468
     * Get the authentication information
469
     *
470
     * @return mixed Authentication information
471
     */
472 4
    public function getAuth()
473
    {
474 4
        return $this->auth;
475
    }
476
477
    /**
478
     * Set the authentication information
479
     *
480
     * @param mixed $auth Authentication information
481
     * @return Route Self reference
482
     */
483
    public function setAuth($auth)
484
    {
485
        $this->auth = $auth;
486
        return $this;
487
    }
488
489
    /**
490
     * Get the custom extra information
491
     *
492
     * @return array Custom extra information
493
     */
494 4
    public function getExtras()
495
    {
496 4
        return $this->extras;
497
    }
498
499
    /**
500
     * Set the custom extra information
501
     *
502
     * @param array $extras Custom extra information
503
     * @return Route Self reference
504
     */
505
    public function setExtras(array $extras)
506
    {
507
        $this->extras = $extras;
508
        return $this;
509
    }
510
511
    /**
512
     * Return whether this is an object route
513
     *
514
     * @return boolean Object route
515
     */
516 4
    public function isObject()
517
    {
518 4
        return $this->object;
519
    }
520
521
    /**
522
     * Set and validate the route action
523
     *
524
     * @param string $action Route action
525
     * @throws InvalidArgumentException If the route action is empty
526
     * @throws InvalidArgumentException If the route action is not a class name
527
     * @throws InvalidArgumentException If the route action is neither a callable nor an ActionInterface
528
     */
529 4
    protected function setAndValidateAction($action)
530
    {
531
        // If the action is given as string
532 4
        if (is_string($action)) {
533 3
            $this->action = trim($action);
534
535
            // If the route action is empty
536 3
            if (!strlen($this->action)) {
537
                throw new InvalidArgumentException(
538
                    'Route action must not be empty',
539
                    InvalidArgumentException::EMPTY_ROUTE_ACTION
540
                );
541
            }
542
543
            // If the route action is not a class name
544 3
            if (!class_exists($this->action)) {
545
                throw new InvalidArgumentException(
546
                    'Route action must be an existing class name',
547
                    InvalidArgumentException::ROUTE_ACTION_MUST_BE_CLASSNAME
548
                );
549
            }
550
551
            // If the route action doesn't implement the ActionInterface
552 3
            $actionReflection = new \ReflectionClass($this->action);
553 3
            if (!$actionReflection->implementsInterface(ActionInterface::class)) {
554
                throw new InvalidArgumentException(
555
                    'Route action must implement '.ActionInterface::class,
556
                    InvalidArgumentException::ROUTE_ACTION_MUST_IMPLEMENT_ACTION_INTERFACE
557
                );
558
            }
559
560 3
            return;
561
        }
562
563
        // If the action is given as callable
564 1
        if (is_callable($action)) {
565 1
            return;
566
        }
567
568
        // If the route action is neither a callable nor an ActionInterface
569
        throw new InvalidArgumentException(
570
            'Route action must be a callable or '.ActionInterface::class,
571
            InvalidArgumentException::ROUTE_ACTION_NOT_CALLABLE_OR_ACTION_INTERFACE
572
        );
573
    }
574
}
575