Completed
Push — master ( 0947c3...b8ee92 )
by Henry
11:06 queued 03:27
created

Group::process()   C

Complexity

Conditions 9
Paths 8

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 90
ccs 0
cts 48
cp 0
rs 6.6626
c 0
b 0
f 0
cc 9
nc 8
nop 1
crap 90

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\Filter;
6
use Redaxscript\Validator;
7
8
/**
9
 * children class to process the admin group request
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Controller
15
 * @author Henry Ruhs
16
 */
17
18
class Group extends ControllerAbstract
19
{
20
	/**
21
	 * process the class
22
	 *
23
	 * @since 4.0.0
24
	 *
25
	 * @param string $action action to process
26
	 *
27
	 * @return string
28
	 */
29
30
	public function process(string $action = null) : string
31
	{
32
		$postArray = $this->_normalizePost($this->_sanitizePost());
33
		$validateArray = $this->_validatePost($postArray);
34
35
		/* validate post */
36
37
		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...
38
		{
39
			return $this->_error(
40
			[
41
				'route' => $postArray['id'] ? 'admin/edit/groups/' . $postArray['id'] : 'admin/new/groups',
42
				'message' => $validateArray
43
			]);
44
		}
45
46
		/* handle create */
47
48
		if ($action === 'create')
49
		{
50
			$createArray =
51
			[
52
				'name' => $postArray['name'],
53
				'alias' => $postArray['alias'],
54
				'description' => $postArray['description'],
55
				'categories' => $postArray['categories'],
56
				'articles' => $postArray['articles'],
57
				'extras' => $postArray['extras'],
58
				'comments' => $postArray['comments'],
59
				'groups' => $postArray['groups'],
60
				'users' => $postArray['users'],
61
				'modules' => $postArray['modules'],
62
				'settings' => $postArray['settings'],
63
				'filter' => $postArray['filter'],
64
				'status' => $postArray['status']
65
			];
66
			if ($this->_create($createArray))
67
			{
68
				return $this->_success(
69
				[
70
					'route' => 'admin/view/groups#' . $postArray['alias'],
71
					'timeout' => 2,
72
					'message' => $this->_language->get('operation_completed')
73
				]);
74
			}
75
		}
76
77
		/* handle update */
78
79
		if ($action === 'update')
80
		{
81
			$updateFullArray =
82
			[
83
				'name' => $postArray['name'],
84
				'description' => $postArray['description'],
85
				'categories' => $postArray['categories'],
86
				'articles' => $postArray['articles'],
87
				'extras' => $postArray['extras'],
88
				'comments' => $postArray['comments'],
89
				'groups' => $postArray['groups'],
90
				'users' => $postArray['users'],
91
				'modules' => $postArray['modules'],
92
				'settings' => $postArray['settings'],
93
				'filter' => $postArray['filter'],
94
				'status' => $postArray['status']
95
			];
96
			$updateLiteArray =
97
			[
98
				'name' => $postArray['name'],
99
				'description' => $postArray['description']
100
			];
101
			if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray))
102
			{
103
				return $this->_success(
104
				[
105
					'route' => 'admin/view/groups#' . $postArray['alias'],
106
					'timeout' => 2,
107
					'message' => $this->_language->get('operation_completed')
108
				]);
109
			}
110
		}
111
112
		/* handle error */
113
114
		return $this->_error(
115
		[
116
			'route' => $postArray['id'] ? 'admin/edit/groups/' . $postArray['id'] : 'admin/new/groups',
117
			'message' => $this->_language->get('something_wrong')
118
		]);
119
	}
120
121
	/**
122
	 * sanitize the post
123
	 *
124
	 * @since 4.0.0
125
	 *
126
	 * @return array
127
	 */
128
129
	protected function _sanitizePost() : array
130
	{
131
		$specialFilter = new Filter\Special();
132
		$aliasFilter = new Filter\Alias();
133
134
		/* sanitize post */
135
136
		return
137
		[
138
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
139
			'name' => $this->_request->getPost('name'),
140
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
141
			'description' => $this->_request->getPost('description'),
142
			'categories' => $this->_request->getPost('categories'),
143
			'articles' => $this->_request->getPost('articles'),
144
			'extras' => $this->_request->getPost('extras'),
145
			'comments' => $this->_request->getPost('comments'),
146
			'groups' => $this->_request->getPost('groups'),
147
			'users' => $this->_request->getPost('users'),
148
			'modules' => $this->_request->getPost('modules'),
149
			'settings' => $this->_request->getPost('settings'),
150
			'filter' => $specialFilter->sanitize($this->_request->getPost('filter')),
151
			'status' => $specialFilter->sanitize($this->_request->getPost('status'))
152
		];
153
	}
154
155
	/**
156
	 * validate the post
157
	 *
158
	 * @since 4.0.0
159
	 *
160
	 * @param array $postArray array of the post
161
	 *
162
	 * @return array
163
	 */
164
165
	protected function _validatePost(array $postArray = []) : array
166
	{
167
		$aliasValidator = new Validator\Alias();
168
		$groupModel = new Admin\Model\Group();
169
		$validateArray = [];
170
171
		/* validate post */
172
173
		if (!$postArray['name'])
174
		{
175
			$validateArray[] = $this->_language->get('name_empty');
176
		}
177
		if (!$postArray['id'])
178
		{
179
			if (!$postArray['alias'])
180
			{
181
				$validateArray[] = $this->_language->get('alias_empty');
182
			}
183
			else if ($aliasValidator->validate($postArray['alias'], 'general'))
184
			{
185
				$validateArray[] = $this->_language->get('alias_incorrect');
186
			}
187
			else if ($groupModel->getByAlias($postArray['alias'])->id !== $groupModel->getById($postArray['id'])->id)
188
			{
189
				$validateArray[] = $this->_language->get('alias_exists');
190
			}
191
		}
192
		return $validateArray;
193
	}
194
195
	/**
196
	 * create the group
197
	 *
198
	 * @since 4.0.0
199
	 *
200
	 * @param array $createArray array of the create
201
	 *
202
	 * @return bool
203
	 */
204
205
	protected function _create(array $createArray = []) : bool
206
	{
207
		$groupModel = new Admin\Model\Group();
208
		return $groupModel->createByArray($createArray);
209
	}
210
211
	/**
212
	 * update the group
213
	 *
214
	 * @since 4.0.0
215
	 *
216
	 * @param int $groupId identifier of the group
217
	 * @param array $updateArray
218
	 *
219
	 * @return bool
220
	 */
221
222
	public function _update(int $groupId = null, array $updateArray = []) : bool
223
	{
224
		$groupModel = new Admin\Model\Group();
225
		return $groupModel->updateByIdAndArray($groupId, $updateArray);
226
	}
227
}
228