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

includes/Admin/Controller/Article.php (21 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 article request
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Controller
17
 * @author Henry Ruhs
18
 */
19
20
class Article 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...rticle::_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...ticle::_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
				'text' => $postArray['text'],
63 1
				'language' => $postArray['language'],
64 1
				'template' => $postArray['template'],
65 1
				'sibling' => $postArray['sibling'],
66 1
				'category' => $postArray['category'],
67 1
				'headline' => $postArray['headline'],
68 1
				'byline' => $postArray['byline'],
69 1
				'comments' => $postArray['comments'],
70 1
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
71 1
				'rank' => $postArray['rank'],
72 1
				'access' => $postArray['access'],
73 1
				'date' => $postArray['date'] ? : $now
74
			];
75 1
			if ($this->_create($createArray))
76
			{
77 1
				return $this->_success(
78
				[
79 1
					'route' => $this->_getSuccessRoute($postArray),
80 1
					'timeout' => 2
81
				]);
82
			}
83
		}
84
85
		/* handle update */
86
87 3
		if ($action === 'update')
88
		{
89
			$updateArray =
90
			[
91 2
				'title' => $postArray['title'],
92 2
				'alias' => $postArray['alias'],
93 2
				'author' => $myName,
94 2
				'description' => $postArray['description'],
95 2
				'keywords' => $postArray['keywords'],
96 2
				'robots' => $postArray['robots'],
97 2
				'text' => $postArray['text'],
98 2
				'language' => $postArray['language'],
99 2
				'template' => $postArray['template'],
100 2
				'sibling' => $postArray['sibling'],
101 2
				'category' => $postArray['category'],
102 2
				'headline' => $postArray['headline'],
103 2
				'byline' => $postArray['byline'],
104 2
				'comments' => $postArray['comments'],
105 2
				'status' => $postArray['date'] > $now ? 2 : $postArray['status'],
106 2
				'rank' => $postArray['rank'],
107 2
				'access' => $postArray['access'],
108 2
				'date' => $postArray['date'] ? : $now
109
			];
110 2
			if ($this->_update($postArray['id'], $updateArray))
111
			{
112 2
				return $this->_success(
113
				[
114 2
					'route' => $this->_getSuccessRoute($postArray),
115 2
					'timeout' => 2
116
				]);
117
			}
118
		}
119
120
		/* handle error */
121
122 1
		return $this->_error(
123
		[
124 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...ticle::_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...
125
		]);
126
	}
127
128
	/**
129
	 * sanitize the post
130
	 *
131
	 * @since 4.0.0
132
	 *
133
	 * @return array
134
	 */
135
136 11
	protected function _sanitizePost() : array
137
	{
138 11
		$aliasFilter = new Filter\Alias();
139 11
		$htmlFilter = new Filter\Html();
140 11
		$numberFilter = new Filter\Number();
141 11
		$specialFilter = new Filter\Special();
142 11
		$textFilter= new Filter\Text();
143 11
		$toggleFilter = new Filter\Toggle();
144
145
		/* sanitize post */
146
147
		return
148
		[
149 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...
150 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...
151 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...
152 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...
153 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...
154 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...
155 11
			'text' => $htmlFilter->sanitize($this->_request->getPost('text'), $this->_registry->get('filter')),
0 ignored issues
show
It seems like $this->_request->getPost('text') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Html::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...
$this->_registry->get('filter') is of type string|array|null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
156 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...
157 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...
158 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...
159 11
			'category' => $numberFilter->sanitize($this->_request->getPost('category')),
0 ignored issues
show
It seems like $this->_request->getPost('category') 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...
160 11
			'headline' => $toggleFilter->sanitize($this->_request->getPost('headline')),
0 ignored issues
show
It seems like $this->_request->getPost('headline') 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...
161 11
			'byline' => $toggleFilter->sanitize($this->_request->getPost('byline')),
0 ignored issues
show
It seems like $this->_request->getPost('byline') 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...
162 11
			'comments' => $toggleFilter->sanitize($this->_request->getPost('comments')),
0 ignored issues
show
It seems like $this->_request->getPost('comments') 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...
163 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...
164 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...
165 11
			'access' => json_encode($this->_request->getPost('access')),
166 11
			'date' => strtotime($this->_request->getPost('date'))
167
		];
168
	}
169
170
	/**
171
	 * validate the post
172
	 *
173
	 * @since 4.0.0
174
	 *
175
	 * @param array $postArray array of the post
176
	 *
177
	 * @return array
178
	 */
179
180 11
	protected function _validatePost(array $postArray = []) : array
181
	{
182 11
		$aliasValidator = new Validator\Alias();
183 11
		$nameValidator = new Validator\Name();
184 11
		$articleModel = new Admin\Model\Article();
185 11
		$validateArray = [];
186
187
		/* validate post */
188
189 11
		if (!$postArray['title'])
190
		{
191 6
			$validateArray[] = $this->_language->get('title_empty');
192
		}
193 5
		else if (!$nameValidator->validate($postArray['title']))
194
		{
195 1
			$validateArray[] = $this->_language->get('title_incorrect');
196
		}
197 11
		if (!$postArray['alias'])
198
		{
199 4
			$validateArray[] = $this->_language->get('alias_empty');
200
		}
201 7
		else if (!$aliasValidator->validate($postArray['alias']) || $aliasValidator->matchSystem($postArray['alias']))
202
		{
203 2
			$validateArray[] = $this->_language->get('alias_incorrect');
204
		}
205 5
		else if (!$articleModel->isUniqueByIdAndAlias($postArray['id'], $postArray['alias']))
206
		{
207 1
			$validateArray[] = $this->_language->get('alias_exists');
208
		}
209 11
		if (!$postArray['text'])
210
		{
211 7
			$validateArray[] = $this->_language->get('article_empty');
212
		}
213 11
		return $validateArray;
214
	}
215
216
	/**
217
	 * create the article
218
	 *
219
	 * @since 4.0.0
220
	 *
221
	 * @param array $createArray array of the create
222
	 *
223
	 * @return bool
224
	 */
225
226 1
	protected function _create(array $createArray = []) : bool
227
	{
228 1
		$articleModel = new Admin\Model\Article();
229 1
		return $articleModel->createByArray($createArray);
230
	}
231
232
	/**
233
	 * update the article
234
	 *
235
	 * @since 4.0.0
236
	 *
237
	 * @param int $articleId identifier of the article
238
	 * @param array $updateArray array of the update
239
	 *
240
	 * @return bool
241
	 */
242
243 2
	protected function _update(int $articleId = null, array $updateArray = []) : bool
244
	{
245 2
		$articleModel = new Admin\Model\Article();
246 2
		return $articleModel->updateByIdAndArray($articleId, $updateArray);
247
	}
248
249
	/**
250
	 * get success route
251
	 *
252
	 * @since 4.0.0
253
	 *
254
	 * @param array $postArray array of the post
255
	 *
256
	 * @return string
257
	 */
258
259 3
	protected function _getSuccessRoute(array $postArray = []) : string
260
	{
261 3
		if ($this->_registry->get('articlesEdit') && $postArray['id'])
262
		{
263 1
			return 'admin/view/articles#row-' . $postArray['id'];
264
		}
265 2
		if ($this->_registry->get('articlesEdit') && $postArray['alias'])
266
		{
267 1
			$articleModel = new Admin\Model\Article();
268 1
			$articleId = $articleModel->getByAlias($postArray['alias'])->id;
269 1
			if ($articleId)
270
			{
271 1
				return 'admin/view/articles#row-' . $articleId;
272
			}
273
			return 'admin/view/articles';
274
		}
275 1
		return 'admin';
276
	}
277
278
	/**
279
	 * get error route
280
	 *
281
	 * @since 4.0.0
282
	 *
283
	 * @param array $postArray array of the post
284
	 *
285
	 * @return string
286
	 */
287
288 8
	protected function _getErrorRoute(array $postArray = []) : string
289
	{
290 8
		if ($this->_registry->get('articlesEdit') && $postArray['id'])
291
		{
292 2
			return 'admin/edit/articles/' . $postArray['id'];
293
		}
294 6
		if ($this->_registry->get('articlesNew'))
295
		{
296 5
			return 'admin/new/articles';
297
		}
298 1
		return 'admin';
299
	}
300
}
301