Completed
Push — master ( dc8f37...8770f3 )
by Henry
15:26 queued 05:23
created

includes/Admin/Controller/Group.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Filter;
6
use Redaxscript\Validator;
7
use function json_encode;
8
9
/**
10
 * children class to process the admin group request
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Controller
16
 * @author Henry Ruhs
17
 */
18
19
class Group 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 10
	public function process(string $action = null) : string
32
	{
33 10
		$postArray = $this->_normalizePost($this->_sanitizePost());
34 10
		$validateArray = $this->_validatePost($postArray);
35
36
		/* validate post */
37
38 10
		if ($validateArray)
39
		{
40 6
			return $this->_error(
41
			[
42 6
				'route' => $this->_getErrorRoute(),
43 6
				'message' => $validateArray
44
			]);
45
		}
46
47
		/* handle create */
48
49 4
		if ($action === 'create')
50
		{
51
			$createArray =
52
			[
53 1
				'name' => $postArray['name'],
54 1
				'alias' => $postArray['alias'],
55 1
				'description' => $postArray['description'],
56 1
				'categories' => $postArray['categories'],
57 1
				'articles' => $postArray['articles'],
58 1
				'extras' => $postArray['extras'],
59 1
				'comments' => $postArray['comments'],
60 1
				'groups' => $postArray['groups'],
61 1
				'users' => $postArray['users'],
62 1
				'modules' => $postArray['modules'],
63 1
				'settings' => $postArray['settings'],
64 1
				'filter' => $postArray['filter'],
65 1
				'status' => $postArray['status']
66
			];
67 1
			if ($this->_create($createArray))
68
			{
69 1
				return $this->_success(
70
				[
71 1
					'route' => $this->_getSuccessRoute($postArray),
72 1
					'timeout' => 2
73
				]);
74
			}
75
		}
76
77
		/* handle update */
78
79 3
		if ($action === 'update')
80
		{
81
			$updateFullArray =
82
			[
83 2
				'name' => $postArray['name'],
84 2
				'description' => $postArray['description'],
85 2
				'categories' => $postArray['categories'],
86 2
				'articles' => $postArray['articles'],
87 2
				'extras' => $postArray['extras'],
88 2
				'comments' => $postArray['comments'],
89 2
				'groups' => $postArray['groups'],
90 2
				'users' => $postArray['users'],
91 2
				'modules' => $postArray['modules'],
92 2
				'settings' => $postArray['settings'],
93 2
				'filter' => $postArray['filter'],
94 2
				'status' => $postArray['status']
95
			];
96
			$updateLiteArray =
97
			[
98 2
				'name' => $postArray['name'],
99 2
				'description' => $postArray['description']
100
			];
101 2
			if ($this->_update($postArray['id'], $postArray['id'] > 1 ? $updateFullArray : $updateLiteArray))
102
			{
103 2
				return $this->_success(
104
				[
105 2
					'route' => $this->_getSuccessRoute($postArray),
106 2
					'timeout' => 2
107
				]);
108
			}
109
		}
110
111
		/* handle error */
112
113 1
		return $this->_error(
114
		[
115 1
			'route' => $this->_getErrorRoute($postArray)
116
		]);
117
	}
118
119
	/**
120
	 * sanitize the post
121
	 *
122
	 * @since 4.0.0
123
	 *
124
	 * @return array
125
	 */
126
127 10
	protected function _sanitizePost() : array
128
	{
129 10
		$aliasFilter = new Filter\Alias();
130 10
		$nameFilter = new Filter\Name();
131 10
		$numberFilter = new Filter\Number();
132
		$toggleFilter = new Filter\Toggle();
133
134
		/* sanitize post */
135
136
		return
137 10
		[
138 10
			'id' => $numberFilter->sanitize($this->_request->getPost('id')),
0 ignored issues
show
It seems like $this->_request->getPost('id') 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...
139 10
			'name' => $nameFilter->sanitize($this->_request->getPost('name')),
0 ignored issues
show
It seems like $this->_request->getPost('name') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Name::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...
140 10
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
141 10
			'description' => $this->_request->getPost('description'),
142 10
			'categories' => json_encode($this->_request->getPost('categories')),
143 10
			'articles' => json_encode($this->_request->getPost('articles')),
144 10
			'extras' => json_encode($this->_request->getPost('extras')),
145 10
			'comments' => json_encode($this->_request->getPost('comments')),
146 10
			'groups' => json_encode($this->_request->getPost('groups')),
147 10
			'users' => json_encode($this->_request->getPost('users')),
148 10
			'modules' => json_encode($this->_request->getPost('modules')),
149 10
			'settings' => $numberFilter->sanitize($this->_request->getPost('settings')),
0 ignored issues
show
It seems like $this->_request->getPost('settings') 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...
150 10
			'filter' => $toggleFilter->sanitize($this->_request->getPost('filter')),
151
			'status' => $toggleFilter->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 10
165
	protected function _validatePost(array $postArray = []) : array
166 10
	{
167 10
		$nameValidator = new Validator\Name();
168 10
		$aliasValidator = new Validator\Alias();
169
		$groupModel = new Admin\Model\Group();
170
		$validateArray = [];
171
172 10
		/* validate post */
173
174 6
		if (!$postArray['name'])
175
		{
176 10
			$validateArray[] = $this->_language->get('name_empty');
177
		}
178 6
		else if (!$nameValidator->validate($postArray['name']))
179
		{
180 3
			$validateArray[] = $this->_language->get('name_incorrect');
181
		}
182 3
		if (!$postArray['id'])
183
		{
184 1
			if (!$postArray['alias'])
185
			{
186 2
				$validateArray[] = $this->_language->get('alias_empty');
187
			}
188 1
			else if (!$aliasValidator->validate($postArray['alias']))
189
			{
190
				$validateArray[] = $this->_language->get('alias_incorrect');
191 10
			}
192
			else if (!$groupModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
193
			{
194
				$validateArray[] = $this->_language->get('alias_exists');
195
			}
196
		}
197
		return $validateArray;
198
	}
199
200
	/**
201
	 * create the group
202
	 *
203
	 * @since 4.0.0
204 1
	 *
205
	 * @param array $createArray array of the create
206 1
	 *
207 1
	 * @return bool
208
	 */
209
210
	protected function _create(array $createArray = []) : bool
211
	{
212
		$groupModel = new Admin\Model\Group();
213
		return $groupModel->createByArray($createArray);
214
	}
215
216
	/**
217
	 * update the group
218
	 *
219
	 * @since 4.0.0
220
	 *
221 2
	 * @param int $groupId identifier of the group
222
	 * @param array $updateArray array of the update
223 2
	 *
224 2
	 * @return bool
225
	 */
226
227
	protected function _update(int $groupId = null, array $updateArray = []) : bool
228
	{
229
		$groupModel = new Admin\Model\Group();
230
		return $groupModel->updateByIdAndArray($groupId, $updateArray);
231
	}
232
233
	/**
234
	 * get success route
235
	 *
236
	 * @since 4.0.0
237 3
	 *
238
	 * @param array $postArray array of the post
239 3
	 *
240
	 * @return string
241 1
	 */
242
243 2
	protected function _getSuccessRoute(array $postArray = []) : string
244
	{
245 1
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
246 1
		{
247
			return 'admin/view/groups#row-' . $postArray['id'];
248 1
		}
249
		if ($this->_registry->get('groupsEdit') && $postArray['alias'])
250
		{
251
			$groupModel = new Admin\Model\Group();
252
			return 'admin/view/groups#row-' . $groupModel->getByAlias($postArray['alias'])->id;
253
		}
254
		return 'admin';
255
	}
256
257
	/**
258
	 * get error route
259
	 *
260
	 * @since 4.0.0
261 7
	 *
262
	 * @param array $postArray array of the post
263 7
	 *
264
	 * @return string
265 1
	 */
266
267 6
	protected function _getErrorRoute(array $postArray = []) : string
268
	{
269 5
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
270
		{
271 1
			return 'admin/edit/groups/' . $postArray['id'];
272
		}
273
		if ($this->_registry->get('groupsNew'))
274
		{
275
			return 'admin/new/groups';
276
		}
277
		return 'admin';
278
	}
279
}
280