Completed
Branchmaster (48c558)
created

Install::process()   D

↳ Parent: Install

Complexity

Conditions 9
Paths 12

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 133
Code Lines 66

Code Coverage

Tests 56
CRAP Score 9.4453

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 133
ccs 56
cts 68
cp 0.8235
rs 4.8196
c 1
b 0
f 0
cc 9
eloc 66
nc 12
nop 0
crap 9.4453

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Redaxscript\Controller;
3
4
use Redaxscript\Config;
5
use Redaxscript\Db;
6
use Redaxscript\Filter;
7
use Redaxscript\Html;
8
use Redaxscript\Installer;
9
use Redaxscript\Language;
10
use Redaxscript\Mailer;
11
use Redaxscript\Messenger;
12
use Redaxscript\Registry;
13
use Redaxscript\Request;
14
use Redaxscript\Validator;
15
16
/**
17
 * children class to process install
18
 *
19
 * @since 3.0.0
20
 *
21
 * @package Redaxscript
22
 * @category Controller
23
 * @author Henry Ruhs
24
 * @author Balázs Szilágyi
25
 */
26
27
class Install extends ControllerAbstract
28
{
29
	/**
30
	 * instance of the config class
31
	 *
32
	 * @var object
33
	 */
34
35
	protected $_config;
36
37
	/**
38
	 * construct of the class
39
	 *
40
	 * @since 3.0.0
41
	 *
42
	 * @param Registry $registry
43
	 * @param Language $language
44
	 * @param Request $request
45
	 * @param Config $config
46
	 */
47
48 11
	public function __construct(Registry $registry, Language $language, Request $request, Config $config)
49
	{
50 11
		parent::__construct($registry, $language, $request);
51 11
		$this->_config = $config;
52 11
	}
53
54
	/**
55
	 * process the class
56
	 *
57
	 * @since 3.0.0
58
	 *
59
	 * @return string
60
	 */
61
62 3
	public function process()
63
	{
64 3
		$specialFilter = new Filter\Special();
65 3
		$emailFilter = new Filter\Email();
66
67
		/* process post */
68
69
		$postArray =
70
		[
71 3
			'dbType' => $this->_request->getPost('db-type'),
72 3
			'dbHost' => $this->_request->getPost('db-host'),
73 3
			'dbName' => $this->_request->getPost('db-name'),
74 3
			'dbUser' => $this->_request->getPost('db-user'),
75 3
			'dbPassword' => $this->_request->getPost('db-password'),
76 3
			'dbPrefix' => $this->_request->getPost('db-prefix'),
77 3
			'dbSalt' => $this->_request->getPost('db-salt'),
78 3
			'adminName' => $specialFilter->sanitize($this->_request->getPost('admin-name')),
79 3
			'adminUser' => $specialFilter->sanitize($this->_request->getPost('admin-user')),
80 3
			'adminPassword' => $specialFilter->sanitize($this->_request->getPost('admin-password')),
81 3
			'adminEmail' => $emailFilter->sanitize($this->_request->getPost('admin-email')),
82 3
			'refreshConnection' => $this->_request->getPost('refresh-connection')
83 3
		];
84
85
		/* handle error */
86
87 3
		$messageArray = $this->_validateDatabase($postArray);
88
		if ($messageArray)
0 ignored issues
show
Bug Best Practice introduced by JimMorrison723
The expression $messageArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
89 3
		{
90 1
			return $this->_error(
91
			[
92 1
                'url' => 'install.php',
93
				'title' => $this->_language->get('database'),
94 1
				'message' => $messageArray
95
			]);
96 2
		}
97
		$messageArray = $this->_validateAccount($postArray);
98 2
		if ($messageArray)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $messageArray of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
99 1
		{
100
			return $this->_error(
101 1
			[
102
                'url' => 'install.php',
103 1
				'title' => $this->_language->get('account'),
104
				'message' => $messageArray
105
			]);
106
		}
107
108
		/* handle success */
109
110 1
		$configArray =
111 1
		[
112 1
			'dbType' => $postArray['dbType'],
113 1
			'dbHost' => $postArray['dbHost'],
114 1
			'dbName' => $postArray['dbName'],
115 1
			'dbUser' => $postArray['dbUser'],
116 1
			'dbPassword' => $postArray['dbPassword'],
117 1
			'dbPrefix' => $postArray['dbPrefix'],
118
			'dbSalt' => $postArray['dbSalt']
119
		];
120 1
		$adminArray =
121 1
		[
122 1
			'adminUser' => $postArray['adminUser'],
123 1
			'adminName' => $postArray['adminName'],
124 1
			'adminEmail' => $postArray['adminEmail'],
125
			'adminPassword' => $postArray['adminPassword']
126
		];
127
128 1
		/* touch file */
129 1
130
		if (!$this->_touch($configArray))
131
		{
132
			return $this->_error(
133
			[
134
				'url' => 'install.php',
135
				'message' => $this->_language->get('directory_permission_grant') . $this->_language->get('point')
136
			]);
137
		}
138 1
139 1
		/* write config */
140
141
		if (!$this->_write($configArray))
142
		{
143
			return $this->_error(
144
			[
145 1
				'url' => 'install.php',
146 1
				'message' => $this->_language->get('file_permission_grant') . $this->_language->get('colon') . ' config.php'
147
			]);
148
		}
149
150
		/* refresh connection */
151
152
		if ($postArray['refreshConnection'])
153
		{
154
			$this->_refreshConnection();
155
		}
156 1
157 1
		/* get status */
158 1
159
		if (!$this->_getStatus())
160 1
		{
161 1
			return $this->_error(
162 1
			[
163
				'url' => 'install.php',
164
				'message' => $this->_language->get('database_failed')
165
			]);
166
		}
167
168
		/* install */
169
170
		if (!$this->_install($adminArray))
171
		{
172
			return $this->_error(
173
			[
174
				'url' => 'install.php',
175
				'message' => $this->_language->get('installation_failed')
176
			]);
177
		}
178
179
		/* mail */
180 1
181
		if (!$this->_mail($adminArray))
182 1
		{
183
			return $this->_warning(
184 1
			[
185 1
				'url' => $this->_registry->get('root'),
186 1
				'message' => $this->_language->get('email_failed')
187
			]);
188
		}
189
		return $this->_success(
190
		[
191
			'url' => $this->_registry->get('root'),
192
			'message' => $this->_language->get('installation_completed')
193
		]);
194
	}
195
196
	/**
197
	 * show the success
198
	 *
199 2
	 * @since 3.0.0
200
	 *
201 2
	 * @param array $successArray array of the success
202
	 *
203 2
	 * @return string
204 2
	 */
205
206
	protected function _success($successArray = [])
207
	{
208
		$messenger = new Messenger($this->_registry);
209
		return $messenger
210
			->setUrl($this->_language->get('home'), $successArray['url'])
211
			->doRedirect()
212
			->success($successArray['message'], $successArray['title']);
213
	}
214
215
	/**
216
	 * show the warning
217 6
	 *
218
	 * @since 3.0.0
219 6
	 *
220 6
	 * @param array $warningArray array of the warning
221 6
	 *
222 2
	 * @return string
223 2
	 */
224 6
225 6
	protected function _warning($warningArray = [])
226 4
	{
227 4
		$messenger = new Messenger($this->_registry);
228 6
		return $messenger
229 6
			->setUrl($this->_language->get('home'), $warningArray['url'])
230 3
			->doRedirect()
231 3
			->warning($warningArray['message'], $warningArray['title']);
232 3
	}
233 3
234 3
	/**
235 3
	 * show the error
236 3
	 *
237 3
	 * @since 3.0.0
238 3
	 *
239 6
	 * @param array $errorArray array of the error
240
	 *
241
	 * @return string
242
	 */
243
244
	protected function _error($errorArray = [])
245
	{
246
		$messenger = new Messenger($this->_registry);
247
		return $messenger
248
			->setUrl($this->_language->get('back'), $errorArray['url'])
249
			->error($errorArray['message'], $errorArray['title']);
250
	}
251
252 5
	/**
253
	 * validate the database
254 5
	 *
255 5
	 * @since 3.0.0
256
	 *
257
	 * @param array $postArray array to be validated
258
	 *
259 5
	 * @return array
260 5
	 */
261 5
262 3
	protected function _validateDatabase($postArray = [])
263 3
	{
264 5
		$messageArray = [];
265 5
		if (!$postArray['dbType'])
266 2
		{
267 2
			$messageArray[] = $this->_language->get('type_empty');
268 3
		}
269 3
		if (!$postArray['dbHost'])
270 1
		{
271 1
			$messageArray[] = $this->_language->get('host_empty');
272 5
		}
273 5
		if ($postArray['dbType'] !== 'sqlite')
274 2
		{
275 2
			if (!$postArray['dbName'])
276 3
			{
277 3
				$messageArray[] = $this->_language->get('name_empty');
278 1
			}
279 1
			if (!$postArray['dbUser'])
280 5
			{
281 5
				$messageArray[] = $this->_language->get('user_empty');
282 2
			}
283 2
		}
284 3
		return $messageArray;
285 3
	}
286 1
287 1
	/**
288 5
	 * validate the account
289
	 *
290
	 * @since 3.0.0
291
	 *
292
	 * @param array $postArray array to be validated
293
	 *
294
	 * @return array
295
	 */
296
297
	protected function _validateAccount($postArray = [])
298
	{
299
		$emailValidator = new Validator\Email();
300
		$loginValidator = new Validator\Login();
301 1
302
		/* validate post */
303 1
304 1
		$messageArray = [];
305 1
		if (!$postArray['adminName'])
306 1
		{
307 1
			$messageArray[] = $this->_language->get('name_empty');
308 1
		}
309 1
		if (!$postArray['adminUser'])
310 1
		{
311
			$messageArray[] = $this->_language->get('user_empty');
312
		}
313
		else if ($loginValidator->validate($postArray['adminUser']) === Validator\ValidatorInterface::FAILED)
314
		{
315
			$messageArray[] = $this->_language->get('user_incorrect');
316
		}
317
		if (!$postArray['adminPassword'])
318
		{
319
			$messageArray[] = $this->_language->get('password_empty');
320
		}
321
		else if ($loginValidator->validate($postArray['adminPassword']) === Validator\ValidatorInterface::FAILED)
322
		{
323
			$messageArray[] = $this->_language->get('password_incorrect');
324
		}
325
		if (!$postArray['adminEmail'])
326
		{
327
			$messageArray[] = $this->_language->get('email_empty');
328
		}
329
		else if ($emailValidator->validate($postArray['adminEmail']) === Validator\ValidatorInterface::FAILED)
330
		{
331
			$messageArray[] = $this->_language->get('email_incorrect');
332
		}
333
		return $messageArray;
334
	}
335
336
	/**
337 3
	 * touch sqlite file
338
	 *
339 3
	 * @since 3.0.0
340 3
	 *
341 3
	 * @param array $configArray
342 3
	 *
343 3
	 * @return boolean
344 3
	 */
345 2
346 2
	protected function _touch($configArray = [])
347 2
	{
348 2
		if ($configArray['dbType'] === 'sqlite')
349 2
		{
350
			$file = $configArray['dbHost'] . '.tmp';
351 2
			return touch($file) && unlink($file);
352 2
		}
353 2
		return true;
354 2
	}
355 2
356 2
	/**
357
	 * write config file
358 1
	 *
359
	 * @since 3.0.0
360
	 *
361
	 * @param array $configArray
362
	 *
363
	 * @return boolean
364
	 */
365
366
	protected function _write($configArray = [])
367
	{
368
		$this->_config->set('dbType', $configArray['dbType']);
369
		$this->_config->set('dbHost', $configArray['dbHost']);
370
		$this->_config->set('dbName', $configArray['dbName']);
371 1
		$this->_config->set('dbUser', $configArray['dbUser']);
372
		$this->_config->set('dbPassword', $configArray['dbPassword']);
373
		$this->_config->set('dbPrefix', $configArray['dbPrefix']);
374
		$this->_config->set('dbSalt', $configArray['dbSalt']);
375 1
		return $this->_config->write();
376
	}
377 1
378
	/**
379 1
	 * get the status
380 1
	 *
381 1
	 * @since 3.0.0
382
	 *
383
	 * @return boolean
384
	 */
385
386
	protected function _getStatus()
387 1
	{
388 1
		return Db::getStatus();
389
	}
390
391 1
	/**
392 1
	 * refresh the connection
393 1
	 *
394
	 * @since 3.0.0
395
	 *
396 1
	 * @return array
397 1
	 */
398 1
399 1
	protected function _refreshConnection()
400 1
	{
401 1
		Db::init();
402
		Db::resetDb();
403
	}
404
405 1
	/**
406 1
	 * install the database
407 1
	 *
408
	 * @since 3.0.0
409
	 *
410
	 * @param array $installArray
411
	 *
412
	 * @return boolean
413
	 */
414
415
	protected function _install($installArray = [])
416
	{
417
		$adminName = $installArray['adminName'];
418
		$adminUser = $installArray['adminUser'];
419
		$adminPassword = $installArray['adminPassword'];
420
		$adminEmail = $installArray['adminEmail'];
421
		if ($adminName && $adminUser && $adminPassword && $adminEmail)
422
		{
423
			$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
424
			$installer->init();
425
			$installer->rawDrop();
426
			$installer->rawCreate();
427
			$installer->insertData(
428
			[
429
				'adminName' => $installArray['adminName'],
430
				'adminUser' => $installArray['adminUser'],
431
				'adminPassword' => $installArray['adminPassword'],
432
				'adminEmail' => $installArray['adminEmail']
433
			]);
434
			return $this->_getStatus() === 2;
435
		}
436
		return false;
437
	}
438
439
	/**
440
	 * send the mail
441
	 *
442
	 * @since 3.0.0
443
	 *
444
	 * @param array $mailArray
445
	 *
446
	 * @return boolean
447
	 */
448
449
	protected function _mail($mailArray = [])
450
	{
451
		/* html elements */
452
453
		$linkElement = new Html\Element();
454
		$linkElement
455
			->init('a',
456
			[
457
				'href' => $this->_registry->get('root')
458
			])
459
			->text($this->_registry->get('root'));
460
461
		/* prepare mail */
462
463
		$toArray =
464
		[
465
			$mailArray['adminName'] => $mailArray['adminEmail']
466
		];
467
		$fromArray =
468
		[
469
			Db::getSetting('author') => Db::getSetting('email')
470
		];
471
		$subject = $this->_language->get('installation');
472
		$bodyArray =
473
		[
474
			'<strong>' . $this->_language->get('user') . $this->_language->get('colon') . '</strong> ' . $mailArray['adminUser'],
475
			'<br />',
476
			'<strong>' . $this->_language->get('password') . $this->_language->get('colon') . '</strong> ' . $mailArray['adminPassword'],
477
			'<br />',
478
			'<strong>' . $this->_language->get('url') . $this->_language->get('colon') . '</strong> ' . $linkElement
479
		];
480
481
		/* send mail */
482
483
		$mailer = new Mailer();
484
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
485
		return $mailer->send();
486
	}
487
}