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