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

Setting   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 181
ccs 0
cts 79
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 65 4
A _sanitizePost() 0 38 1
A _validatePost() 0 12 3
A _update() 0 5 1
A _getErrorRoute() 0 8 2
1
<?php
2
namespace Redaxscript\Admin\Controller;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Filter;
6
7
/**
8
 * children class to process the admin setting request
9
 *
10
 * @since 4.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Controller
14
 * @author Henry Ruhs
15
 */
16
17
class Setting extends ControllerAbstract
18
{
19
	/**
20
	 * process the class
21
	 *
22
	 * @since 4.0.0
23
	 *
24
	 * @param string $action action to process
25
	 *
26
	 * @return string
27
	 */
28
29
	public function process(string $action = null) : string
30
	{
31
		$postArray = $this->_normalizePost($this->_sanitizePost());
32
		$validateArray = $this->_validatePost($postArray);
0 ignored issues
show
Bug introduced by
It seems like $postArray defined by $this->_normalizePost($this->_sanitizePost()) on line 31 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...
33
34
		/* validate post */
35
36
		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...
37
		{
38
			return $this->_error(
39
			[
40
				'route' => $this->_getErrorRoute(),
41
				'message' => $validateArray
42
			]);
43
		}
44
45
		/* handle update */
46
47
		if ($action === 'update')
48
		{
49
			$updateArray =
50
			[
51
				'language' => $postArray['language'],
52
				'template' => $postArray['template'],
53
				'title' => $postArray['title'],
54
				'author' => $postArray['author'],
55
				'copyright' => $postArray['copyright'],
56
				'description' => $postArray['description'],
57
				'keywords' => $postArray['keywords'],
58
				'robots' => $postArray['robots'],
59
				'email' => $postArray['email'],
60
				'subject' => $postArray['subject'],
61
				'notification' => $postArray['notification'],
62
				'charset' => $postArray['charset'],
63
				'divider' => $postArray['divider'],
64
				'zone' => $postArray['zone'],
65
				'time' => $postArray['time'],
66
				'date' => $postArray['date'],
67
				'homepage' => $postArray['homepage'],
68
				'limit' => $postArray['limit'],
69
				'order' => $postArray['order'],
70
				'pagination' => $postArray['pagination'],
71
				'moderation' => $postArray['moderation'],
72
				'registration' => $postArray['registration'],
73
				'verification' => $postArray['verification'],
74
				'recovery' => $postArray['recovery'],
75
				'captcha' => $postArray['captcha']
76
			];
77
			if ($this->_update($updateArray))
78
			{
79
				return $this->_success(
80
				[
81
					'route' => 'admin',
82
					'timeout' => 2
83
				]);
84
			}
85
		}
86
87
		/* handle error */
88
89
		return $this->_error(
90
		[
91
			'route' => $this->_getErrorRoute()
92
		]);
93
	}
94
95
	/**
96
	 * sanitize the post
97
	 *
98
	 * @since 4.0.0
99
	 *
100
	 * @return array
101
	 */
102
103
	protected function _sanitizePost() : array
104
	{
105
		$emailFilter = new Filter\Email();
106
		$numberFilter = new Filter\Number();
107
		$specialFilter = new Filter\Special();
108
		$toggleFilter = new Filter\Toggle();
109
110
		/* sanitize post */
111
112
		return
113
		[
114
			'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...
115
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
0 ignored issues
show
Bug introduced by
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...
116
			'title' => $this->_request->getPost('title'),
117
			'author' => $this->_request->getPost('author'),
118
			'copyright' => $this->_request->getPost('copyright'),
119
			'description' => $this->_request->getPost('description'),
120
			'keywords' => $this->_request->getPost('keywords'),
121
			'robots' => $this->_request->getPost('robots'),
122
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
0 ignored issues
show
Bug introduced by
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...
123
			'subject' => $this->_request->getPost('subject'),
124
			'notification' => $toggleFilter->sanitize($this->_request->getPost('notification')),
0 ignored issues
show
Bug introduced by
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...
125
			'charset' => $this->_request->getPost('charset'),
126
			'divider' => $this->_request->getPost('divider'),
127
			'zone' => $this->_request->getPost('zone'),
128
			'time' => $this->_request->getPost('time'),
129
			'date' => $this->_request->getPost('date'),
130
			'homepage' => $this->_request->getPost('homepage'),
131
			'limit' => $numberFilter->sanitize($this->_request->getPost('limit')),
0 ignored issues
show
Bug introduced by
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...
132
			'order' => $specialFilter->sanitize($this->_request->getPost('order')),
0 ignored issues
show
Bug introduced by
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...
133
			'pagination' => $toggleFilter->sanitize($this->_request->getPost('pagination')),
0 ignored issues
show
Bug introduced by
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...
134
			'moderation' => $toggleFilter->sanitize($this->_request->getPost('moderation')),
0 ignored issues
show
Bug introduced by
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...
135
			'registration' => $toggleFilter->sanitize($this->_request->getPost('registration')),
0 ignored issues
show
Bug introduced by
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...
136
			'verification' => $toggleFilter->sanitize($this->_request->getPost('verification')),
0 ignored issues
show
Bug introduced by
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...
137
			'recovery' => $toggleFilter->sanitize($this->_request->getPost('recovery')),
0 ignored issues
show
Bug introduced by
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...
138
			'captcha' => $numberFilter->sanitize($this->_request->getPost('captcha'))
0 ignored issues
show
Bug introduced by
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...
139
		];
140
	}
141
142
	/**
143
	 * validate the post
144
	 *
145
	 * @since 4.0.0
146
	 *
147
	 * @param array $postArray array of the post
148
	 *
149
	 * @return array
150
	 */
151
152
	protected function _validatePost(array $postArray = []) : array
153
	{
154
		$validateArray = [];
155
156
		/* validate post */
157
158
		if (!$postArray['charset'] || !$postArray['limit'])
159
		{
160
			$validateArray[] = $this->_language->get('input_empty');
161
		}
162
		return $validateArray;
163
	}
164
165
	/**
166
	 * update the setting
167
	 *
168
	 * @since 4.0.0
169
	 *
170
	 * @param array $updateArray array of the update
171
	 *
172
	 * @return bool
173
	 */
174
175
	protected function _update(array $updateArray = []) : bool
176
	{
177
		$settingModel = new Admin\Model\Setting();
178
		return $settingModel->updateByArray($updateArray);
179
	}
180
181
	/**
182
	 * get error route
183
	 *
184
	 * @since 4.1.0
185
	 *
186
	 * @return string
187
	 */
188
189
	protected function _getErrorRoute() : string
190
	{
191
		if ($this->_registry->get('settingsEdit'))
192
		{
193
			return 'admin/edit/settings';
194
		}
195
		return 'admin';
196
	}
197
}
198