Completed
Push — master ( e2a767...40afc9 )
by Henry
16:31 queued 08:12
created

includes/Admin/Controller/Group.php (10 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 10
	public function process(string $action = null) : string
32
	{
33 10
		$postArray = $this->_normalizePost($this->_sanitizePost());
34 10
		$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 10
		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 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)
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 10
	protected function _sanitizePost() : array
128
	{
129 10
		$aliasFilter = new Filter\Alias();
130 10
		$numberFilter = new Filter\Number();
131 10
		$textFilter = new Filter\Text();
132 10
		$toggleFilter = new Filter\Toggle();
133
134
		/* sanitize post */
135
136
		return
137
		[
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 integer|string|null, 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' => $textFilter->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\Text::sanitize() does only seem to accept integer|string|null, 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')),
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...
141 10
			'description' => $textFilter->sanitize($this->_request->getPost('description')),
0 ignored issues
show
It seems like $this->_request->getPost('description') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Text::sanitize() does only seem to accept integer|string|null, 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...
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 integer|string|null, 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')),
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...
151 10
			'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...
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
165 10
	protected function _validatePost(array $postArray = []) : array
166
	{
167 10
		$nameValidator = new Validator\Name();
168 10
		$aliasValidator = new Validator\Alias();
169 10
		$groupModel = new Admin\Model\Group();
170 10
		$validateArray = [];
171
172
		/* validate post */
173
174 10
		if (!$postArray['name'])
175
		{
176 4
			$validateArray[] = $this->_language->get('name_empty');
177
		}
178 6
		else if (!$nameValidator->validate($postArray['name']))
179
		{
180 1
			$validateArray[] = $this->_language->get('name_incorrect');
181
		}
182 10
		if (!$postArray['id'])
183
		{
184 6
			if (!$postArray['alias'])
185
			{
186 3
				$validateArray[] = $this->_language->get('alias_empty');
187
			}
188 3
			else if (!$aliasValidator->validate($postArray['alias']))
189
			{
190 1
				$validateArray[] = $this->_language->get('alias_incorrect');
191
			}
192 2
			else if (!$groupModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
193
			{
194 1
				$validateArray[] = $this->_language->get('alias_exists');
195
			}
196
		}
197 10
		return $validateArray;
198
	}
199
200
	/**
201
	 * create the group
202
	 *
203
	 * @since 4.0.0
204
	 *
205
	 * @param array $createArray array of the create
206
	 *
207
	 * @return bool
208
	 */
209
210 1
	protected function _create(array $createArray = []) : bool
211
	{
212 1
		$groupModel = new Admin\Model\Group();
213 1
		return $groupModel->createByArray($createArray);
214
	}
215
216
	/**
217
	 * update the group
218
	 *
219
	 * @since 4.0.0
220
	 *
221
	 * @param int $groupId identifier of the group
222
	 * @param array $updateArray array of the update
223
	 *
224
	 * @return bool
225
	 */
226
227 2
	protected function _update(int $groupId = null, array $updateArray = []) : bool
228
	{
229 2
		$groupModel = new Admin\Model\Group();
230 2
		return $groupModel->updateByIdAndArray($groupId, $updateArray);
231
	}
232
233
	/**
234
	 * get success route
235
	 *
236
	 * @since 4.0.0
237
	 *
238
	 * @param array $postArray array of the post
239
	 *
240
	 * @return string
241
	 */
242
243 3
	protected function _getSuccessRoute(array $postArray = []) : string
244
	{
245 3
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
246
		{
247 1
			return 'admin/view/groups#row-' . $postArray['id'];
248
		}
249 2
		if ($this->_registry->get('groupsEdit') && $postArray['alias'])
250
		{
251 1
			$groupModel = new Admin\Model\Group();
252 1
			return 'admin/view/groups#row-' . $groupModel->getByAlias($postArray['alias'])->id;
253
		}
254 1
		return 'admin';
255
	}
256
257
	/**
258
	 * get error route
259
	 *
260
	 * @since 4.0.0
261
	 *
262
	 * @param array $postArray array of the post
263
	 *
264
	 * @return string
265
	 */
266
267 7
	protected function _getErrorRoute(array $postArray = []) : string
268
	{
269 7
		if ($this->_registry->get('groupsEdit') && $postArray['id'])
270
		{
271 1
			return 'admin/edit/groups/' . $postArray['id'];
272
		}
273 6
		if ($this->_registry->get('groupsNew'))
274
		{
275 5
			return 'admin/new/groups';
276
		}
277 1
		return 'admin';
278
	}
279
}
280