Completed
Push — master ( 8f4e9e...0947c3 )
by Henry
13:23
created

Group::_validatePost()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 13
cp 0
rs 8.8337
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 42
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
			$updateArray =
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
			if ($this->_update($postArray['id'], $updateArray))
97
			{
98
				return $this->_success(
99
				[
100
					'route' => 'admin/view/groups#' . $postArray['alias'],
101
					'timeout' => 2,
102
					'message' => $this->_language->get('operation_completed')
103
				]);
104
			}
105
		}
106
107
		/* handle error */
108
109
		return $this->_error(
110
		[
111
			'route' => $postArray['id'] ? 'admin/edit/groups/' . $postArray['id'] : 'admin/new/groups',
112
			'message' => $this->_language->get('something_wrong')
113
		]);
114
	}
115
116
	/**
117
	 * sanitize the post
118
	 *
119
	 * @since 4.0.0
120
	 *
121
	 * @return array
122
	 */
123
124
	protected function _sanitizePost() : array
125
	{
126
		$specialFilter = new Filter\Special();
127
		$aliasFilter = new Filter\Alias();
128
129
		/* sanitize post */
130
131
		return
132
		[
133
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
134
			'name' => $this->_request->getPost('name'),
135
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
136
			'description' => $this->_request->getPost('description'),
137
			'categories' => $this->_request->getPost('categories'),
138
			'articles' => $this->_request->getPost('articles'),
139
			'extras' => $this->_request->getPost('extras'),
140
			'comments' => $this->_request->getPost('comments'),
141
			'groups' => $this->_request->getPost('groups'),
142
			'users' => $this->_request->getPost('users'),
143
			'modules' => $this->_request->getPost('modules'),
144
			'settings' => $this->_request->getPost('settings'),
145
			'filter' => $specialFilter->sanitize($this->_request->getPost('filter')),
146
			'status' => $specialFilter->sanitize($this->_request->getPost('status'))
147
		];
148
	}
149
150
	/**
151
	 * validate the post
152
	 *
153
	 * @since 4.0.0
154
	 *
155
	 * @param array $postArray array of the post
156
	 *
157
	 * @return array
158
	 */
159
160
	protected function _validatePost(array $postArray = []) : array
161
	{
162
		$aliasValidator = new Validator\Alias();
163
		$groupModel = new Admin\Model\Group();
164
		$validateArray = [];
165
166
		/* validate post */
167
168
		if (!$postArray['name'])
169
		{
170
			$validateArray[] = $this->_language->get('name_empty');
171
		}
172
		if (!$postArray['id'])
173
		{
174
			if (!$postArray['alias'])
175
			{
176
				$validateArray[] = $this->_language->get('alias_empty');
177
			}
178
			else if ($aliasValidator->validate($postArray['alias'], 'general'))
179
			{
180
				$validateArray[] = $this->_language->get('alias_incorrect');
181
			}
182
			else if ($groupModel->getByAlias($postArray['alias'])->id !== $groupModel->getById($postArray['id'])->id)
183
			{
184
				$validateArray[] = $this->_language->get('alias_exists');
185
			}
186
		}
187
		return $validateArray;
188
	}
189
190
	/**
191
	 * create the group
192
	 *
193
	 * @since 4.0.0
194
	 *
195
	 * @param array $createArray array of the create
196
	 *
197
	 * @return bool
198
	 */
199
200
	protected function _create(array $createArray = []) : bool
201
	{
202
		$groupModel = new Admin\Model\Group();
203
		return $groupModel->createByArray($createArray);
204
	}
205
206
	/**
207
	 * update the group
208
	 *
209
	 * @since 4.0.0
210
	 *
211
	 * @param int $groupId identifier of the group
212
	 * @param array $updateArray
213
	 *
214
	 * @return bool
215
	 */
216
217
	public function _update(int $groupId = null, array $updateArray = []) : bool
218
	{
219
		$groupModel = new Admin\Model\Group();
220
		return $groupModel->updateByIdAndArray($groupId, $updateArray);
221
	}
222
}
223