Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

Extra   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 11
dl 0
loc 255
ccs 0
cts 99
cp 0
rs 10
c 0
b 0
f 0

7 Methods

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