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

Router   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 180
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCurrentRoute() 0 4 1
A route() 0 11 2
A __construct() 0 9 1
A configure() 0 11 2
A getRoute() 0 6 1
A configureSlice() 0 9 2
A routes() 0 9 2
A find() 0 15 3
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Exceptions;
6
use Bavix\Slice\Slice;
7
use Psr\Cache\CacheItemPoolInterface;
8
9
class Router
10
{
11
12
    /**
13
     * @var array
14
     */
15
    protected $classMap = [
16
        'configure' => Configure::class
17
    ];
18
19
    /**
20
     * @var Slice
21
     */
22
    protected $slice;
23
24
    /**
25
     * @var Slice
26
     */
27
    protected $configureSlice;
28
29
    /**
30
     * @var Route[]
31
     */
32
    protected $routes;
33
34
    /**
35
     * @var Configure
36
     */
37
    protected $configure;
38
39
    /**
40
     * @var string
41
     */
42
    protected $method;
43
44
    /**
45
     * @var string
46
     */
47
    protected $protocol;
48
49
    /**
50
     * @var string
51
     */
52
    protected $host;
53
54
    /**
55
     * @var string
56
     */
57
    protected $path;
58
59
    /**
60
     * @var CacheItemPoolInterface
61
     */
62
    protected $pool;
63
64
    /**
65
     * Router constructor.
66
     *
67
     * @param Slice                  $slice
68
     * @param CacheItemPoolInterface $pool
69
     */
70
    public function __construct(Slice $slice, CacheItemPoolInterface $pool = null)
71
    {
72
        $this->slice    = $slice;
73
        $this->pool     = $pool;
74
        $this->method   = method();
75
        $this->protocol = protocol();
76
        $this->host     = host();
77
        $this->path     = path();
78
    }
79
80
    /**
81
     * @return Configure
82
     */
83
    protected function configure()
84
    {
85
        if (!$this->configure)
86
        {
87
            $class = $this->classMap['configure'];
88
89
            $this->configure = new $class($this->slice, $this->pool);
90
        }
91
92
        return $this->configure;
93
    }
94
95
    /**
96
     * @return Route
97
     *
98
     * @throws Exceptions\NotFound\Data
99
     */
100
    public function getCurrentRoute()
101
    {
102
        return $this->getRoute($this->path);
103
    }
104
105
    /**
106
     * @param string $path
107
     * @param string $host
108
     * @param string $protocol
109
     *
110
     * @return Route
111
     * @throws Exceptions\NotFound\Data
112
     */
113
    public function getRoute($path, $host = null, $protocol = null)
114
    {
115
        $uri = ($protocol ?? $this->protocol) . '://' . ($host ?? $this->host) . $path;
116
117
        return $this->find($uri);
118
    }
119
120
    /**
121
     * @return Slice
122
     */
123
    protected function configureSlice()
124
    {
125
        if (!$this->configureSlice)
126
        {
127
            $this->configureSlice = $this->configure()->data();
128
        }
129
130
        return $this->configureSlice;
131
    }
132
133
    /**
134
     * @return Route[]
135
     */
136
    public function routes()
137
    {
138
        if (empty($this->routes))
139
        {
140
            $this->routes = $this->configureSlice()->asArray();
141
        }
142
143
        return $this->routes;
144
    }
145
146
    /**
147
     * @param string $path
148
     *
149
     * @return Route
150
     *
151
     * @throws Exceptions\NotFound\Path
152
     */
153
    public function route($path)
154
    {
155
        $route = $this->configureSlice()->atData($path);
156
157
        if (empty($route))
158
        {
159
            throw new Exceptions\NotFound\Path('Route `' . $path . '` not found');
160
        }
161
162
        return $route;
163
    }
164
165
    /**
166
     * @param $uri
167
     *
168
     * @return Route
169
     *
170
     * @throws Exceptions\NotFound\Page
171
     */
172
    protected function find($uri)
173
    {
174
        /**
175
         * @var Route $route
176
         */
177
        foreach ($this->routes() as $key => $route)
178
        {
179
            if ($route->test($uri, $this->method))
180
            {
181
                return $route;
182
            }
183
        }
184
185
        throw new Exceptions\NotFound\Page('Page `' . $uri . '` not found', 404);
186
    }
187
188
}
189