Completed
Push — master ( 9458ed...7d322b )
by Henry
10:04
created

Setting::_validatePost()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 7
cts 7
cp 1
rs 8.5066
c 0
b 0
f 0
cc 7
nc 18
nop 1
crap 7
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 setting request
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Controller
15
 * @author Henry Ruhs
16
 */
17
18
class Setting 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 4
30
	public function process(string $action = null) : string
31 4
	{
32 4
		$postArray = $this->_normalizePost($this->_sanitizePost());
33
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 32 can also be of type null; however, Redaxscript\Admin\Contro...etting::_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
35
		/* validate post */
36 4
37
		if ($validateArray)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
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...
38 2
		{
39
			return $this->_error(
40 2
			[
41 2
				'route' => $this->_getErrorRoute(),
42
				'message' => $validateArray
43
			]);
44
		}
45
46
		/* handle update */
47 2
48
		if ($action === 'update')
49
		{
50
			$updateArray =
51 1
			[
52 1
				'language' => $postArray['language'],
53 1
				'template' => $postArray['template'],
54 1
				'title' => $postArray['title'],
55 1
				'author' => $postArray['author'],
56 1
				'copyright' => $postArray['copyright'],
57 1
				'description' => $postArray['description'],
58 1
				'keywords' => $postArray['keywords'],
59 1
				'robots' => $postArray['robots'],
60 1
				'email' => $postArray['email'],
61 1
				'subject' => $postArray['subject'],
62 1
				'notification' => $postArray['notification'],
63 1
				'charset' => $postArray['charset'],
64 1
				'divider' => $postArray['divider'],
65 1
				'zone' => $postArray['zone'],
66 1
				'time' => $postArray['time'],
67 1
				'date' => $postArray['date'],
68 1
				'homepage' => $postArray['homepage'],
69 1
				'limit' => $postArray['limit'],
70 1
				'order' => $postArray['order'],
71 1
				'pagination' => $postArray['pagination'],
72 1
				'moderation' => $postArray['moderation'],
73 1
				'registration' => $postArray['registration'],
74 1
				'verification' => $postArray['verification'],
75 1
				'recovery' => $postArray['recovery'],
76
				'captcha' => $postArray['captcha']
77 1
			];
78
			if ($this->_update($updateArray))
79 1
			{
80
				return $this->_success(
81 1
				[
82
					'route' => 'admin',
83
					'timeout' => 2
84
				]);
85
			}
86
		}
87
88
		/* handle error */
89 1
90
		return $this->_error(
91 1
		[
92
			'route' => $this->_getErrorRoute()
93
		]);
94
	}
95
96
	/**
97
	 * sanitize the post
98
	 *
99
	 * @since 4.0.0
100
	 *
101
	 * @return array
102
	 */
103 4
104
	protected function _sanitizePost() : array
105 4
	{
106 4
		$emailFilter = new Filter\Email();
107 4
		$nameFilter = new Filter\Name();
108 4
		$numberFilter = new Filter\Number();
109
		$specialFilter = new Filter\Special();
110
		$toggleFilter = new Filter\Toggle();
111
112
		/* sanitize post */
113
114 4
		return
115 4
		[
116 4
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
0 ignored issues
show
Bug introduced by Henry Ruhs
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...
117 4
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
0 ignored issues
show
Bug introduced by Henry Ruhs
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...
118 4
			'title' => $nameFilter->sanitize($this->_request->getPost('title')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('title') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Name::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...
119 4
			'author' => $nameFilter->sanitize($this->_request->getPost('author')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('author') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Name::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...
120 4
			'copyright' => $this->_request->getPost('copyright'),
121 4
			'description' => $this->_request->getPost('description'),
122 4
			'keywords' => $this->_request->getPost('keywords'),
123 4
			'robots' => $this->_request->getPost('robots'),
124 4
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('email') targeting Redaxscript\Request::getPost() can also be of type array; however, Redaxscript\Filter\Email::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...
125 4
			'subject' => $this->_request->getPost('subject'),
126 4
			'notification' => $toggleFilter->sanitize($this->_request->getPost('notification')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('notification') 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...
127 4
			'charset' => $this->_request->getPost('charset'),
128 4
			'divider' => $this->_request->getPost('divider'),
129 4
			'zone' => $this->_request->getPost('zone'),
130 4
			'time' => $this->_request->getPost('time'),
131 4
			'date' => $this->_request->getPost('date'),
132 4
			'homepage' => $this->_request->getPost('homepage'),
133 4
			'limit' => $numberFilter->sanitize($this->_request->getPost('limit')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('limit') 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...
134 4
			'order' => $specialFilter->sanitize($this->_request->getPost('order')),
0 ignored issues
show
Bug introduced by Henry Ruhs
It seems like $this->_request->getPost('order') 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...
135 4
			'pagination' => $toggleFilter->sanitize($this->_request->getPost('pagination')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('pagination') 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...
136 4
			'moderation' => $toggleFilter->sanitize($this->_request->getPost('moderation')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('moderation') 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...
137 4
			'registration' => $toggleFilter->sanitize($this->_request->getPost('registration')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('registration') 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...
138 4
			'verification' => $toggleFilter->sanitize($this->_request->getPost('verification')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('verification') 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...
139
			'recovery' => $toggleFilter->sanitize($this->_request->getPost('recovery')),
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('recovery') 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...
140
			'captcha' => $numberFilter->sanitize($this->_request->getPost('captcha'))
0 ignored issues
show
Bug introduced by redaxmedia
It seems like $this->_request->getPost('captcha') 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...
141
		];
142
	}
143
144
	/**
145
	 * validate the post
146
	 *
147
	 * @since 4.0.0
148
	 *
149
	 * @param array $postArray array of the post
150
	 *
151
	 * @return array
152 4
	 */
153
154 4
	protected function _validatePost(array $postArray = []) : array
155
	{
156
		$nameValidator = new Validator\Name();
157
		$userValidator = new Validator\User();
158 4
		$validateArray = [];
159
160 2
		/* validate post */
161
162 4
		if (!$postArray['title'])
163
		{
164
			$validateArray[] = $this->_language->get('title_empty');
165
		}
166
		else if (!$nameValidator->validate($postArray['title']))
167
		{
168
			$validateArray[] = $this->_language->get('title_incorrect');
169
		}
170
		if (!$postArray['author'])
171
		{
172
			$validateArray[] = $this->_language->get('author_empty');
173
		}
174
		else if (!$userValidator->validate($postArray['author']))
175 1
		{
176
			$validateArray[] = $this->_language->get('author_incorrect');
177 1
		}
178 1
		if (!$postArray['charset'] || !$postArray['limit'])
179
		{
180
			$validateArray[] = $this->_language->get('input_empty');
181
		}
182
		return $validateArray;
183
	}
184
185
	/**
186
	 * update the setting
187
	 *
188
	 * @since 4.0.0
189 3
	 *
190
	 * @param array $updateArray array of the update
191 3
	 *
192
	 * @return bool
193 2
	 */
194
195 1
	protected function _update(array $updateArray = []) : bool
196
	{
197
		$settingModel = new Admin\Model\Setting();
198
		return $settingModel->updateByArray($updateArray);
199
	}
200
201
	/**
202
	 * get error route
203
	 *
204
	 * @since 4.1.0
205
	 *
206
	 * @return string
207
	 */
208
209
	protected function _getErrorRoute() : string
210
	{
211
		if ($this->_registry->get('settingsEdit'))
212
		{
213
			return 'admin/edit/settings';
214
		}
215
		return 'admin';
216
	}
217
}
218