Completed
Push — master ( ba8c52...47b68a )
by Henry
07:01
created

User::process()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 85

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 0
cts 41
cp 0
rs 7.0828
c 0
b 0
f 0
cc 8
nc 12
nop 1
crap 72

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\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Hash;
6
use Redaxscript\Filter;
7
use Redaxscript\Validator;
8
9
/**
10
 * children class to process the admin user request
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Controller
16
 * @author Henry Ruhs
17
 */
18
19
class User extends ControllerAbstract
20
{
21
	/**
22
	 * process the class
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @param string $action action to process
27
	 *
28
	 * @return string
29
	 */
30
31
	public function process(string $action = null) : string
32
	{
33
		$postArray = $this->_normalizePost($this->_sanitizePost());
34
		$validateArray = $this->_validatePost($postArray);
35
		$passwordHash = new Hash();
36
37
		/* validate post */
38
39
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by
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...
40
		{
41
			return $this->_error(
42
			[
43
				'route' => $this->_getErrorRoute($postArray),
44
				'message' => $validateArray
45
			]);
46
		}
47
48
		/* handle create */
49
50
		if ($action === 'create')
51
		{
52
			$passwordHash->init($postArray['password']);
53
			$createArray =
54
			[
55
				'name' => $postArray['name'],
56
				'user' => $postArray['user'],
57
				'description' => $postArray['description'],
58
				'password' => $passwordHash->getHash(),
59
				'email' => $postArray['email'],
60
				'language' => $postArray['language'],
61
				'status' => $postArray['status'],
62
				'groups' => $postArray['groups']
63
			];
64
			if ($this->_create($createArray))
65
			{
66
				return $this->_success(
67
				[
68
					'route' => $this->_getSuccessRoute($postArray),
69
					'timeout' => 2
70
				]);
71
			}
72
		}
73
74
		/* handle update */
75
76
		if ($action === 'update')
77
		{
78
			$updateFullArray =
79
			[
80
				'name' => $postArray['name'],
81
				'description' => $postArray['description'],
82
				'email' => $postArray['email'],
83
				'language' => $postArray['language'],
84
				'status' => $postArray['status'],
85
				'groups' => $postArray['groups']
86
			];
87
			$updateLiteArray =
88
			[
89
				'name' => $postArray['name'],
90
				'description' => $postArray['description'],
91
				'email' => $postArray['email'],
92
				'language' => $postArray['language']
93
			];
94
			if ($postArray['password'])
95
			{
96
				$passwordHash->init($postArray['password']);
97
				$updateFullArray['password'] = $updateLiteArray['password'] = $passwordHash->getHash();
98
			}
99
			if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray))
100
			{
101
				return $this->_success(
102
				[
103
					'route' => $this->_getSuccessRoute($postArray),
104
					'timeout' => 2
105
				]);
106
			}
107
		}
108
109
		/* handle error */
110
111
		return $this->_error(
112
		[
113
			'route' => $this->_getErrorRoute($postArray)
114
		]);
115
	}
116
117
	/**
118
	 * sanitize the post
119
	 *
120
	 * @since 4.0.0
121
	 *
122
	 * @return array
123
	 */
124
125
	protected function _sanitizePost() : array
126
	{
127
		$specialFilter = new Filter\Special();
128
		$emailFilter = new Filter\Email();
129
130
		/* sanitize post */
131
132
		return
133
		[
134
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
135
			'name' => $this->_request->getPost('name'),
136
			'user' => $this->_request->getPost('user'),
137
			'description' => $this->_request->getPost('description'),
138
			'password' => $this->_request->getPost('password'),
139
			'password_confirm' => $this->_request->getPost('password_confirm'),
140
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
141
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
142
			'status' => $specialFilter->sanitize($this->_request->getPost('status')),
143
			'groups' => json_encode($this->_request->getPost('groups'))
144
		];
145
	}
146
147
	/**
148
	 * validate the post
149
	 *
150
	 * @since 4.0.0
151
	 *
152
	 * @param array $postArray array of the post
153
	 *
154
	 * @return array
155
	 */
156
157
	protected function _validatePost(array $postArray = []) : array
158
	{
159
		$loginValidator = new Validator\Login();
160
		$emailValidator = new Validator\Email();
161
		$userModel = new Admin\Model\User();
162
		$validateArray = [];
163
164
		/* validate post */
165
166
		if (!$postArray['name'])
167
		{
168
			$validateArray[] = $this->_language->get('name_empty');
169
		}
170
		if (!$postArray['id'])
171
        {
172
			if (!$postArray['user'])
173
			{
174
				$validateArray[] = $this->_language->get('user_empty');
175
			}
176
			else if (!$loginValidator->validate($postArray['user']))
177
			{
178
				$validateArray[] = $this->_language->get('user_incorrect');
179
			}
180
			else if ($userModel->getByUser($postArray['user'])->id !== $userModel->getById($postArray['id'])->id)
181
			{
182
				$validateArray[] = $this->_language->get('user_exists');
183
			}
184
			if (!$postArray['password'])
185
			{
186
				$validateArray[] = $this->_language->get('password_empty');
187
			}
188
			else if (!$loginValidator->validate($postArray['password']))
189
			{
190
				$validateArray[] = $this->_language->get('password_incorrect');
191
			}
192
			else if ($postArray['password'] !== $postArray['password_confirm'])
193
			{
194
				$validateArray[] = $this->_language->get('password_mismatch');
195
			}
196
		}
197
		else if ($postArray['password'])
198
		{
199
			if (!$loginValidator->validate($postArray['password']))
200
			{
201
				$validateArray[] = $this->_language->get('password_incorrect');
202
			}
203
			else if ($postArray['password'] !== $postArray['password_confirm'])
204
			{
205
				$validateArray[] = $this->_language->get('password_mismatch');
206
			}
207
		}
208
		if (!$emailValidator->validate($postArray['email']))
209
		{
210
			$validateArray[] = $this->_language->get('email_incorrect');
211
		}
212
		return $validateArray;
213
	}
214
215
	/**
216
	 * create the user
217
	 *
218
	 * @since 4.0.0
219
	 *
220
	 * @param array $createArray array of the create
221
	 *
222
	 * @return bool
223
	 */
224
225
	protected function _create(array $createArray = []) : bool
226
	{
227
		$userModel = new Admin\Model\User();
228
		return $userModel->createByArray($createArray);
229
	}
230
231
	/**
232
	 * update the user
233
	 *
234
	 * @since 4.0.0
235
	 *
236
	 * @param int $userId identifier of the user
237
	 * @param array $updateArray array of the update
238
	 *
239
	 * @return bool
240
	 */
241
242
	public function _update(int $userId = null, array $updateArray = []) : bool
243
	{
244
		$userModel = new Admin\Model\User();
245
		return $userModel->updateByIdAndArray($userId, $updateArray);
246
	}
247
248
	/**
249
	 * get success route
250
	 *
251
	 * @since 4.0.0
252
	 *
253
	 * @param array $postArray array of the post
254
	 *
255
	 * @return string
256
	 */
257
258
	protected function _getSuccessRoute(array $postArray = []) : string
259
	{
260
		if ($this->_registry->get('usersEdit') && $postArray['id'])
261
		{
262
			return 'admin/view/users#row-' . $postArray['id'];
263
		}
264
		if ($this->_registry->get('usersEdit') && $postArray['user'])
265
		{
266
			$userModel = new Admin\Model\User();
267
			return 'admin/view/users#row-' . $userModel->getByUser($postArray['user'])->id;
268
		}
269
		return 'admin';
270
	}
271
272
	/**
273
	 * get error route
274
	 *
275
	 * @since 4.0.0
276
	 *
277
	 * @param array $postArray array of the post
278
	 *
279
	 * @return string
280
	 */
281
282
	protected function _getErrorRoute(array $postArray = []) : string
283
	{
284
		if ($this->_registry->get('usersEdit') && $postArray['id'])
285
		{
286
			return 'admin/edit/users/' . $postArray['id'];
287
		}
288
		if ($this->_registry->get('usersNew'))
289
		{
290
			return 'admin/new/users';
291
		}
292
		return 'admin';
293
	}
294
}
295