Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

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

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
	public function process(string $action = null) : string
32
	{
33
		$postArray = $this->_normalizePost($this->_sanitizePost());
34
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 33 can also be of type null; however, Redaxscript\Admin\Contro...\Group::_validatePost() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35
36
		/* validate post */
37
38
		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...
39
		{
40
			return $this->_error(
41
			[
42
				'route' => $this->_getErrorRoute(),
43
				'message' => $validateArray
44
			]);
45
		}
46
47
		/* handle create */
48
49
		if ($action === 'create')
50
		{
51
			$createArray =
52
			[
53
				'name' => $postArray['name'],
54
				'alias' => $postArray['alias'],
55
				'description' => $postArray['description'],
56
				'categories' => $postArray['categories'],
57
				'articles' => $postArray['articles'],
58
				'extras' => $postArray['extras'],
59
				'comments' => $postArray['comments'],
60
				'groups' => $postArray['groups'],
61
				'users' => $postArray['users'],
62
				'modules' => $postArray['modules'],
63
				'settings' => $postArray['settings'],
64
				'filter' => $postArray['filter'],
65
				'status' => $postArray['status']
66
			];
67
			if ($this->_create($createArray))
68
			{
69
				return $this->_success(
70
				[
71
					'route' => $this->_getSuccessRoute($postArray),
72
					'timeout' => 2
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' => $this->_getSuccessRoute($postArray),
106
					'timeout' => 2
107
				]);
108
			}
109
		}
110
111
		/* handle error */
112
113
		return $this->_error(
114
		[
115
			'route' => $this->_getErrorRoute($postArray)
0 ignored issues
show
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 33 can also be of type null; however, Redaxscript\Admin\Contro...Group::_getErrorRoute() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
		]);
117
	}
118
119
	/**
120
	 * sanitize the post
121
	 *
122
	 * @since 4.0.0
123
	 *
124
	 * @return array
125
	 */
126
127
	protected function _sanitizePost() : array
128
	{
129
		$aliasFilter = new Filter\Alias();
130
		$numberFilter = new Filter\Number();
131
		$toggleFilter = new Filter\Toggle();
132
133
		/* sanitize post */
134
135
		return
136
		[
137
			'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...
138
			'name' => $this->_request->getPost('name'),
139
			'alias' => $aliasFilter->sanitize($this->_request->getPost('alias')),
0 ignored issues
show
It seems like $this->_request->getPost('alias') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Alias::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
			'description' => $this->_request->getPost('description'),
141
			'categories' => json_encode($this->_request->getPost('categories')),
142
			'articles' => json_encode($this->_request->getPost('articles')),
143
			'extras' => json_encode($this->_request->getPost('extras')),
144
			'comments' => json_encode($this->_request->getPost('comments')),
145
			'groups' => json_encode($this->_request->getPost('groups')),
146
			'users' => json_encode($this->_request->getPost('users')),
147
			'modules' => json_encode($this->_request->getPost('modules')),
148
			'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...
149
			'filter' => $toggleFilter->sanitize($this->_request->getPost('filter')),
0 ignored issues
show
It seems like $this->_request->getPost('filter') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::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
			'status' => $toggleFilter->sanitize($this->_request->getPost('status'))
0 ignored issues
show
It seems like $this->_request->getPost('status') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Toggle::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...
151
		];
152
	}
153
154
	/**
155
	 * validate the post
156
	 *
157
	 * @since 4.0.0
158
	 *
159
	 * @param array $postArray array of the post
160
	 *
161
	 * @return array
162
	 */
163
164
	protected function _validatePost(array $postArray = []) : array
165
	{
166
		$aliasValidator = new Validator\Alias();
167
		$groupModel = new Admin\Model\Group();
168
		$validateArray = [];
169
170
		/* validate post */
171
172
		if (!$postArray['name'])
173
		{
174
			$validateArray[] = $this->_language->get('name_empty');
175
		}
176
		if (!$postArray['id'])
177
		{
178
			if (!$postArray['alias'])
179
			{
180
				$validateArray[] = $this->_language->get('alias_empty');
181
			}
182
			else if (!$aliasValidator->validate($postArray['alias'], 'general'))
183
			{
184
				$validateArray[] = $this->_language->get('alias_incorrect');
185
			}
186
			else if (!$groupModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
187
			{
188
				$validateArray[] = $this->_language->get('alias_exists');
189
			}
190
		}
191
		return $validateArray;
192
	}
193
194
	/**
195
	 * create the group
196
	 *
197
	 * @since 4.0.0
198
	 *
199
	 * @param array $createArray array of the create
200
	 *
201
	 * @return bool
202
	 */
203
204
	protected function _create(array $createArray = []) : bool
205
	{
206
		$groupModel = new Admin\Model\Group();
207
		return $groupModel->createByArray($createArray);
208
	}
209
210
	/**
211
	 * update the group
212
	 *
213
	 * @since 4.0.0
214
	 *
215
	 * @param int $groupId identifier of the group
216
	 * @param array $updateArray array of the update
217
	 *
218
	 * @return bool
219
	 */
220
221
	protected function _update(int $groupId = null, array $updateArray = []) : bool
222
	{
223
		$groupModel = new Admin\Model\Group();
224
		return $groupModel->updateByIdAndArray($groupId, $updateArray);
225
	}
226
227
	/**
228
	 * get success route
229
	 *
230
	 * @since 4.0.0
231
	 *
232
	 * @param array $postArray array of the post
233
	 *
234
	 * @return string
235
	 */
236
237
	protected function _getSuccessRoute(array $postArray = []) : string
238
	{
239
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
240
		{
241
			return 'admin/view/groups#row-' . $postArray['id'];
242
		}
243
		if ($this->_registry->get('groupsEdit') && $postArray['alias'])
244
		{
245
			$groupModel = new Admin\Model\Group();
246
			return 'admin/view/groups#row-' . $groupModel->getByAlias($postArray['alias'])->id;
247
		}
248
		return 'admin';
249
	}
250
251
	/**
252
	 * get error route
253
	 *
254
	 * @since 4.0.0
255
	 *
256
	 * @param array $postArray array of the post
257
	 *
258
	 * @return string
259
	 */
260
261
	protected function _getErrorRoute(array $postArray = []) : string
262
	{
263
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
264
		{
265
			return 'admin/edit/groups/' . $postArray['id'];
266
		}
267
		if ($this->_registry->get('groupsNew'))
268
		{
269
			return 'admin/new/groups';
270
		}
271
		return 'admin';
272
	}
273
}
274