Passed
Push — master ( 39666f...8b4565 )
by Бабичев
09:02
created

Type   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 123
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
build() 0 1 ?
A __construct() 0 16 2
A regexData() 0 12 3
A path() 0 13 3
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Exceptions\NotFound;
6
use Bavix\Slice\Slice;
7
8
abstract class Type
9
{
10
11
    use HelperThrows;
12
13
    /**
14
     * @var bool
15
     */
16
    protected $pathRequired;
17
18
    /**
19
     * @var Configure
20
     */
21
    protected $configure;
22
23
    /**
24
     * @var Slice
25
     */
26
    protected $slice;
27
28
    /**
29
     * @var array
30
     */
31
    protected $regex;
32
33
    /**
34
     * @var array
35
     */
36
    protected $defaults;
37
38
    /**
39
     * @var array
40
     */
41
    protected $methods;
42
43
    /**
44
     * @var string
45
     */
46
    protected $protocol;
47
48
    /**
49
     * @var string
50
     */
51
    protected $host;
52
53
    /**
54
     * @var string
55
     */
56
    protected $path;
57
58
    /**
59
     * @var string
60
     */
61
    protected $key;
62
63
    /**
64
     * Type constructor.
65
     *
66
     * @param Configure $configure
67
     * @param Slice     $slice
68
     * @param array     $options
69
     *
70
     * @throws NotFound\Data
71
     */
72
    public function __construct(Configure $configure, Slice $slice, array $options)
73
    {
74
        $this->configure = $configure;
75
        $this->slice     = $slice;
76
77
        $this->protocol = $options['protocol'] ?? null;
78
        $this->host     = $options['host'] ?? null;
79
        $this->key      = $options['key'] ?? null;
80
81
        $data = $this->path($slice);
82
83
        $this->path     = $options['path'] ?? $data[0];
84
        $this->regex    = $options['regex'] ?? $data[1];
85
        $this->defaults = (array)($options['defaults'] ?? $slice->getData('defaults'));
86
        $this->methods  = (array)(empty($options['methods']) ? $slice->getData('methods') : $options['methods']);
87
    }
88
89
    /**
90
     * @return array
91
     */
92
    protected function regexData(&$storage)
93
    {
94
        $regex = (array)$this->regex;
95
96
        if (is_array($storage) && isset($storage[1]))
97
        {
98
            $regex   = $storage[1] + $regex;
99
            $storage = $storage[0];
100
        }
101
102
        return $regex;
103
    }
104
105
    /**
106
     * @param Slice $slice
107
     *
108
     * @return array
109
     * @throws NotFound\Path
110
     */
111
    protected function path(Slice $slice)
112
    {
113
        $path = $slice->getData('path', $this->path);
114
115
        if ($this->pathRequired && !$path)
116
        {
117
            throw new NotFound\Path('Parameter `path` not found in a route of `' . $this->key . '`');
118
        }
119
120
        $regex = $this->regexData($path);
121
122
        return [$path, $regex];
123
    }
124
125
    /**
126
     * @return array
127
     */
128
    abstract public function build();
129
130
}