Completed
Push — master ( 3f0917...ad69cb )
by Henry
07:39
created

Router::_messengerFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Redaxscript\Router;
3
4
use Redaxscript\Controller;
5
use Redaxscript\Filter;
6
use Redaxscript\Header;
7
use Redaxscript\Messenger;
8
use Redaxscript\Model;
9
use Redaxscript\Module;
10
use Redaxscript\View;
11
12
/**
13
 * parent class to provide the router
14
 *
15
 * @since 3.3.0
16
 *
17
 * @package Redaxscript
18
 * @category Router
19
 * @author Henry Ruhs
20
 */
21
22
class Router extends RouterAbstract
23
{
24
	/**
25
	 * route the header
26
	 *
27
	 * @since 3.3.0
28
	 *
29
	 * @return bool
30
	 */
31
32 3
	public function routeHeader() : bool
33
	{
34 3
		Module\Hook::trigger('routeHeader');
35
36
		/* handle break */
37
38 3
		if ($this->_registry->get('routerBreak'))
39
		{
40 1
			$this->_registry->set('contentError', false);
41
		}
42
43
		/* handle post */
44
45 3
		if ($this->_request->getPost('Redaxscript\View\SearchForm'))
46
		{
47 1
			return $this->_redirectSearch();
48
		}
49 2
		return $this->_registry->get('routerBreak');
50
	}
51
52
	/**
53
	 * route the content
54
	 *
55
	 * @since 3.3.0
56
	 *
57
	 * @return string|bool
58
	 */
59
60 17
	public function routeContent()
61
	{
62 17
		Module\Hook::trigger('routeContent');
63 17
		$firstParameter = $this->getFirst();
64 17
		$fileInstall = $this->_registry->get('file') === 'install.php' && $this->_config->get('env') !== 'production';
65
66
		/* handle guard */
67
68 17
		if ($this->_tokenGuard())
69
		{
70 1
			return $this->_errorToken();
71
		}
72
73
		/* handle post */
74
75 16
		if ($this->_request->getPost('Redaxscript\View\CommentForm'))
76
		{
77 1
			return $this->_processComment();
78
		}
79 15
		if ($this->_request->getPost('Redaxscript\View\LoginForm'))
80
		{
81 1
			return $this->_processLogin();
82
		}
83 14
		if ($this->_request->getPost('Redaxscript\View\ResetForm'))
84
		{
85 1
			return $this->_processReset();
86
		}
87 13
		if ($this->_request->getPost('Redaxscript\View\RecoverForm'))
88
		{
89 1
			return $this->_processRecover();
90
		}
91 12
		if ($this->_request->getPost('Redaxscript\View\RegisterForm'))
92
		{
93 1
			return $this->_processRegister();
94
		}
95 11
		if ($fileInstall && $this->_request->getPost('Redaxscript\View\InstallForm'))
96
		{
97 1
			return $this->_processInstall();
98
		}
99
100
		/* handle route */
101
102 10
		if ($firstParameter === 'search')
103
		{
104 1
			return $this->_processSearch();
105
		}
106 9
		if ($firstParameter === 'login')
107
		{
108 4
			return $this->_renderLogin();
109
		}
110 5
		if ($firstParameter === 'logout')
111
		{
112 1
			return $this->_processLogout();
113
		}
114 4
		if ($firstParameter === 'register')
115
		{
116 2
			return $this->_renderRegister();
117
		}
118 2
		if ($fileInstall)
119
		{
120 1
			return $this->_renderInstall();
121
		}
122 1
		return $this->_registry->get('routerBreak');
123
	}
124
125
	/**
126
	 * token guard
127
	 *
128
	 * @since 3.3.0
129
	 *
130
	 * @return bool
131
	 */
132
133 17
	protected function _tokenGuard() : bool
134
	{
135 17
		return $this->_request->getPost() && $this->_request->getPost('token') !== $this->_registry->get('token');
136
	}
137
138
	/**
139
	 * redirect the search
140
	 *
141
	 * @since 3.3.0
142
	 *
143
	 * @return bool
144
	 */
145
146 1
	protected function _redirectSearch() : bool
147
	{
148 1
		$aliasFilter = new Filter\Alias();
149 1
		$root = $this->_registry->get('root');
150 1
		$parameterRoute = $this->_registry->get('parameterRoute');
151
152
		/* handle post */
153
154 1
		$table = $aliasFilter->sanitize($this->_request->getPost('table'));
155 1
		$search = $aliasFilter->sanitize($this->_request->getPost('search'));
156 1
		$tableString = $table ? '/' . $table : null;
157
158
		/* redirect */
159
160 1
		return Header::doRedirect($root . '/' . $parameterRoute . 'search' . $tableString . '/' . $search);
161
	}
162
163
	/**
164
	 * process the search
165
	 *
166
	 * @since 3.3.0
167
	 *
168
	 * @return string
169
	 */
170
171 1
	protected function _processSearch() : string
172
	{
173 1
		$searchController = new Controller\Search($this->_registry, $this->_request, $this->_language, $this->_config);
174 1
		return $searchController->process();
175
	}
176
177
	/**
178
	 * process the comment
179
	 *
180
	 * @since 3.3.0
181
	 *
182
	 * @return string
183
	 */
184
185 1
	protected function _processComment() : string
186
	{
187 1
		$commentController = new Controller\Comment($this->_registry, $this->_request, $this->_language, $this->_config);
188 1
		return $commentController->process();
189
	}
190
191
	/**
192
	 * process the login
193
	 *
194
	 * @since 3.3.0
195
	 *
196
	 * @return string
197
	 */
198
199 1
	protected function _processLogin() : string
200
	{
201 1
		$loginController = new Controller\Login($this->_registry, $this->_request, $this->_language, $this->_config);
202 1
		return $loginController->process();
203
	}
204
205
	/**
206
	 * process the reset
207
	 *
208
	 * @since 3.3.0
209
	 *
210
	 * @return string
211
	 */
212
213 1
	protected function _processReset() : string
214
	{
215 1
		$resetController = new Controller\Reset($this->_registry, $this->_request, $this->_language, $this->_config);
216 1
		return $resetController->process();
217
	}
218
219
	/**
220
	 * process the recover
221
	 *
222
	 * @since 3.3.0
223
	 *
224
	 * @return string
225
	 */
226
227 1
	protected function _processRecover() : string
228
	{
229 1
		$recoverController = new Controller\Recover($this->_registry, $this->_request, $this->_language, $this->_config);
230 1
		return $recoverController->process();
231
	}
232
233
	/**
234
	 * process the register
235
	 *
236
	 * @since 3.3.0
237
	 *
238
	 * @return string
239
	 */
240
241 1
	protected function _processRegister() : string
242
	{
243 1
		$registerController = new Controller\Register($this->_registry, $this->_request, $this->_language, $this->_config);
244 1
		return $registerController->process();
245
	}
246
247
	/**
248
	 * process the logout
249
	 *
250
	 * @since 3.3.0
251
	 *
252
	 * @return string
253
	 */
254
255 1
	protected function _processLogout() : string
256
	{
257 1
		$logoutController = new Controller\Logout($this->_registry, $this->_request, $this->_language, $this->_config);
258 1
		return $logoutController->process();
259
	}
260
261
	/**
262
	 * process the install
263
	 *
264
	 * @since 3.3.0
265
	 *
266
	 * @return string
267
	 */
268
269 1
	protected function _processInstall() : string
270
	{
271 1
		$this->_request->setSession('installArray',
272
		[
273 1
			'dbType' => $this->_request->getPost('db-type'),
274 1
			'dbHost' => $this->_request->getPost('db-host'),
275 1
			'dbName' => $this->_request->getPost('db-name'),
276 1
			'dbUser' => $this->_request->getPost('db-user'),
277 1
			'dbPassword' => $this->_request->getPost('db-password'),
278 1
			'dbPrefix' => $this->_request->getPost('db-prefix'),
279 1
			'adminName' => $this->_request->getPost('admin-name'),
280 1
			'adminUser' => $this->_request->getPost('admin-user'),
281 1
			'adminPassword' => $this->_request->getPost('admin-password'),
282 1
			'adminEmail' => $this->_request->getPost('admin-email')
283
		]);
284 1
		$installController = new Controller\Install($this->_registry, $this->_request, $this->_language, $this->_config);
285 1
		return $installController->process();
286
	}
287
288
	/**
289
	 * render the login
290
	 *
291
	 * @since 3.3.0
292
	 *
293
	 * @return string
294
	 */
295
296 4
	protected function _renderLogin() : string
297
	{
298 4
		$secondParameter = $this->getSecond();
299 4
		$thirdParameter = $this->getThird();
300 4
		$thirdSubParameter = $this->getThirdSub();
301 4
		$settingModel = new Model\Setting();
302
303
		/* handle login */
304
305 4
		if ((int)$settingModel->get('recovery') === 1)
306
		{
307 2
			if ($secondParameter === 'recover')
308
			{
309 1
				$recoverForm = new View\RecoverForm($this->_registry, $this->_language);
310 1
				return $recoverForm->render();
311
			}
312 1
			if ($secondParameter === 'reset' && $thirdParameter && $thirdSubParameter)
313
			{
314 1
				$resetForm = new View\ResetForm($this->_registry, $this->_language);
315 1
				return $resetForm->render();
316
			}
317
		}
318 2
		if (!$secondParameter)
319
		{
320 1
			$loginForm = new View\LoginForm($this->_registry, $this->_language);
321 1
			return $loginForm->render();
322
		}
323 1
		return $this->_errorAccess();
324
	}
325
326
	/**
327
	 * render the register
328
	 *
329
	 * @since 3.3.0
330
	 *
331
	 * @return string
332
	 */
333
334 2
	protected function _renderRegister() : string
335
	{
336 2
		$settingModel = new Model\Setting();
337 2
		if ((int)$settingModel->get('registration') === 1)
338
		{
339 1
			$registerForm = new View\RegisterForm($this->_registry, $this->_language);
340 1
			return $registerForm->render();
341
		}
342 1
		return $this->_errorAccess();
343
	}
344
345
	/**
346
	 * render the install
347
	 *
348
	 * @since 3.3.0
349
	 *
350
	 * @return string
351
	 */
352
353 1
	protected function _renderInstall() : string
354
	{
355 1
		$installArray = $this->_request->getSession('installArray');
356 1
		$systemStatus = new View\SystemStatus($this->_registry, $this->_language);
357 1
		$installForm = new View\InstallForm($this->_registry, $this->_language);
358 1
		return $systemStatus->render() . $installForm->render($installArray ? $installArray : []);
359
	}
360
361
	/**
362
	 * messenger factory
363
	 *
364
	 * @since 4.0.0
365
	 *
366
	 * @return Messenger
367
	 */
368
369 1
	protected function _messengerFactory()
370
	{
371 1
		return new Messenger($this->_registry);
372
	}
373 1
374 1
	/**
375
	 * show the token error
376
	 *
377
	 * @since 3.3.0
378
	 *
379
	 * @return string
380
	 */
381
382
	protected function _errorToken() : string
383
	{
384
		$messenger = $this->_messengerFactory();
385 2
		return $messenger
386
			->setUrl($this->_language->get('home'), $this->_registry->get('root'))
387 2
			->error($this->_language->get('token_incorrect'), $this->_language->get('error_occurred'));
388
	}
389 2
390 2
	/**
391
	 * show the access error
392
	 *
393
	 * @since 3.3.0
394
	 *
395
	 * @return string
396
	 */
397
398
	protected function _errorAccess() : string
399
	{
400
		$messenger = $this->_messengerFactory();
401
		return $messenger
402
			->setUrl($this->_language->get('home'), $this->_registry->get('root'))
403
			->error($this->_language->get('access_no'), $this->_language->get('error_occurred'));
404
	}
405
}
406