Completed
Push — master ( aaa756...6a04f6 )
by Henry
70:00 queued 35:28
created

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