Passed
Push — master ( 54ca4c...4c57ef )
by Pierre
02:19
created

Route   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 88
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A getExpr() 0 3 1
A getMethod() 0 3 1
A getSlugs() 0 3 1
A parsedSlugs() 0 6 3
1
<?php
2
3
namespace App\Component\Http;
4
5
use App\Component\Http\Interfaces\IRoute;
6
7
class Route implements IRoute
8
{
9
10
    /**
11
     * allowed request method
12
     *
13
     * @var string
14
     */
15
    protected $method;
16
17
    /**
18
     * uri validation regexp
19
     *
20
     * @var string
21
     */
22
    protected $expr;
23
24
    /**
25
     * slug collection
26
     *
27
     * @var array
28
     */
29
    protected $slugs;
30
31
    /**
32
     * instanciate
33
     *
34
     * @param string $routeItem
35
     */
36 6
    public function __construct(string $routeItem)
37
    {
38 6
        $this->method = 'GET';
39 6
        $this->expr = '/^(*.)$/';
40 6
        $this->slugs = [];
41 6
        if (strpos($routeItem, ';') !== false) {
42
            list(
43 1
                $this->method,
44 1
                $this->expr,
45
                $slugs
46 1
            ) = explode(';', $routeItem);
47 1
            $this->slugs = $this->parsedSlugs($slugs);
48
        } else {
49 6
            $this->expr = $routeItem;
50
        }
51
    }
52
53
    /**
54
     * return regexp pattern
55
     *
56
     * @return string
57
     */
58 2
    public function getExpr(): string
59
    {
60 2
        return $this->expr;
61
    }
62
63
    /**
64
     * return required request method
65
     *
66
     * @return string
67
     */
68 2
    public function getMethod(): string
69
    {
70 2
        return $this->method;
71
    }
72
73
    /**
74
     * return slugs
75
     *
76
     * @return array
77
     */
78 1
    public function getSlugs(): array
79
    {
80 1
        return $this->slugs;
81
    }
82
83
    /**
84
     * return parsed slugs as string collection
85
     *
86
     * @param string $rawSlug
87
     * @return array
88
     */
89 1
    protected function parsedSlugs(string $rawSlug): array
90
    {
91 1
        if (is_null($rawSlug) || empty($rawSlug)) {
92 1
            return [];
93
        }
94 1
        return explode(',', $rawSlug);
95
    }
96
}
97