Completed
Push — master ( dc8f37...8770f3 )
by Henry
15:26 queued 05:23
created

includes/Admin/Controller/Extra.php (6 issues)

Labels
Severity

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