Passed
Push — development ( 555989...6305db )
by Alexander
03:13
created

Route   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Test Coverage

Coverage 95.56%

Importance

Changes 7
Bugs 1 Features 0
Metric Value
eloc 47
c 7
b 1
f 0
dl 0
loc 189
ccs 43
cts 45
cp 0.9556
rs 10
wmc 22

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getUri() 0 3 1
A getAdapter() 0 10 2
A parseUri() 0 11 2
A execNotFoundHandler() 0 7 3
A getTemplate() 0 3 1
A getParams() 0 3 1
A process() 0 12 3
A getController() 0 9 2
A isAjax() 0 3 1
A getActionName() 0 7 2
A getName() 0 3 1
A init() 0 15 3
1
<?php
2
/**
3
 * Routing class
4
 *
5
 * @file      Route.php
6
 *
7
 * PHP version 7.1+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2020 Alexander Yancharuk
11
 * @date      Сбт Июн 23 08:52:41 2012
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Routing;
17
18
use Exception;
19
use Veles\Controllers\BaseController;
20
use Veles\Controllers\RestApiController;
21
use Veles\Request\HttpRequestAbstract;
22
use Veles\Request\Validator\ValidatorInterface;
23
use Veles\View\Adapters\ViewAdapterAbstract;
24
25
/**
26
 * Class Route
27
 *
28
 * @author  Alexander Yancharuk <alex at itvault dot info>
29
 */
30
class Route extends RouteBase
31
{
32
	protected $name;
33
	/** @var  array Current route config */
34
	protected $config;
35
	protected $template;
36
	protected $params = [];
37
	/** @var  ValidatorInterface */
38
	protected $validator;
39
	/** @var  HttpRequestAbstract */
40
	protected $request;
41
	/** @var string */
42
	protected $uri = '';
43
44
	/**
45
	 * Config parser and controller vars initialisation
46
	 *
47
	 * @throws Exception
48
	 */
49 22
	public function init(): self
50
	{
51 22
		[$uri, $section] = $this->parseUri();
52 22
		$this->uri = $uri;
53 22
		$routes    = $this->getConfigHandler()->getSection($section);
54
55 22
		foreach ($routes as $name => $route) {
56 20
			if (!$route['class']::check($route['route'], $uri)) {
57 4
				continue;
58
			}
59
60 20
			$this->process($name, $route);
61
		}
62
63 22
		return $this->execNotFoundHandler();
64
	}
65
66
	/**
67
	 * Get current URI
68
	 *
69
	 * @return string
70
	 */
71
	public function getUri(): string
72
	{
73
		return $this->uri;
74
	}
75
76
	/**
77
	 * Process route
78
	 *
79
	 * @param $name
80
	 * @param array $config
81
	 */
82 18
	protected function process($name, array $config): void
83
	{
84 18
		$this->config = $config;
85 18
		$this->name   = $name;
86
87 18
		if (isset($config['tpl'])) {
88 14
			$this->template = $config['tpl'];
89
		}
90
91 18
		if (RouteRegex::class === $config['class']) {
92
			/** @noinspection PhpUndefinedMethodInspection */
93 12
			$this->params = $config['class']::getParams();
94
		}
95 18
	}
96
97
	/**
98
	 * Safe way to get uri
99
	 *
100
	 * @return array
101
	 * @codeCoverageIgnore
102
	 */
103
	protected function parseUri(): array
104
	{
105
		$uri = parse_url(
106
			filter_input(INPUT_SERVER, 'REQUEST_URI'),
107
			PHP_URL_PATH
108
		);
109
110
		$parts   = explode('/', $uri);
111
		$section = isset($parts[2]) ? $parts[1] : '';
112
113
		return [$uri, $section];
114
	}
115
116
	/**
117
	 * Not found exception handler
118
	 *
119
	 * @return $this
120
	 */
121 8
	protected function execNotFoundHandler(): self
122
	{
123 8
		if (null === $this->config && null !== $this->ex404) {
124 2
			throw new $this->ex404;
125
		}
126
127 6
		return $this;
128
	}
129
130
	/**
131
	 * Getting ajax-flag
132
	 *
133
	 * @return bool
134
	 * @throws Exception
135
	 */
136 4
	public function isAjax(): bool
137
	{
138 4
		return isset($this->config['ajax']);
139
	}
140
141
	/**
142
	 * Build and return controller object
143
	 *
144
	 * @return BaseController|RestApiController
145
	 * @throws Exception
146
	 */
147 4
	public function getController()
148
	{
149 4
		if (!isset($this->config['controller'])) {
150 2
			throw new Exception('Controller name not set!');
151
		}
152
153 2
		$controller = 'Controllers\\' . $this->config['controller'];
154
155 2
		return new $controller;
156
	}
157
158
	/**
159
	 * Get controller method name
160
	 *
161
	 * @return string
162
	 * @throws Exception
163
	 */
164 4
	public function getActionName(): string
165
	{
166 4
		if (!isset($this->config['action'])) {
167 2
			throw new Exception('Action not set!');
168
		}
169
170 2
		return $this->config['action'];
171
	}
172
173
	/**
174
	 * Get View adapter class
175
	 *
176
	 * @return ViewAdapterAbstract
177
	 * @throws Exception
178
	 */
179 4
	public function getAdapter()
180
	{
181 4
		if (!isset($this->config['view'])) {
182 2
			throw new Exception('Route adapter not set!');
183
		}
184
185
		/** @var ViewAdapterAbstract $adapter_name */
186 2
		$adapter_name = $this->config['view'];
187
188 2
		return $adapter_name::instance();
189
	}
190
191
	/**
192
	 * Getting route name
193
	 *
194
	 * @return string
195
	 */
196 2
	public function getName(): string
197
	{
198 2
		return $this->name;
199
	}
200
201
	/**
202
	 * Getting URL-params
203
	 *
204
	 * @return array
205
	 */
206 12
	public function getParams(): array
207
	{
208 12
		return $this->params;
209
	}
210
211
	/**
212
	 * Return template path
213
	 *
214
	 * @return string
215
	 */
216 2
	public function getTemplate(): string
217
	{
218 2
		return $this->template;
219
	}
220
}
221