Setting   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 96.84%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 9
dl 0
loc 204
ccs 92
cts 95
cp 0.9684
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 67 4
A _sanitizePost() 0 41 1
B _validatePost() 0 30 7
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
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
30 4
	public function process(string $action = null) : string
31
	{
32 4
		$postArray = $this->_normalizePost($this->_sanitizePost());
33 4
		$validateArray = $this->_validatePost($postArray);
34
35
		/* validate post */
36
37 4
		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...
38
		{
39 2
			return $this->_error(
40
			[
41 2
				'route' => $this->_getErrorRoute(),
42 2
				'message' => $validateArray
43
			]);
44
		}
45
46
		/* handle update */
47
48 2
		if ($action === 'update')
49
		{
50
			$updateArray =
51
			[
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
				'smtp' => $postArray['smtp'],
62 1
				'subject' => $postArray['subject'],
63 1
				'notification' => $postArray['notification'],
64 1
				'charset' => $postArray['charset'],
65 1
				'locale' => $postArray['locale'],
66 1
				'divider' => $postArray['divider'],
67 1
				'zone' => $postArray['zone'],
68 1
				'time' => $postArray['time'],
69 1
				'date' => $postArray['date'],
70 1
				'homepage' => $postArray['homepage'],
71 1
				'limit' => $postArray['limit'],
72 1
				'order' => $postArray['order'],
73 1
				'pagination' => $postArray['pagination'],
74 1
				'moderation' => $postArray['moderation'],
75 1
				'registration' => $postArray['registration'],
76 1
				'verification' => $postArray['verification'],
77 1
				'recovery' => $postArray['recovery'],
78 1
				'captcha' => $postArray['captcha']
79
			];
80 1
			if ($this->_update($updateArray))
81
			{
82 1
				return $this->_success(
83
				[
84 1
					'route' => 'admin',
85
					'timeout' => 2
86
				]);
87
			}
88
		}
89
90
		/* handle error */
91
92 1
		return $this->_error(
93
		[
94 1
			'route' => $this->_getErrorRoute()
95
		]);
96
	}
97
98
	/**
99
	 * sanitize the post
100
	 *
101
	 * @since 4.0.0
102
	 *
103
	 * @return array
104
	 */
105
106 4
	protected function _sanitizePost() : array
107
	{
108 4
		$emailFilter = new Filter\Email();
109 4
		$numberFilter = new Filter\Number();
110 4
		$specialFilter = new Filter\Special();
111 4
		$textFilter = new Filter\Text();
112 4
		$toggleFilter = new Filter\Toggle();
113
114
		/* sanitize post */
115
116
		return
117
		[
118 4
			'language' => $specialFilter->sanitize($this->_request->getPost('language')),
119 4
			'template' => $specialFilter->sanitize($this->_request->getPost('template')),
120 4
			'title' => $textFilter->sanitize($this->_request->getPost('title')),
121 4
			'author' => $textFilter->sanitize($this->_request->getPost('author')),
122 4
			'copyright' => $textFilter->sanitize($this->_request->getPost('copyright')),
123 4
			'description' => $textFilter->sanitize($this->_request->getPost('description')),
124 4
			'keywords' => $textFilter->sanitize($this->_request->getPost('keywords')),
125 4
			'robots' => $numberFilter->sanitize($this->_request->getPost('robots')),
126 4
			'email' => $emailFilter->sanitize($this->_request->getPost('email')),
127 4
			'smtp' => $emailFilter->sanitize($this->_request->getPost('smtp')),
128 4
			'subject' => $textFilter->sanitize($this->_request->getPost('subject')),
129 4
			'notification' => $toggleFilter->sanitize($this->_request->getPost('notification')),
130 4
			'charset' => $textFilter->sanitize($this->_request->getPost('charset')),
131 4
			'locale' => $textFilter->sanitize($this->_request->getPost('locale')),
132 4
			'divider' => $textFilter->sanitize($this->_request->getPost('divider')),
133 4
			'zone' => $textFilter->sanitize($this->_request->getPost('zone')),
134 4
			'time' => $textFilter->sanitize($this->_request->getPost('time')),
135 4
			'date' => $textFilter->sanitize($this->_request->getPost('date')),
136 4
			'homepage' => $numberFilter->sanitize($this->_request->getPost('homepage')),
137 4
			'limit' => $numberFilter->sanitize($this->_request->getPost('limit')),
138 4
			'order' => $specialFilter->sanitize($this->_request->getPost('order')),
139 4
			'pagination' => $toggleFilter->sanitize($this->_request->getPost('pagination')),
140 4
			'moderation' => $toggleFilter->sanitize($this->_request->getPost('moderation')),
141 4
			'registration' => $toggleFilter->sanitize($this->_request->getPost('registration')),
142 4
			'verification' => $toggleFilter->sanitize($this->_request->getPost('verification')),
143 4
			'recovery' => $toggleFilter->sanitize($this->_request->getPost('recovery')),
144 4
			'captcha' => $numberFilter->sanitize($this->_request->getPost('captcha'))
145
		];
146
	}
147
148
	/**
149
	 * validate the post
150
	 *
151
	 * @since 4.0.0
152
	 *
153
	 * @param array $postArray array of the post
154
	 *
155
	 * @return array
156
	 */
157
158 4
	protected function _validatePost(array $postArray = []) : array
159
	{
160 4
		$nameValidator = new Validator\Name();
161 4
		$dnsValidator = new Validator\Dns();
162 4
		$validateArray = [];
163
164
		/* validate post */
165
166 4
		if (!$postArray['title'])
167
		{
168 2
			$validateArray[] = $this->_language->get('title_empty');
169
		}
170 2
		else if (!$nameValidator->validate($postArray['title']))
171
		{
172
			$validateArray[] = $this->_language->get('title_incorrect');
173
		}
174 4
		if (!$postArray['author'])
175
		{
176 2
			$validateArray[] = $this->_language->get('author_empty');
177
		}
178 2
		else if (!$nameValidator->validate($postArray['author']))
179
		{
180
			$validateArray[] = $this->_language->get('author_incorrect');
181
		}
182 4
		if ($postArray['smtp'] && !$dnsValidator->validate($postArray['smtp'], 'mx'))
183
		{
184
			$validateArray[] = $this->_language->get('smtp_incorrect');
185
		}
186 4
		return $validateArray;
187
	}
188
189
	/**
190
	 * update the setting
191
	 *
192
	 * @since 4.0.0
193
	 *
194
	 * @param array $updateArray array of the update
195
	 *
196
	 * @return bool
197
	 */
198
199 1
	protected function _update(array $updateArray = []) : bool
200
	{
201 1
		$settingModel = new Admin\Model\Setting();
202 1
		return $settingModel->updateByArray($updateArray);
203
	}
204
205
	/**
206
	 * get error route
207
	 *
208
	 * @since 4.1.0
209
	 *
210
	 * @return string
211
	 */
212
213 3
	protected function _getErrorRoute() : string
214
	{
215 3
		if ($this->_registry->get('settingsEdit'))
216
		{
217 2
			return 'admin/edit/settings';
218
		}
219 1
		return 'admin';
220
	}
221
}
222