Passed
Pull Request — master (#3)
by Бабичев
01:36
created

Router::loadingRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.2109

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 8
cp 0.625
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 2.2109
1
<?php
2
3
namespace Bavix\Router;
4
5
use Bavix\Router\Rules\PatternRule;
6
use Psr\Cache\CacheItemPoolInterface;
7
use Bavix\Exceptions;
8
9
class Router
10
{
11
12
    /**
13
     * @var PatternRule[]
14
     */
15
    protected $routes;
16
17
    /**
18
     * @var iterable
19
     */
20
    protected $config;
21
22
    /**
23
     * @var CacheItemPoolInterface
24
     */
25
    protected $pool;
26
27
    /**
28
     * Router constructor.
29
     *
30
     * @param iterable $data
31
     * @param CacheItemPoolInterface   $pool
32
     */
33 5
    public function __construct($data, CacheItemPoolInterface $pool = null)
34
    {
35 5
        $this->config = $data;
36 5
        $this->pool = $pool;
37 5
    }
38
39
    /**
40
     * @return Route
41
     *
42
     * @throws Exceptions\NotFound\Data
43
     * @throws Exceptions\NotFound\Path
44
     */
45
    public function getCurrentRoute(): Route
46
    {
47
        return $this->find(Server::sharedInstance()->path());
48
    }
49
50
    /**
51
     * @param string $path
52
     * @param string $host
53
     * @param string $protocol
54
     *
55
     * @return Route
56
     * @throws Exceptions\NotFound\Data
57
     * @throws Exceptions\NotFound\Path
58
     */
59 3
    public function getRoute(string $path, string $host = null, string $protocol = null): Route
60
    {
61 3
        return $this->find(Server::url($path, $host, $protocol));
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    protected function hash(): string
68
    {
69
        return \sha1(\json_encode($this->config));
70
    }
71
72
    /**
73
     * @return array
74
     * @throws
75
     */
76 5
    protected function loadingRoutes(): array
77
    {
78 5
        $loader = new Loader($this->config);
79 5
        $this->routes = $loader->simplify();
80
81 3
        if ($this->pool) {
82
            $item = $this->pool->getItem($this->hash());
83
            $item->set($this->routes);
84
            $this->pool->save($item);
85
        }
86
87 3
        return $this->routes;
88
    }
89
90
    /**
91
     * @return array
92
     * @throws
93
     */
94 5
    protected function bootRoutes(): array
95
    {
96 5
        if ($this->pool) {
97
            $item = $this->pool->getItem($this->hash());
98
            $data = $item->get();
99
            if ($data) {
100
                return $data;
101
            }
102
        }
103
104 5
        return $this->loadingRoutes();
105
    }
106
107
    /**
108
     * @return Route[]
109
     */
110 5
    public function routes(): array 
111
    {
112 5
        if (empty($this->routes)) {
113 5
            $this->routes = $this->bootRoutes();
114
        }
115
116 3
        return $this->routes;
117
    }
118
119
    /**
120
     * @param string $path
121
     *
122
     * @return Route
123
     *
124
     * @throws Exceptions\NotFound\Path
125
     */
126
    public function route(string $path): Route
127
    {
128
        $routes = $this->routes();
129
130
        if (empty($routes[$path])) {
131
            throw new Exceptions\NotFound\Path('Route `' . $path . '` not found');
132
        }
133
134
        return $routes[$path];
135
    }
136
137
    /**
138
     * @param string $subject
139
     * @return Route
140
     */
141 3
    protected function find(string $subject): Route
142
    {
143 3
        foreach ($this->routes() as $name => $patternRule) {
144 3
            $match = new Match($patternRule, $subject, Server::sharedInstance()->method());
145 3
            if ($match->isTest()) {
146 3
                return new Route($match);
147
            }
148
        }
149
150 1
        throw new Exceptions\NotFound\Page('Page `' . $subject . '` not found', 404);
151
    }
152
153
}
154