Completed
Push — master ( c23832...c6d61a )
by Alexander
05:59 queued 03:03
created

Route::checkAjax()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A Route::isAjax() 0 4 2
1
<?php
2
/**
3
 * Routing class
4
 *
5
 * @file      Route.php
6
 *
7
 * PHP version 5.6+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2016 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\Application\Application;
20
use Veles\Request\HttpRequestAbstract;
21
use Veles\Request\Validator\Validator;
22
use Veles\Request\Validator\ValidatorInterface;
23
24
/**
25
 * Class Route
26
 *
27
 * @author  Alexander Yancharuk <alex at itvault dot info>
28
 * @TODO Decrease class responsibility by creating separate class for request
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
42
	/**
43
	 * Config parser and controller vars initialisation
44
	 *
45
	 * @throws Exception
46
	 */
47 11
	public function init()
48
	{
49 11
		list($uri, $section) = $this->parseUri();
50 11
		$routes = $this->getConfigHandler()->getSection($section);
51
52 11
		foreach ($routes as $name => $route) {
53
			/** @noinspection PhpUndefinedMethodInspection */
54 10
			if (!$route['class']::check($route['route'], $uri)) {
55 2
				continue;
56
			}
57
58 10
			$this->config = $route;
59 10
			$this->name   = $name;
60
61 10
			if (isset($route['tpl'])) {
62 8
				$this->template = $route['tpl'];
63 8
			}
64
65 10
			if ('Veles\Routing\RouteRegex' === $route['class']) {
66
				/** @noinspection PhpUndefinedMethodInspection */
67 6
				$this->params = $route['class']::getParams();
68 6
			}
69 11
		}
70
71 11
		return $this->execNotFoundHandler();
72
	}
73
74
	/**
75
	 * Safe way to get uri
76
	 *
77
	 * @return array
78
	 * @codeCoverageIgnore
79
	 */
80
	protected function parseUri()
81
	{
82
		$uri = parse_url(
83
			filter_input(INPUT_SERVER, 'REQUEST_URI'), PHP_URL_PATH
84
		);
85
86
		$parts   = explode('/', $uri);
87
		$section = isset($parts[2]) ? $parts[1] : '';
88
89
		return [$uri, $section];
90
	}
91
92
	/**
93
	 * Not found exception handler
94
	 *
95
	 * @return $this
96
	 */
97 4
	protected function execNotFoundHandler()
98
	{
99 4
		if (null === $this->config && null !== $this->ex404) {
100 1
			throw new $this->ex404;
101
		}
102
103 3
		return $this;
104
	}
105
106
	/**
107
	 * Getting ajax-flag
108
	 *
109
	 * @throws Exception
110
	 * @return bool
111
	 */
112 2
	public function isAjax()
113
	{
114 2
		return isset($this->config['ajax']) ? true : false;
115
	}
116
117
	/**
118
	 * Build and return controller object
119
	 *
120
	 * @param Application $application
121
	 *
122
	 * @return object
123
	 * @throws Exception
124
	 */
125 2
	public function getController(Application $application)
126
	{
127 2
		if (!isset($this->config['controller'])) {
128 1
			throw new Exception('Controller name not set!');
129
		}
130
131 1
		if (isset($this->config['validator'])) {
132 1
			$validator = (new Validator)->setAdapter(new $this->config['validator']);
133 1
			$request   = (new $this->config['request'])->setValidator($validator);
134
135 1
			$application->setRequest($request);
136 1
		}
137
138 1
		$controller = 'Controllers\\' . $this->config['controller'];
139
140 1
		return new $controller($application);
141
	}
142
143
	/**
144
	 * Get controller method name
145
	 *
146
	 * @throws Exception
147
	 * @return string
148
	 */
149 2
	public function getActionName()
150
	{
151 2
		if (!isset($this->config['action'])) {
152 1
			throw new Exception('Action not set!');
153
		}
154
155 1
		return $this->config['action'];
156
	}
157
158
	/**
159
	 * Get View adapter class
160
	 *
161
	 * @return \Veles\View\Adapters\ViewAdapterAbstract
162
	 * @throws \Exception
163
	 */
164 2
	public function getAdapter()
165
	{
166 2
		if (!isset($this->config['view'])) {
167 1
			throw new Exception('Route adapter not set!');
168
		}
169
170
		/** @var \Veles\View\Adapters\ViewAdapterAbstract $adapter_name */
171 1
		$adapter_name = $this->config['view'];
172 1
		return $adapter_name::instance();
173
	}
174
175
	/**
176
	 * Getting route name
177
	 *
178
	 * @return string
179
	 */
180 1
	public function getName()
181
	{
182 1
		return $this->name;
183
	}
184
185
	/**
186
	 * Getting URL-params
187
	 *
188
	 * @return array
189
	 */
190 6
	public function getParams()
191
	{
192 6
		return $this->params;
193
	}
194
195
	/**
196
	 * Return template path
197
	 */
198 1
	public function getTemplate()
199
	{
200 1
		return $this->template;
201
	}
202
}
203