Total Complexity | 42 |
Total Lines | 464 |
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 | $iptablesWatchingFolder = rtrim($this->getConfig('iptables.config.watching_folder'), '\\/ '); |
||
207 | |||
208 | if ($enableiptables) { |
||
209 | if (empty($iptablesWatchingFolder)) { |
||
210 | // @codeCoverageIgnoreStart |
||
211 | $iptablesWatchingFolder = $this->directory . '/iptables'; |
||
212 | // @codeCoverageIgnoreEnd |
||
213 | } |
||
214 | |||
215 | $this->setConfig('iptables.config.watching_folder', $iptablesWatchingFolder); |
||
216 | |||
217 | if (!is_dir($iptablesWatchingFolder)) { |
||
218 | // @codeCoverageIgnoreStart |
||
219 | $originalUmask = umask(0); |
||
220 | mkdir($iptablesWatchingFolder, 0777, true); |
||
221 | umask($originalUmask); |
||
222 | // @codeCoverageIgnoreEnd |
||
223 | } |
||
224 | |||
225 | // Create default log files. |
||
226 | if (is_writable($iptablesWatchingFolder)) { |
||
227 | fopen($iptablesWatchingFolder . '/iptables_queue.log', 'w+'); |
||
228 | fopen($iptablesWatchingFolder . '/ipv4_status.log', 'w+'); |
||
229 | fopen($iptablesWatchingFolder . '/ipv6_status.log', 'w+'); |
||
230 | fopen($iptablesWatchingFolder . '/ipv4_command.log', 'w+'); |
||
231 | fopen($iptablesWatchingFolder . '/ipv6_command.log', 'w+'); |
||
232 | |||
233 | return $result; |
||
234 | } |
||
235 | |||
236 | // @codeCoverageIgnoreStart |
||
237 | $result = false; |
||
238 | |||
239 | $this->pushMessage( |
||
240 | 'error', |
||
241 | __( |
||
242 | 'panel', |
||
243 | 'error_ip6tables_directory_not_writable', |
||
244 | 'iptables watching folder requires the storage directory writable.' |
||
245 | ) |
||
246 | ); |
||
247 | // @codeCoverageIgnoreEnd |
||
248 | } |
||
249 | |||
250 | return $result; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Check the settings of Data drivers. |
||
255 | * |
||
256 | * @param bool $result The result passed from previous check. |
||
257 | * |
||
258 | * @return bool |
||
259 | */ |
||
260 | protected function saveConfigCheckDataDriver(bool $result): bool |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Check the settings of Data drivers. |
||
283 | * |
||
284 | * @param bool $result The result passed from previous check. |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | protected function saveCofigCheckDataDriverMySql(bool $result): bool |
||
336 | |||
337 | // @codeCoverageIgnoreEnd |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Check the settings of Data drivers. |
||
342 | * |
||
343 | * @param bool $result The result passed from previous check. |
||
344 | * |
||
345 | * @return bool |
||
346 | */ |
||
347 | protected function saveCofigCheckDataDriverSqlLite(bool $result): bool |
||
420 | |||
421 | // @codeCoverageIgnoreEnd |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Check the settings of Data drivers. |
||
426 | * |
||
427 | * @param bool $result The result passed from previous check. |
||
428 | * |
||
429 | * @return bool |
||
430 | */ |
||
431 | protected function saveCofigCheckDataDriverRedis(bool $result): bool |
||
465 | |||
466 | // @codeCoverageIgnoreEnd |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Check the settings of Data drivers. |
||
471 | * |
||
472 | * @param bool $result The result passed from previous check. |
||
473 | * |
||
474 | * @return bool |
||
475 | */ |
||
476 | protected function saveCofigCheckDataDriverFile(bool $result): bool |
||
510 | } |
||
511 | } |
||
512 |