Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

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