Install   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 449
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 96.27%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 15
dl 0
loc 449
ccs 155
cts 161
cp 0.9627
rs 9.28
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
C process() 0 116 9
A _sanitizePost() 0 22 1
B _validateDatabase() 0 24 6
B _validateAccount() 0 38 8
A _touch() 0 9 3
A _write() 0 10 1
A _getStatus() 0 4 1
A _refreshConnection() 0 5 1
A _install() 0 23 5
A _mail() 0 40 1
A _success() 0 8 1
A _warning() 0 8 1
A _error() 0 7 1
1
<?php
2
namespace Redaxscript\Controller;
3
4
use Redaxscript\Db;
5
use Redaxscript\Filter;
6
use Redaxscript\Html;
7
use Redaxscript\Installer;
8
use Redaxscript\Mailer;
9
use Redaxscript\Model;
10
use Redaxscript\Validator;
11
use function touch;
12
use function unlink;
13
14
/**
15
 * children class to process install
16
 *
17
 * @since 3.0.0
18
 *
19
 * @package Redaxscript
20
 * @category Controller
21
 * @author Henry Ruhs
22
 * @author Balázs Szilágyi
23
 */
24
25
class Install extends ControllerAbstract
26
{
27
	/**
28
	 * process the class
29
	 *
30
	 * @since 3.0.0
31
	 *
32
	 * @return string
33
	 */
34
35 8
	public function process() : string
36
	{
37 8
		$postArray = $this->_normalizePost($this->_sanitizePost());
38
39
		/* validate database */
40
41 8
		$validateArray = $this->_validateDatabase($postArray);
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 37 can also be of type null; however, Redaxscript\Controller\I...ll::_validateDatabase() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42 8
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $validateArray 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...
43
		{
44 1
			return $this->_error(
45
			[
46 1
				'url' => 'install.php',
47 1
				'title' => $this->_language->get('database'),
48 1
				'message' => $validateArray
49
			]);
50
		}
51
52
		/* validate account */
53
54 7
		$validateArray = $this->_validateAccount($postArray);
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 37 can also be of type null; however, Redaxscript\Controller\Install::_validateAccount() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55 7
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $validateArray 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...
56
		{
57 1
			return $this->_error(
58
			[
59 1
				'url' => 'install.php',
60 1
				'title' => $this->_language->get('account'),
61 1
				'message' => $validateArray
62
			]);
63
		}
64
65
		/* touch config */
66
67
		$configArray =
68
		[
69 6
			'dbType' => $postArray['dbType'],
70 6
			'dbHost' => $postArray['dbHost'],
71 6
			'dbName' => $postArray['dbName'],
72 6
			'dbUser' => $postArray['dbUser'],
73 6
			'dbPassword' => $postArray['dbPassword'],
74 6
			'dbPrefix' => $postArray['dbPrefix']
75
		];
76 6
		if (!$this->_touch($configArray))
77
		{
78 1
			return $this->_error(
79
			[
80 1
				'url' => 'install.php',
81 1
				'message' => $this->_language->get('directory_permission_grant') . $this->_language->get('point')
82
			]);
83
		}
84
85
		/* write config */
86
87 5
		if (!$this->_write($configArray))
88
		{
89 1
			return $this->_error(
90
			[
91 1
				'url' => 'install.php',
92 1
				'message' => $this->_language->get('file_permission_grant') . $this->_language->get('colon') . ' config.php'
93
			]);
94
		}
95
96
		/* refresh connection */
97
98 4
		if ($postArray['refreshConnection'])
99
		{
100
			$this->_refreshConnection();
101
		}
102
103
		/* handle database */
104
105 4
		if (!$this->_getStatus())
106
		{
107 1
			return $this->_error(
108
			[
109 1
				'url' => 'install.php',
110 1
				'message' => $this->_language->get('database_failed')
111
			]);
112
		}
113
114
		/* handle install */
115
116
		$adminArray =
117
		[
118 3
			'adminUser' => $postArray['adminUser'],
119 3
			'adminName' => $postArray['adminName'],
120 3
			'adminEmail' => $postArray['adminEmail'],
121 3
			'adminPassword' => $postArray['adminPassword']
122
		];
123 3
		if (!$this->_install($adminArray))
124
		{
125 1
			return $this->_error(
126
			[
127 1
				'url' => 'install.php',
128 1
				'message' => $this->_language->get('installation_failed')
129
			]);
130
		}
131
132
		/* handle mail */
133
134 2
		if (!$this->_mail($adminArray))
135
		{
136 1
			return $this->_warning(
137
			[
138 1
				'url' => 'index.php',
139 1
				'message' => $this->_language->get('email_failed')
140
			]);
141
		}
142
143
		/* handle success */
144
145 1
		return $this->_success(
146
		[
147 1
			'url' => 'index.php',
148 1
			'message' => $this->_language->get('installation_completed')
149
		]);
150
	}
151
152
	/**
153
	 * sanitize the post
154
	 *
155
	 * @since 4.0.0
156
	 *
157
	 * @return array
158
	 */
159
160 8
	protected function _sanitizePost() : array
161
	{
162 8
		$emailFilter = new Filter\Email();
163 8
		$specialFilter = new Filter\Special();
164
165
		/* sanitize post */
166
167
		return
168
		[
169 8
			'dbType' => $this->_request->getPost('db-type'),
170 8
			'dbHost' => $this->_request->getPost('db-host'),
171 8
			'dbName' => $this->_request->getPost('db-name'),
172 8
			'dbUser' => $this->_request->getPost('db-user'),
173 8
			'dbPassword' => $this->_request->getPost('db-password'),
174 8
			'dbPrefix' => $this->_request->getPost('db-prefix'),
175 8
			'adminName' => $specialFilter->sanitize($this->_request->getPost('admin-name')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('admin-name') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
176 8
			'adminUser' => $specialFilter->sanitize($this->_request->getPost('admin-user')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('admin-user') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
177 8
			'adminPassword' => $specialFilter->sanitize($this->_request->getPost('admin-password')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('admin-password') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Special::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
178 8
			'adminEmail' => $emailFilter->sanitize($this->_request->getPost('admin-email')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('admin-email') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Email::sanitize() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
179 8
			'refreshConnection' => $this->_request->getPost('refresh-connection')
180
		];
181
	}
182
183
	/**
184
	 * validate the database
185
	 *
186
	 * @since 3.0.0
187
	 *
188
	 * @param array $postArray array of the post
189
	 *
190
	 * @return array
191
	 */
192
193 11
	protected function _validateDatabase(array $postArray = []) : array
194
	{
195 11
		$validateArray = [];
196 11
		if (!$postArray['dbType'])
197
		{
198 2
			$validateArray[] = $this->_language->get('type_empty');
199
		}
200 11
		if (!$postArray['dbHost'])
201
		{
202 4
			$validateArray[] = $this->_language->get('host_empty');
203
		}
204 11
		if ($postArray['dbType'] !== 'sqlite')
205
		{
206 3
			if (!$postArray['dbName'])
207
			{
208 3
				$validateArray[] = $this->_language->get('name_empty');
209
			}
210 3
			if (!$postArray['dbUser'])
211
			{
212 3
				$validateArray[] = $this->_language->get('user_empty');
213
			}
214
		}
215 11
		return $validateArray;
216
	}
217
218
	/**
219
	 * validate the account
220
	 *
221
	 * @since 3.0.0
222
	 *
223
	 * @param array $postArray array of the post
224
	 *
225
	 * @return array
226
	 */
227
228 10
	protected function _validateAccount(array $postArray = []) : array
229
	{
230 10
		$emailValidator = new Validator\Email();
231 10
		$loginValidator = new Validator\Login();
232 10
		$validateArray = [];
233
234
		/* validate post */
235
236 10
		if (!$postArray['adminName'])
237
		{
238 3
			$validateArray[] = $this->_language->get('name_empty');
239
		}
240 10
		if (!$postArray['adminUser'])
241
		{
242 2
			$validateArray[] = $this->_language->get('user_empty');
243
		}
244 8
		else if (!$loginValidator->validate($postArray['adminUser']))
245
		{
246 1
			$validateArray[] = $this->_language->get('user_incorrect');
247
		}
248 10
		if (!$postArray['adminPassword'])
249
		{
250 2
			$validateArray[] = $this->_language->get('password_empty');
251
		}
252 8
		else if (!$loginValidator->validate($postArray['adminPassword']))
253
		{
254 1
			$validateArray[] = $this->_language->get('password_incorrect');
255
		}
256 10
		if (!$postArray['adminEmail'])
257
		{
258 2
			$validateArray[] = $this->_language->get('email_empty');
259
		}
260 8
		else if (!$emailValidator->validate($postArray['adminEmail']))
261
		{
262 1
			$validateArray[] = $this->_language->get('email_incorrect');
263
		}
264 10
		return $validateArray;
265
	}
266
267
	/**
268
	 * touch sqlite file
269
	 *
270
	 * @since 3.0.0
271
	 *
272
	 * @param array $configArray
273
	 *
274
	 * @return bool
275
	 */
276
277 5
	protected function _touch(array $configArray = []) : bool
278
	{
279 5
		if ($configArray['dbType'] === 'sqlite')
280
		{
281 5
			$file = $configArray['dbHost'] . '.tmp';
282 5
			return touch($file) && unlink($file);
283
		}
284
		return true;
285
	}
286
287
	/**
288
	 * write config file
289
	 *
290
	 * @since 3.0.0
291
	 *
292
	 * @param array $configArray
293
	 *
294
	 * @return bool
295
	 */
296
297 4
	protected function _write(array $configArray = []) : bool
298
	{
299 4
		$this->_config->set('dbType', $configArray['dbType']);
300 4
		$this->_config->set('dbHost', $configArray['dbHost']);
301 4
		$this->_config->set('dbName', $configArray['dbName']);
302 4
		$this->_config->set('dbUser', $configArray['dbUser']);
303 4
		$this->_config->set('dbPassword', $configArray['dbPassword']);
304 4
		$this->_config->set('dbPrefix', $configArray['dbPrefix']);
305 4
		return $this->_config->write();
306
	}
307
308
	/**
309
	 * get the status
310
	 *
311
	 * @since 3.0.0
312
	 *
313
	 * @return int
314
	 */
315
316 4
	protected function _getStatus() : int
317
	{
318 4
		return Db::getStatus();
319
	}
320
321
	/**
322
	 * refresh the connection
323
	 *
324
	 * @since 3.0.0
325
	 */
326
327
	protected function _refreshConnection() : void
328
	{
329
		Db::resetDb();
330
		Db::init();
331
	}
332
333
	/**
334
	 * install the database
335
	 *
336
	 * @since 3.0.0
337
	 *
338
	 * @param array $installArray
339
	 *
340
	 * @return bool
341
	 */
342
343 4
	protected function _install(array $installArray = []) : bool
344
	{
345 4
		$adminName = $installArray['adminName'];
346 4
		$adminUser = $installArray['adminUser'];
347 4
		$adminPassword = $installArray['adminPassword'];
348 4
		$adminEmail = $installArray['adminEmail'];
349 4
		if ($adminName && $adminUser && $adminPassword && $adminEmail)
350
		{
351 3
			$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
352 3
			$installer->init();
353 3
			$installer->rawDrop();
354 3
			$installer->rawCreate();
355 3
			$installer->insertData(
356
			[
357 3
				'adminName' => $installArray['adminName'],
358 3
				'adminUser' => $installArray['adminUser'],
359 3
				'adminPassword' => $installArray['adminPassword'],
360 3
				'adminEmail' => $installArray['adminEmail']
361
			]);
362 3
			return $this->_getStatus() === 2;
363
		}
364 1
		return false;
365
	}
366
367
	/**
368
	 * send the mail
369
	 *
370
	 * @since 3.0.0
371
	 *
372
	 * @param array $mailArray
373
	 *
374
	 * @return bool
375
	 */
376
377 1
	protected function _mail(array $mailArray = []) : bool
378
	{
379 1
		$settingModel = new Model\Setting();
380
381
		/* html element */
382
383 1
		$linkElement = new Html\Element();
384
		$linkElement
385 1
			->init('a',
386
			[
387 1
				'href' => $this->_registry->get('root')
388
			])
389 1
			->text($this->_registry->get('root'));
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_registry->get('root') targeting Redaxscript\Registry::get() can also be of type array; however, Redaxscript\Html\Element::text() does only seem to accept string|integer|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
390
391
		/* prepare mail */
392
393
		$toArray =
394
		[
395 1
			$mailArray['adminName'] => $mailArray['adminEmail']
396
		];
397
		$fromArray =
398
		[
399 1
			$settingModel->get('author') => $settingModel->get('email')
400
		];
401 1
		$subject = $this->_language->get('installation');
402
		$bodyArray =
403
		[
404 1
			$this->_language->get('user') . $this->_language->get('colon') . ' ' . $mailArray['adminUser'],
405 1
			'<br />',
406 1
			$this->_language->get('password') . $this->_language->get('colon') . ' ' . $mailArray['adminPassword'],
407 1
			'<br />',
408 1
			$this->_language->get('url') . $this->_language->get('colon') . ' ' . $linkElement
409
		];
410
411
		/* send mail */
412
413 1
		$mailer = new Mailer();
414 1
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $subject defined by $this->_language->get('installation') on line 401 can also be of type array; however, Redaxscript\Mailer::init() does only seem to accept null|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
415 1
		return $mailer->send();
416
	}
417
418
	/**
419
	 * show the success
420
	 *
421
	 * @since 3.0.0
422
	 *
423
	 * @param array $successArray array of the success
424
	 *
425
	 * @return string
426
	 */
427
428 1
	protected function _success(array $successArray = []) : string
429
	{
430 1
		$messenger = $this->_messengerFactory();
431
		return $messenger
432 1
			->setUrl($this->_language->get('home'), $successArray['url'])
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('home') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\View\Helper\Messenger::setUrl() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
433 1
			->doRedirect()
434 1
			->success($successArray['message'], $successArray['title']);
435
	}
436
437
	/**
438
	 * show the warning
439
	 *
440
	 * @since 3.0.0
441
	 *
442
	 * @param array $warningArray array of the warning
443
	 *
444
	 * @return string
445
	 */
446
447 1
	protected function _warning(array $warningArray = []) : string
448
	{
449 1
		$messenger = $this->_messengerFactory();
450
		return $messenger
451 1
			->setUrl($this->_language->get('home'), $warningArray['url'])
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('home') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\View\Helper\Messenger::setUrl() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
452 1
			->doRedirect()
453 1
			->warning($warningArray['message'], $warningArray['title']);
454
	}
455
456
	/**
457
	 * show the error
458
	 *
459
	 * @since 3.0.0
460
	 *
461
	 * @param array $errorArray array of the error
462
	 *
463
	 * @return string
464
	 */
465
466 6
	protected function _error(array $errorArray = []) : string
467
	{
468 6
		$messenger = $this->_messengerFactory();
469
		return $messenger
470 6
			->setUrl($this->_language->get('back'), $errorArray['url'])
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_language->get('back') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\View\Helper\Messenger::setUrl() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
471 6
			->error($errorArray['message'], $errorArray['title']);
472
	}
473
}
474