Total Complexity | 41 |
Total Lines | 460 |
Duplicated Lines | 0 % |
Changes | 7 | ||
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 |
||
46 | trait ConfigMethodsTrait |
||
47 | { |
||
48 | /** |
||
49 | * Public methods | Desctiotion |
||
50 | * ----------------------|--------------------------------------------- |
||
51 | * | No public methods. |
||
52 | * ----------------------|--------------------------------------------- |
||
53 | */ |
||
54 | |||
55 | /** |
||
56 | * Get a variable from configuration. |
||
57 | * |
||
58 | * @param string $field The field of the configuration. |
||
59 | * |
||
60 | * @return mixed |
||
61 | */ |
||
62 | abstract public function getConfig(string $field); |
||
63 | |||
64 | /** |
||
65 | * Set a variable to the configuration. |
||
66 | * |
||
67 | * @param string $field The field of the configuration. |
||
68 | * @param mixed $value The vale of a field in the configuration. |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | abstract public function setConfig(string $field, $value): void; |
||
73 | |||
74 | /** |
||
75 | * Response message to front. |
||
76 | * |
||
77 | * @param string $type The message status type. error|success |
||
78 | * @param string $text The message body. |
||
79 | * |
||
80 | * @return void |
||
81 | */ |
||
82 | abstract protected function pushMessage(string $type, string $text): void; |
||
83 | |||
84 | /** |
||
85 | * Parse the POST fields and set them into configuration data structure. |
||
86 | * Used for saveConfig method only. |
||
87 | * |
||
88 | * @param array $postParams The PSR-7 variable of $_POST |
||
89 | * |
||
90 | * @return void |
||
91 | */ |
||
92 | protected function saveConfigPrepareSettings(array $postParams): void |
||
93 | { |
||
94 | foreach ($postParams as $postKey => $postData) { |
||
95 | |||
96 | if (is_string($postData)) { |
||
97 | if ($postData === 'on') { |
||
98 | $this->setConfig(str_replace('__', '.', $postKey), true); |
||
99 | |||
100 | } elseif ($postData === 'off') { |
||
101 | $this->setConfig(str_replace('__', '.', $postKey), false); |
||
102 | |||
103 | } elseif ($postKey === 'ip_variable_source') { |
||
104 | $this->setConfig('ip_variable_source.REMOTE_ADDR', false); |
||
105 | $this->setConfig('ip_variable_source.HTTP_CF_CONNECTING_IP', false); |
||
106 | $this->setConfig('ip_variable_source.HTTP_X_FORWARDED_FOR', false); |
||
107 | $this->setConfig('ip_variable_source.HTTP_X_FORWARDED_HOST', false); |
||
108 | $this->setConfig('ip_variable_source.' . $postData, true); |
||
109 | |||
110 | } elseif ($postKey === 'dialog_ui__shadow_opacity') { |
||
111 | $this->setConfig('dialog_ui.shadow_opacity', (string) $postData); |
||
112 | |||
113 | } elseif ($postKey === 'admin__pass') { |
||
114 | if (strlen($postParams['admin__pass']) < 58) { |
||
115 | $this->setConfig('admin.pass', password_hash($postData, PASSWORD_BCRYPT)); |
||
116 | } |
||
117 | |||
118 | } else if (strpos($postKey, 'config__recipients') !== false) { |
||
119 | // For example: |
||
120 | // messengers__sendgrid__config__recipients |
||
121 | // => messengers.sendgrid.config.recipients |
||
122 | $this->setConfig( |
||
123 | str_replace('__', '.', $postKey), |
||
124 | preg_split( |
||
125 | '/\r\n|[\r\n]/', |
||
126 | $postData |
||
127 | ) |
||
128 | ); |
||
129 | |||
130 | } elseif (is_numeric($postData)) { |
||
131 | $this->setConfig(str_replace('__', '.', $postKey), (int) $postData); |
||
132 | |||
133 | } else { |
||
134 | $this->setConfig(str_replace('__', '.', $postKey), $postData); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Check the settings of Action Logger. |
||
142 | * |
||
143 | * @param bool $result The result passed from previous check. |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | protected function saveConfigCheckActionLogger(bool $result): bool |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Check the settings of iptables. |
||
193 | * |
||
194 | * @param bool $result The result passed from previous check. |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | protected function saveConfigCheckIptables(bool $result): bool |
||
199 | { |
||
200 | if (!$result) { |
||
201 | return false; |
||
202 | } |
||
203 | |||
204 | // System firewall. |
||
205 | $enableiptables = $this->getConfig('iptables.enable'); |
||
206 | |||
207 | // $iptablesWatchingFolder = rtrim($this->getConfig('iptables.config.watching_folder'), '\\/ '); |
||
208 | $iptablesWatchingFolder = $this->directory . '/iptables'; |
||
209 | |||
210 | if ($enableiptables) { |
||
211 | $this->setConfig('iptables.config.watching_folder', $iptablesWatchingFolder); |
||
212 | |||
213 | if (!is_dir($iptablesWatchingFolder)) { |
||
214 | // @codeCoverageIgnoreStart |
||
215 | $originalUmask = umask(0); |
||
216 | mkdir($iptablesWatchingFolder, 0777, true); |
||
217 | umask($originalUmask); |
||
218 | // @codeCoverageIgnoreEnd |
||
219 | } |
||
220 | |||
221 | // Create default log files. |
||
222 | if (is_writable($iptablesWatchingFolder)) { |
||
223 | fopen($iptablesWatchingFolder . '/iptables_queue.log', 'w+'); |
||
224 | fopen($iptablesWatchingFolder . '/ipv4_status.log', 'w+'); |
||
225 | fopen($iptablesWatchingFolder . '/ipv6_status.log', 'w+'); |
||
226 | fopen($iptablesWatchingFolder . '/ipv4_command.log', 'w+'); |
||
227 | fopen($iptablesWatchingFolder . '/ipv6_command.log', 'w+'); |
||
228 | |||
229 | return $result; |
||
230 | } |
||
231 | |||
232 | // @codeCoverageIgnoreStart |
||
233 | $result = false; |
||
234 | |||
235 | $this->pushMessage( |
||
236 | 'error', |
||
237 | __( |
||
238 | 'panel', |
||
239 | 'error_ip6tables_directory_not_writable', |
||
240 | 'iptables watching folder requires the storage directory writable.' |
||
241 | ) |
||
242 | ); |
||
243 | // @codeCoverageIgnoreEnd |
||
244 | } |
||
245 | |||
246 | return $result; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Check the settings of Data drivers. |
||
251 | * |
||
252 | * @param bool $result The result passed from previous check. |
||
253 | * |
||
254 | * @return bool |
||
255 | */ |
||
256 | protected function saveConfigCheckDataDriver(bool $result): bool |
||
257 | { |
||
258 | if (!$result) { |
||
259 | return false; |
||
260 | } |
||
261 | |||
262 | $type = $this->getConfig('driver_type'); |
||
263 | |||
264 | $methods = [ |
||
265 | 'mysql' => 'saveCofigCheckDataDriverMySql', |
||
266 | 'sqlite' => 'saveCofigCheckDataDriverSqlLite', |
||
267 | 'redis' => 'saveCofigCheckDataDriverRedis', |
||
268 | 'file' => 'saveCofigCheckDataDriverFile', |
||
269 | ]; |
||
270 | |||
271 | $method = $methods[$type]; |
||
272 | $result = $this->{$method}($result); |
||
273 | |||
274 | return $result; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Check the settings of Data drivers. |
||
279 | * |
||
280 | * @param bool $result The result passed from previous check. |
||
281 | * |
||
282 | * @return bool |
||
283 | */ |
||
284 | protected function saveCofigCheckDataDriverMySql(bool $result): bool |
||
285 | { |
||
286 | if (class_exists('PDO')) { |
||
287 | |||
288 | $db = [ |
||
289 | 'host' => $this->getConfig('drivers.mysql.host'), |
||
290 | 'dbname' => $this->getConfig('drivers.mysql.dbname'), |
||
291 | 'user' => $this->getConfig('drivers.mysql.user'), |
||
292 | 'pass' => $this->getConfig('drivers.mysql.pass'), |
||
293 | 'charset' => $this->getConfig('drivers.mysql.charset'), |
||
294 | ]; |
||
295 | |||
296 | try { |
||
297 | new PDO( |
||
298 | 'mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'] . ';charset=' . $db['charset'], |
||
299 | (string) $db['user'], |
||
300 | (string) $db['pass'] |
||
301 | ); |
||
302 | } catch (PDOException $e) { |
||
303 | |||
304 | $result = false; |
||
305 | |||
306 | $this->pushMessage( |
||
307 | 'error', |
||
308 | __( |
||
309 | 'panel', |
||
310 | 'error_mysql_connection', |
||
311 | 'Cannot access to your MySQL database, please check your settings.' |
||
312 | ) |
||
313 | ); |
||
314 | } |
||
315 | return $result; |
||
316 | } |
||
317 | |||
318 | // @codeCoverageIgnoreStart |
||
319 | |||
320 | $result = false; |
||
321 | |||
322 | $this->pushMessage( |
||
323 | 'error', |
||
324 | __( |
||
325 | 'panel', |
||
326 | 'error_mysql_driver_not_supported', |
||
327 | 'Your system doesn’t support MySQL driver.' |
||
328 | ) |
||
329 | ); |
||
330 | |||
331 | return $result; |
||
332 | |||
333 | // @codeCoverageIgnoreEnd |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Check the settings of Data drivers. |
||
338 | * |
||
339 | * @param bool $result The result passed from previous check. |
||
340 | * |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function saveCofigCheckDataDriverSqlLite(bool $result): bool |
||
416 | |||
417 | // @codeCoverageIgnoreEnd |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Check the settings of Data drivers. |
||
422 | * |
||
423 | * @param bool $result The result passed from previous check. |
||
424 | * |
||
425 | * @return bool |
||
426 | */ |
||
427 | protected function saveCofigCheckDataDriverRedis(bool $result): bool |
||
461 | |||
462 | // @codeCoverageIgnoreEnd |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * Check the settings of Data drivers. |
||
467 | * |
||
468 | * @param bool $result The result passed from previous check. |
||
469 | * |
||
470 | * @return bool |
||
471 | */ |
||
472 | protected function saveCofigCheckDataDriverFile(bool $result): bool |
||
506 | } |
||
507 | } |
||
508 |