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

Group::_getSuccessRoute()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 0
cp 0
rs 9.5222
c 0
b 0
f 0
cc 5
nc 3
nop 1
crap 30
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' => $this->_getSuccessRoute($postArray),
71
					'timeout' => 2
72
				]);
73
			}
74
		}
75
76
		/* handle update */
77
78
		if ($action === 'update')
79
		{
80
			$updateFullArray =
81
			[
82
				'name' => $postArray['name'],
83
				'description' => $postArray['description'],
84
				'categories' => $postArray['categories'],
85
				'articles' => $postArray['articles'],
86
				'extras' => $postArray['extras'],
87
				'comments' => $postArray['comments'],
88
				'groups' => $postArray['groups'],
89
				'users' => $postArray['users'],
90
				'modules' => $postArray['modules'],
91
				'settings' => $postArray['settings'],
92
				'filter' => $postArray['filter'],
93
				'status' => $postArray['status']
94
			];
95
			$updateLiteArray =
96
			[
97
				'name' => $postArray['name'],
98
				'description' => $postArray['description']
99
			];
100
			if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray))
101
			{
102
				return $this->_success(
103
				[
104
					'route' => $this->_getSuccessRoute($postArray),
105
					'timeout' => 2
106
				]);
107
			}
108
		}
109
110
		/* handle error */
111
112
		return $this->_error(
113
		[
114
			'route' => $this->_getErrorRoute($postArray)
115
		]);
116
	}
117
118
	/**
119
	 * sanitize the post
120
	 *
121
	 * @since 4.0.0
122
	 *
123
	 * @return array
124
	 */
125
126
	protected function _sanitizePost() : array
127
	{
128
		$specialFilter = new Filter\Special();
129
		$aliasFilter = new Filter\Alias();
130
131
		/* sanitize post */
132
133
		return
134
		[
135
			'id' => $specialFilter->sanitize($this->_request->getPost('id')),
136
			'name' => $this->_request->getPost('name'),
137
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
138
			'description' => $this->_request->getPost('description'),
139
			'categories' => json_encode($this->_request->getPost('categories')),
140
			'articles' => json_encode($this->_request->getPost('articles')),
141
			'extras' => json_encode($this->_request->getPost('extras')),
142
			'comments' => json_encode($this->_request->getPost('comments')),
143
			'groups' => json_encode($this->_request->getPost('groups')),
144
			'users' => json_encode($this->_request->getPost('users')),
145
			'modules' => json_encode($this->_request->getPost('modules')),
146
			'settings' => $this->_request->getPost('settings'),
147
			'filter' => $specialFilter->sanitize($this->_request->getPost('filter')),
148
			'status' => $specialFilter->sanitize($this->_request->getPost('status'))
149
		];
150
	}
151
152
	/**
153
	 * validate the post
154
	 *
155
	 * @since 4.0.0
156
	 *
157
	 * @param array $postArray array of the post
158
	 *
159
	 * @return array
160
	 */
161
162
	protected function _validatePost(array $postArray = []) : array
163
	{
164
		$aliasValidator = new Validator\Alias();
165
		$groupModel = new Admin\Model\Group();
166
		$validateArray = [];
167
168
		/* validate post */
169
170
		if (!$postArray['name'])
171
		{
172
			$validateArray[] = $this->_language->get('name_empty');
173
		}
174
		if (!$postArray['id'])
175
		{
176
			if (!$postArray['alias'])
177
			{
178
				$validateArray[] = $this->_language->get('alias_empty');
179
			}
180
			else if ($aliasValidator->validate($postArray['alias'], 'general'))
181
			{
182
				$validateArray[] = $this->_language->get('alias_incorrect');
183
			}
184
			else if ($groupModel->getByAlias($postArray['alias'])->id !== $groupModel->getById($postArray['id'])->id)
185
			{
186
				$validateArray[] = $this->_language->get('alias_exists');
187
			}
188
		}
189
		return $validateArray;
190
	}
191
192
	/**
193
	 * create the group
194
	 *
195
	 * @since 4.0.0
196
	 *
197
	 * @param array $createArray array of the create
198
	 *
199
	 * @return bool
200
	 */
201
202
	protected function _create(array $createArray = []) : bool
203
	{
204
		$groupModel = new Admin\Model\Group();
205
		return $groupModel->createByArray($createArray);
206
	}
207
208
	/**
209
	 * update the group
210
	 *
211
	 * @since 4.0.0
212
	 *
213
	 * @param int $groupId identifier of the group
214
	 * @param array $updateArray array of the update
215
	 *
216
	 * @return bool
217
	 */
218
219
	public function _update(int $groupId = null, array $updateArray = []) : bool
220
	{
221
		$groupModel = new Admin\Model\Group();
222
		return $groupModel->updateByIdAndArray($groupId, $updateArray);
223
	}
224
225
	/**
226
	 * get success route
227
	 *
228
	 * @since 4.0.0
229
	 *
230
	 * @param array $postArray array of the post
231
	 *
232
	 * @return string
233
	 */
234
235
	protected function _getSuccessRoute(array $postArray = []) : string
236
	{
237
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
238
		{
239
			return 'admin/view/groups#row-' . $postArray['id'];
240
		}
241
		if ($this->_registry->get('groupsEdit') && $postArray['alias'])
242
		{
243
			$groupModel = new Admin\Model\Group();
244
			return 'admin/view/groups#row-' . $groupModel->getByAlias($postArray['alias'])->id;
245
		}
246
		return 'admin';
247
	}
248
249
	/**
250
	 * get error route
251
	 *
252
	 * @since 4.0.0
253
	 *
254
	 * @param array $postArray array of the post
255
	 *
256
	 * @return string
257
	 */
258
259
	protected function _getErrorRoute(array $postArray = []) : string
260
	{
261
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
262
		{
263
			return 'admin/edit/groups/' . $postArray['id'];
264
		}
265
		if ($this->_registry->get('groupsNew'))
266
		{
267
			return 'admin/new/groups';
268
		}
269
		return 'admin';
270
	}
271
}
272