Register   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 16
dl 0
loc 227
ccs 86
cts 88
cp 0.9773
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 67 6
A _sanitizePost() 0 17 1
B _validatePost() 0 41 9
A _create() 0 5 1
A _mail() 0 44 2
1
<?php
2
namespace Redaxscript\Controller;
3
4
use Redaxscript\Filter;
5
use Redaxscript\Hash;
6
use Redaxscript\Html;
7
use Redaxscript\Mailer;
8
use Redaxscript\Model;
9
use Redaxscript\Validator;
10
use function uniqid;
11
12
/**
13
 * children class to process the register request
14
 *
15
 * @since 3.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Controller
19
 * @author Henry Ruhs
20
 * @author Balázs Szilágyi
21
 */
22
23
class Register extends ControllerAbstract
24
{
25
	/**
26
	 * process
27
	 *
28
	 * @since 3.0.0
29
	 *
30
	 * @return string
31
	 */
32
33 5
	public function process() : string
34
	{
35 5
		$passwordHash = new Hash();
36 5
		$passwordHash->init(uniqid());
37 5
		$groupModel = new Model\Group();
38 5
		$settingModel = new Model\Setting();
39 5
		$postArray = $this->_normalizePost($this->_sanitizePost());
40 5
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 39 can also be of type null; however, Redaxscript\Controller\Register::_validatePost() 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...
41
42
		/* validate post */
43
44 5
		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...
45
		{
46 3
			return $this->_error(
47
			[
48 3
				'route' => 'register',
49 3
				'message' => $validateArray
50
			]);
51
		}
52
53
		/* handle create */
54
55
		$createArray =
56
		[
57 2
			'name' => $postArray['name'],
58 2
			'user' => $postArray['user'],
59 2
			'password' => $passwordHash->getHash(),
60 2
			'email' => $postArray['email'],
61 2
			'language' => $this->_registry->get('language'),
62 2
			'groups' => $groupModel->getByAlias('members')->id,
63 2
			'status' => $settingModel->get('verification') ? 0 : 1
64
		];
65 2
		if (!$this->_create($createArray))
66
		{
67
			return $this->_error(
68
			[
69
				'route' => 'register'
70
			]);
71
		}
72
73
		/* handle mail */
74
75
		$mailArray =
76
		[
77 2
			'name' => $postArray['name'],
78 2
			'user' => $postArray['user'],
79 2
			'password' => $passwordHash->getRaw(),
80 2
			'email' => $postArray['email']
81
		];
82 2
		if (!$this->_mail($mailArray))
83
		{
84 1
			return $this->_error(
85
			[
86 1
				'route' => 'register',
87 1
				'message' => $this->_language->get('email_failed')
88
			]);
89
		}
90
91
		/* handle success */
92
93 1
		return $this->_success(
94
		[
95 1
			'route' => 'login',
96 1
			'timeout' => 2,
97 1
			'message' => $settingModel->get('verification') ? $this->_language->get('registration_verification') : $this->_language->get('registration_sent')
98
		]);
99
	}
100
101
	/**
102
	 * sanitize the post
103
	 *
104
	 * @since 4.0.0
105
	 *
106
	 * @return array
107
	 */
108
109 5
	protected function _sanitizePost() : array
110
	{
111 5
		$numberFilter = new Filter\Number();
112 5
		$specialFilter = new Filter\Special();
113 5
		$emailFilter = new Filter\Email();
114
115
		/* sanitize post */
116
117
		return
118
		[
119 5
			'name' => $specialFilter->sanitize($this->_request->getPost('name')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('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...
120 5
			'user' => $specialFilter->sanitize($this->_request->getPost('user')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('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...
121 5
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('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...
122 5
			'task' => $numberFilter->sanitize($this->_request->getPost('task')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('task') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Number::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...
123 5
			'solution' => $this->_request->getPost('solution')
124
		];
125
	}
126
127
	/**
128
	 * validate the post
129
	 *
130
	 * @since 3.0.0
131
	 *
132
	 * @param array $postArray array of the post
133
	 *
134
	 * @return array
135
	 */
136
137 5
	protected function _validatePost(array $postArray = []) : array
138
	{
139 5
		$loginValidator = new Validator\Login();
140 5
		$emailValidator = new Validator\Email();
141 5
		$captchaValidator = new Validator\Captcha();
142 5
		$settingModel = new Model\Setting();
143 5
		$userModel = new Model\User();
144 5
		$validateArray = [];
145
146
		/* validate post */
147
148 5
		if (!$postArray['name'])
149
		{
150 1
			$validateArray[] = $this->_language->get('name_empty');
151
		}
152 5
		if (!$postArray['user'])
153
		{
154 1
			$validateArray[] = $this->_language->get('user_empty');
155
		}
156 4
		else if (!$loginValidator->validate($postArray['user']))
157
		{
158 1
			$validateArray[] = $this->_language->get('user_incorrect');
159
		}
160 3
		else if ($userModel->query()->where('user', $postArray['user'])->findOne()->id)
161
		{
162 1
			$validateArray[] = $this->_language->get('user_exists');
163
		}
164 5
		if (!$postArray['email'])
165
		{
166 1
			$validateArray[] = $this->_language->get('email_empty');
167
		}
168 4
		else if (!$emailValidator->validate($postArray['email']))
169
		{
170 1
			$validateArray[] = $this->_language->get('email_incorrect');
171
		}
172 5
		if ($settingModel->get('captcha') > 0 && !$captchaValidator->validate($postArray['task'], $postArray['solution']))
173
		{
174 1
			$validateArray[] = $this->_language->get('captcha_incorrect');
175
		}
176 5
		return $validateArray;
177
	}
178
179
	/**
180
	 * create the user
181
	 *
182
	 * @since 3.0.0
183
	 *
184
	 * @param array $createArray
185
	 *
186
	 * @return bool
187
	 */
188
189 2
	protected function _create(array $createArray = []) : bool
190
	{
191 2
		$userModel = new Model\User();
192 2
		return $userModel->createByArray($createArray);
193
	}
194
195
	/**
196
	 * send the mail
197
	 *
198
	 * @since 3.0.0
199
	 *
200
	 * @param array $mailArray
201
	 *
202
	 * @return bool
203
	 */
204
205 1
	protected function _mail(array $mailArray = []) : bool
206
	{
207 1
		$settingModel = new Model\Setting();
208 1
		$urlLogin = $this->_registry->get('root') . '/' . $this->_registry->get('parameterRoute') . 'login';
209
210
		/* html element */
211
212 1
		$linkElement = new Html\Element();
213
		$linkElement
214 1
			->init('a',
215
			[
216 1
				'href' => $urlLogin
217
			])
218 1
			->text($urlLogin);
219
220
		/* prepare mail */
221
222
		$toArray =
223
		[
224 1
			$mailArray['name'] => $mailArray['email'],
225 1
			$settingModel->get('author') => $settingModel->get('notification') ? $settingModel->get('email') : null
226
		];
227
		$fromArray =
228
		[
229 1
			$mailArray['name'] => $mailArray['email']
230
		];
231 1
		$subject = $this->_language->get('registration');
232
		$bodyArray =
233
		[
234 1
			$this->_language->get('name') . $this->_language->get('colon') . ' ' . $mailArray['name'],
235 1
			'<br />',
236 1
			$this->_language->get('user') . $this->_language->get('colon') . ' ' . $mailArray['user'],
237 1
			'<br />',
238 1
			$this->_language->get('password') . $this->_language->get('colon') . ' ' . $mailArray['password'],
239 1
			'<br />',
240 1
			$this->_language->get('login') . $this->_language->get('colon') . ' ' . $linkElement
241
		];
242
243
		/* send mail */
244
245 1
		$mailer = new Mailer();
246 1
		$mailer->init($toArray, $fromArray, $subject, $bodyArray);
0 ignored issues
show
Bug introduced by JimMorrison723
It seems like $subject defined by $this->_language->get('registration') on line 231 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...
247 1
		return $mailer->send();
248
	}
249
}
250