Total Complexity | 47 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ConfigMethodsTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConfigMethodsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | trait ConfigMethodsTrait |
||
37 | { |
||
38 | /** |
||
39 | * Parse the POST fields and set them into configuration data structure. |
||
40 | * Used for saveConfig method only. |
||
41 | * |
||
42 | * @param array $postParams |
||
43 | * |
||
44 | * @return void |
||
45 | */ |
||
46 | protected function saveConfigPrepareSettings(array $postParams): void |
||
47 | { |
||
48 | foreach ($postParams as $postKey => $postData) { |
||
49 | if (is_string($postData)) { |
||
50 | if ($postData === 'on') { |
||
51 | $this->setConfig(str_replace('__', '.', $postKey), true); |
||
|
|||
52 | |||
53 | } elseif ($postData === 'off') { |
||
54 | $this->setConfig(str_replace('__', '.', $postKey), false); |
||
55 | |||
56 | } else { |
||
57 | if ($postKey === 'ip_variable_source') { |
||
58 | $this->setConfig('ip_variable_source.REMOTE_ADDR', false); |
||
59 | $this->setConfig('ip_variable_source.HTTP_CF_CONNECTING_IP', false); |
||
60 | $this->setConfig('ip_variable_source.HTTP_X_FORWARDED_FOR', false); |
||
61 | $this->setConfig('ip_variable_source.HTTP_X_FORWARDED_HOST', false); |
||
62 | $this->setConfig('ip_variable_source.' . $postData, true); |
||
63 | |||
64 | } elseif ($postKey === 'dialog_ui__shadow_opacity') { |
||
65 | $this->setConfig('dialog_ui.shadow_opacity', (string) $postData); |
||
66 | |||
67 | } elseif ($postKey === 'admin__pass') { |
||
68 | if (strlen($postParams['admin__pass']) < 58) { |
||
69 | $this->setConfig('admin.pass', password_hash($postData, PASSWORD_BCRYPT)); |
||
70 | } |
||
71 | } else if ($postKey === 'messengers__sendgrid__config__recipients') { |
||
72 | $this->setConfig( |
||
73 | 'messengers.sendgrid.config.recipients', |
||
74 | preg_split('/\r\n|[\r\n]/', |
||
75 | $postData) |
||
76 | ); |
||
77 | } else { |
||
78 | if (is_numeric($postData)) { |
||
79 | $this->setConfig(str_replace('__', '.', $postKey), (int) $postData); |
||
80 | } else { |
||
81 | $this->setConfig(str_replace('__', '.', $postKey), $postData); |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Check the settings of Action Logger. |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | protected function saveConfigCheckActionLogger(bool $result): bool |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Check the settings of Iptables. |
||
136 | * |
||
137 | * @param bool $result The result passed from previous check. |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | protected function saveConfigCheckIptables(bool $result): bool |
||
142 | { |
||
143 | if (!$result) { |
||
144 | return false; |
||
145 | } |
||
146 | |||
147 | // System firewall. |
||
148 | $enableIptables = $this->getConfig('iptables.enable'); |
||
149 | $iptablesWatchingFolder = rtrim($this->getConfig('iptables.config.watching_folder'), '\\/ '); |
||
150 | |||
151 | $result = true; |
||
152 | |||
153 | if ($enableIptables) { |
||
154 | if (empty($iptablesWatchingFolder)) { |
||
155 | $iptablesWatchingFolder = $this->directory . '/iptables'; |
||
156 | } |
||
157 | |||
158 | $this->setConfig('iptables.config.watching_folder', $iptablesWatchingFolder); |
||
159 | |||
160 | if (!is_dir($iptablesWatchingFolder)) { |
||
161 | $originalUmask = umask(0); |
||
162 | mkdir($iptablesWatchingFolder, 0777, true); |
||
163 | umask($originalUmask); |
||
164 | |||
165 | // Create default log files. |
||
166 | if (is_writable($iptablesWatchingFolder)) { |
||
167 | fopen($iptablesWatchingFolder . '/iptables_queue.log', 'w+'); |
||
168 | fopen($iptablesWatchingFolder . '/ipv4_status.log', 'w+'); |
||
169 | fopen($iptablesWatchingFolder . '/ipv6_status.log', 'w+'); |
||
170 | fopen($iptablesWatchingFolder . '/ipv4_command.log', 'w+'); |
||
171 | fopen($iptablesWatchingFolder . '/ipv6_command.log', 'w+'); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | if (!is_writable($iptablesWatchingFolder)) { |
||
176 | $result = false; |
||
177 | $this->pushMessage('error', |
||
178 | __( |
||
179 | 'panel', |
||
180 | 'error_ip6tables_directory_not_writable', |
||
181 | 'iptables watching folder requires the storage directory writable.' |
||
182 | ) |
||
183 | ); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | return $result; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Check the settings of Data drivers. |
||
192 | * |
||
193 | * @param bool $result The result passed from previous check. |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | protected function saveConfigCheckDataDriver(bool $result): bool |
||
198 | { |
||
199 | if (!$result) { |
||
200 | return false; |
||
201 | } |
||
202 | |||
203 | switch ($this->configuration['driver_type']) { |
||
204 | case 'mysql': |
||
205 | $result = $this->saveCofigCheckDataDriverMySql($result); |
||
206 | break; |
||
207 | |||
208 | case 'sqlite': |
||
209 | $result = $this->saveCofigCheckDataDriverSqlLite($result); |
||
210 | break; |
||
211 | |||
212 | case 'redis': |
||
213 | $result = $this->saveCofigCheckDataDriverRedis($result); |
||
214 | break; |
||
215 | |||
216 | case 'file': |
||
217 | default: |
||
218 | $result = $this->saveCofigCheckDataDriverFile($result); |
||
219 | // endswitch |
||
220 | } |
||
221 | |||
222 | return $result; |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Check the settings of Data drivers. |
||
227 | * |
||
228 | * @param bool $result The result passed from previous check. |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | protected function saveCofigCheckDataDriverMySql(bool $result): bool |
||
233 | { |
||
234 | if (class_exists('PDO')) { |
||
235 | $db = [ |
||
236 | 'host' => $this->getConfig('drivers.mysql.host'), |
||
237 | 'dbname' => $this->getConfig('drivers.mysql.dbname'), |
||
238 | 'user' => $this->getConfig('drivers.mysql.user'), |
||
239 | 'pass' => $this->getConfig('drivers.mysql.pass'), |
||
240 | 'charset' => $this->getConfig('drivers.mysql.charset'), |
||
241 | ]; |
||
242 | |||
243 | try { |
||
244 | new PDO( |
||
245 | 'mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'] . ';charset=' . $db['charset'], |
||
246 | (string) $db['user'], |
||
247 | (string) $db['pass'] |
||
248 | ); |
||
249 | } catch(PDOException $e) { |
||
250 | $result = false; |
||
251 | $this->pushMessage('error', |
||
252 | __( |
||
253 | 'panel', |
||
254 | 'error_mysql_connection', |
||
255 | 'Cannot access to your MySQL database, please check your settings.' |
||
256 | ) |
||
257 | ); |
||
258 | } |
||
259 | } else { |
||
260 | $result = false; |
||
261 | $this->pushMessage('error', |
||
262 | __( |
||
263 | 'panel', |
||
264 | 'error_mysql_driver_not_supported', |
||
265 | 'Your system doesn’t support MySQL driver.' |
||
266 | ) |
||
267 | ); |
||
268 | } |
||
269 | |||
270 | return $result; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Check the settings of Data drivers. |
||
275 | * |
||
276 | * @param bool $result The result passed from previous check. |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | protected function saveCofigCheckDataDriverSqlLite(bool $result): bool |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Check the settings of Data drivers. |
||
332 | * |
||
333 | * @param bool $result The result passed from previous check. |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | protected function saveCofigCheckDataDriverRedis(bool $result): bool |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Check the settings of Data drivers. |
||
367 | * |
||
368 | * @param bool $result The result passed from previous check. |
||
369 | * |
||
370 | * @return bool |
||
371 | */ |
||
372 | protected function saveCofigCheckDataDriverFile(bool $result): bool |
||
401 | } |
||
402 | } |
||
403 |