Completed
Push — master ( cd1d0d...6a003f )
by Joschi
02:54
created

Route::validateAction()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 45
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 45
ccs 22
cts 22
cp 1
rs 8.439
cc 6
eloc 22
nc 6
nop 1
crap 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|\Closure|array
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|\Closure|array $action Route action
186
     */
187 15
    public function __construct($verbs, $name, $path, $action)
188
    {
189
        // Set and validate the allowed HTTP verbs
190 15
        $this->setAndValidateVerbs($verbs);
191
192
        // Set and validate the route name
193 13
        $this->setAndValidateName($name);
194
195
        // Set and validate the route path
196 12
        $this->setAndValidatePath($path);
197
198
        // Set and validate the route action(s)
199 11
        $this->setAndValidateActionList($action);
200 7
    }
201
202
    /**
203
     * Validate the allowed HTTP verbs
204
     *
205
     * @param string|array $verbs Allowed HTTP verbs
206
     * @throws InvalidArgumentException If the HTTP verb list is empty
207
     * @throws InvalidArgumentException If the HTTP verb is invalid
208
     */
209 15
    protected function setAndValidateVerbs($verbs)
210
    {
211 15
        $this->verbs = array_map('strtoupper', array_filter((array)$verbs));
212
213
        // If the HTTP verb list is empty
214 15
        if (!count($this->verbs)) {
215 1
            throw new InvalidArgumentException(
216 1
                'Empty route HTTP verbs',
217 1
                InvalidArgumentException::EMPTY_ROUTE_HTTP_VERBS
218
            );
219
        }
220
221
        // Run through all registered HTTP verbs
222 14
        foreach ($this->verbs as $verb) {
223
            // If the HTTP verb is invalid
224 14
            if (!array_key_exists($verb, self::$validVerbs)) {
225 1
                throw new InvalidArgumentException(
226 1
                    sprintf('Invalid route HTTP verb "%s"', $verb),
227 14
                    InvalidArgumentException::INVALID_ROUTE_HTTP_VERB
228
                );
229
            }
230
        }
231 13
    }
232
233
    /**
234
     * Set and validate the route name
235
     *
236
     * @param string $name Route name
237
     * @throws InvalidArgumentException If the route name is empty
238
     */
239 13
    protected function setAndValidateName($name)
240
    {
241 13
        $this->name = trim($name);
242
        // If the route name is empty
243 13
        if (!strlen($this->name)) {
244 1
            throw new InvalidArgumentException(
245 1
                'Route name must not be empty',
246 1
                InvalidArgumentException::EMPTY_ROUTE_NAME
247
            );
248
        }
249 12
    }
250
251
    /**
252
     * Set and validate the route path
253
     *
254
     * @param string $path Route path
255
     * @throws InvalidArgumentException If the route path is empty
256
     */
257 12
    protected function setAndValidatePath($path)
258
    {
259 12
        $this->path = trim($path);
260
        // If the route path is empty
261 12
        if (!strlen($this->path)) {
262 1
            throw new InvalidArgumentException(
263 1
                'Route path must not be empty',
264 1
                InvalidArgumentException::EMPTY_ROUTE_PATH
265
            );
266
        }
267 11
    }
268
269
    /**
270
     * Set and validate the route action
271
     *
272
     * @param string|\Callable|\Closure|array $actions Route actions
273
     */
274 11
    protected function setAndValidateActionList($actions)
275
    {
276 11
        $actionList = array_map([$this, 'validateAction'], (array)$actions);
277 7
        $this->action = is_array($actions) ? $actionList : current($actionList);
278 7
    }
279
280
    /**
281
     * Get the route name
282
     *
283
     * @return string Route name
284
     */
285 6
    public function getName()
286
    {
287 6
        return $this->name;
288
    }
289
290
    /**
291
     * Get the route path
292
     *
293
     * @return string Route path
294
     */
295 6
    public function getPath()
296
    {
297 6
        return $this->path;
298
    }
299
300
    /**
301
     * Get the route action
302
     *
303
     * @return \Callable|\Closure|string|array Route action
304
     */
305 6
    public function getAction()
306
    {
307 6
        return $this->action;
308
    }
309
310
    /**
311
     * Get the route tokens
312
     *
313
     * @return array Route tokens
314
     */
315 6
    public function getTokens()
316
    {
317 6
        return $this->tokens;
318
    }
319
320
    /**
321
     * Set the route tokens
322
     *
323
     * @param array $tokens Route tokens
324
     * @return Route Self reference
325
     */
326 2
    public function setTokens(array $tokens)
327
    {
328 2
        $this->tokens = $tokens;
329 2
        return $this;
330
    }
331
332
    /**
333
     * Get the route defaults
334
     *
335
     * @return array Route defaults
336
     */
337 6
    public function getDefaults()
338
    {
339 6
        return $this->defaults;
340
    }
341
342
    /**
343
     * Set the route defaults
344
     *
345
     * @param array $defaults Route defaults
346
     * @return Route Self reference
347
     */
348 1
    public function setDefaults(array $defaults)
349
    {
350 1
        $this->defaults = $defaults;
351 1
        return $this;
352
    }
353
354
    /**
355
     * Set the route wildcard name
356
     *
357
     * @return null|string
358
     */
359 6
    public function getWildcard()
360
    {
361 6
        return $this->wildcard;
362
    }
363
364
    /**
365
     * Get the route wildcard name
366
     *
367
     * @param null|string $wildcard Wildcard name
368
     * @return Route Self reference
369
     */
370 1
    public function setWildcard($wildcard)
371
    {
372 1
        $this->wildcard = $wildcard;
373 1
        return $this;
374
    }
375
376
    /**
377
     * Get the route host
378
     *
379
     * @return null|string Route host
380
     */
381 6
    public function getHost()
382
    {
383 6
        return $this->host;
384
    }
385
386
    /**
387
     * Set the route host
388
     *
389
     * @param null|string $host Route host
390
     * @return Route Self reference
391
     */
392 1
    public function setHost($host)
393
    {
394 1
        $this->host = $host;
395 1
        return $this;
396
    }
397
398
    /**
399
     * Get the allowed Accept headers
400
     *
401
     * @return array Allowed Accept headers
402
     */
403 6
    public function getAccepts()
404
    {
405 6
        return $this->accepts;
406
    }
407
408
    /**
409
     * Set the allowed Accept headers
410
     *
411
     * @param array $accepts Allowed Accept headers
412
     * @return Route Self reference
413
     */
414 2
    public function setAccepts(array $accepts)
415
    {
416 2
        $this->accepts = $accepts;
417 2
        return $this;
418
    }
419
420
    /**
421
     * Get the allowed HTTP verbs
422
     *
423
     * @return array Allowed HTTP verbs
424
     */
425 6
    public function getVerbs()
426
    {
427 6
        return $this->verbs;
428
    }
429
430
    /**
431
     * Set the allowed HTTP verbs
432
     *
433
     * @param array $verbs Allowed HTTP verbs
434
     * @return Route Self reference
435
     */
436 1
    public function setVerbs(array $verbs)
437
    {
438
        // Set and validate the allowed HTTP verbs
439 1
        $this->setAndValidateVerbs($verbs);
440
441 1
        return $this;
442
    }
443
444
    /**
445
     * Get the secure protocol mode
446
     *
447
     * @return boolean|null Secure protocol mode
448
     */
449 6
    public function getSecure()
450
    {
451 6
        return $this->secure;
452
    }
453
454
    /**
455
     * Set the secure protocol mode
456
     *
457
     * @param boolean|null $secure Secure protocol mode
458
     * @return Route Self reference
459
     */
460 1
    public function setSecure($secure)
461
    {
462 1
        $this->secure = $secure;
463 1
        return $this;
464
    }
465
466
    /**
467
     * Get the authentication information
468
     *
469
     * @return mixed Authentication information
470
     */
471 6
    public function getAuth()
472
    {
473 6
        return $this->auth;
474
    }
475
476
    /**
477
     * Set the authentication information
478
     *
479
     * @param mixed $auth Authentication information
480
     * @return Route Self reference
481
     */
482 1
    public function setAuth($auth)
483
    {
484 1
        $this->auth = $auth;
485 1
        return $this;
486
    }
487
488
    /**
489
     * Get the custom extra information
490
     *
491
     * @return array Custom extra information
492
     */
493 6
    public function getExtras()
494
    {
495 6
        return $this->extras;
496
    }
497
498
    /**
499
     * Set the custom extra information
500
     *
501
     * @param array $extras Custom extra information
502
     * @return Route Self reference
503
     */
504 1
    public function setExtras(array $extras)
505
    {
506 1
        $this->extras = $extras;
507 1
        return $this;
508
    }
509
510
    /**
511
     * Return whether this is an object route
512
     *
513
     * @return boolean Object route
514
     */
515 6
    public function isObject()
516
    {
517 6
        return $this->object;
518
    }
519
520
    /**
521
     * Set whether this is an object route
522
     *
523
     * @param boolean $object Object route
524
     * @return Route Self reference
525
     */
526 2
    public function setObject($object)
527
    {
528 2
        $this->object = !!$object;
529 2
        return $this;
530
    }
531
532
    /**
533
     * Validate the route action
534
     *
535
     * @param string|\Callable|\Closure $action Route action
536
     * @return string|\Callable|\Closure Validated route action
537
     * @throws InvalidArgumentException If the route action is empty
538
     * @throws InvalidArgumentException If the route action is not a class name
539
     * @throws InvalidArgumentException If the route action is neither a callable nor an ActionInterface
540
     */
541 11
    protected function validateAction($action)
542
    {
543
        // If the action is given as string
544 11
        if (is_string($action)) {
545 8
            $action = trim($action);
546
547
            // If the route action is empty
548 8
            if (!strlen($action)) {
549 1
                throw new InvalidArgumentException(
550 1
                    'Route action must not be empty',
551 1
                    InvalidArgumentException::EMPTY_ROUTE_ACTION
552
                );
553
            }
554
555
            // If the route action is not a class name
556 7
            if (!class_exists($action)) {
557 1
                throw new InvalidArgumentException(
558 1
                    'Route action must be an existing class name',
559 1
                    InvalidArgumentException::ROUTE_ACTION_MUST_BE_CLASSNAME
560
                );
561
            }
562
563
            // If the route action doesn't implement the ActionInterface
564 6
            $actionReflection = new \ReflectionClass($action);
565 6
            if (!$actionReflection->implementsInterface(ActionInterface::class)) {
566 1
                throw new InvalidArgumentException(
567 1
                    'Route action must implement '.ActionInterface::class,
568 1
                    InvalidArgumentException::ROUTE_ACTION_MUST_IMPLEMENT_ACTION_INTERFACE
569
                );
570
            }
571
572 5
            return $action;
573
        }
574
575
        // If the action is given as callable / closure
576 3
        if (is_callable($action)) {
577 2
            return $action;
578
        }
579
580
        // If the route action is neither a callable nor an ActionInterface
581 1
        throw new InvalidArgumentException(
582 1
            'Route action must be a callable or '.ActionInterface::class,
583 1
            InvalidArgumentException::ROUTE_ACTION_NOT_CALLABLE_OR_ACTION_INTERFACE
584
        );
585
    }
586
}
587