| Total Complexity | 42 |
| Total Lines | 371 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 | |||
| 50 | if (is_string($postData)) { |
||
| 51 | if ($postData === 'on') { |
||
| 52 | $this->setConfig(str_replace('__', '.', $postKey), true); |
||
|
|
|||
| 53 | |||
| 54 | } elseif ($postData === 'off') { |
||
| 55 | $this->setConfig(str_replace('__', '.', $postKey), false); |
||
| 56 | |||
| 57 | } elseif ($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 | |||
| 72 | } else if ($postKey === 'messengers__sendgrid__config__recipients') { |
||
| 73 | $this->setConfig( |
||
| 74 | 'messengers.sendgrid.config.recipients', |
||
| 75 | preg_split('/\r\n|[\r\n]/', |
||
| 76 | $postData) |
||
| 77 | ); |
||
| 78 | |||
| 79 | } elseif (is_numeric($postData)) { |
||
| 80 | $this->setConfig(str_replace('__', '.', $postKey), (int) $postData); |
||
| 81 | |||
| 82 | } else { |
||
| 83 | $this->setConfig(str_replace('__', '.', $postKey), $postData); |
||
| 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 |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Check the settings of Iptables. |
||
| 134 | * |
||
| 135 | * @param bool $result The result passed from previous check. |
||
| 136 | * |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | protected function saveConfigCheckIptables(bool $result): bool |
||
| 140 | { |
||
| 141 | if (!$result) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | // System firewall. |
||
| 146 | $enableIptables = $this->getConfig('iptables.enable'); |
||
| 147 | $iptablesWatchingFolder = rtrim($this->getConfig('iptables.config.watching_folder'), '\\/ '); |
||
| 148 | |||
| 149 | if ($enableIptables) { |
||
| 150 | if (empty($iptablesWatchingFolder)) { |
||
| 151 | $iptablesWatchingFolder = $this->directory . '/iptables'; |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->setConfig('iptables.config.watching_folder', $iptablesWatchingFolder); |
||
| 155 | |||
| 156 | if (!is_dir($iptablesWatchingFolder)) { |
||
| 157 | $originalUmask = umask(0); |
||
| 158 | mkdir($iptablesWatchingFolder, 0777, true); |
||
| 159 | umask($originalUmask); |
||
| 160 | } |
||
| 161 | |||
| 162 | // Create default log files. |
||
| 163 | if (is_writable($iptablesWatchingFolder)) { |
||
| 164 | fopen($iptablesWatchingFolder . '/iptables_queue.log', 'w+'); |
||
| 165 | fopen($iptablesWatchingFolder . '/ipv4_status.log', 'w+'); |
||
| 166 | fopen($iptablesWatchingFolder . '/ipv6_status.log', 'w+'); |
||
| 167 | fopen($iptablesWatchingFolder . '/ipv4_command.log', 'w+'); |
||
| 168 | fopen($iptablesWatchingFolder . '/ipv6_command.log', 'w+'); |
||
| 169 | |||
| 170 | return $result; |
||
| 171 | } |
||
| 172 | |||
| 173 | $result = false; |
||
| 174 | |||
| 175 | $this->pushMessage('error', |
||
| 176 | __( |
||
| 177 | 'panel', |
||
| 178 | 'error_ip6tables_directory_not_writable', |
||
| 179 | 'iptables watching folder requires the storage directory writable.' |
||
| 180 | ) |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $result; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Check the settings of Data drivers. |
||
| 189 | * |
||
| 190 | * @param bool $result The result passed from previous check. |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | protected function saveConfigCheckDataDriver(bool $result): bool |
||
| 195 | { |
||
| 196 | if (!$result) { |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | $type = $this->configuration['driver_type']; |
||
| 201 | |||
| 202 | $methods = [ |
||
| 203 | 'mysql' => 'saveCofigCheckDataDriverMySql', |
||
| 204 | 'sqlite' => 'saveCofigCheckDataDriverSqlLite', |
||
| 205 | 'redis' => 'saveCofigCheckDataDriverRedis', |
||
| 206 | 'file' => 'saveCofigCheckDataDriverFile', |
||
| 207 | ]; |
||
| 208 | |||
| 209 | $method = $methods[$type]; |
||
| 210 | $result = $result = $this->{$method}($result); |
||
| 211 | |||
| 212 | return $result; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Check the settings of Data drivers. |
||
| 217 | * |
||
| 218 | * @param bool $result The result passed from previous check. |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | protected function saveCofigCheckDataDriverMySql(bool $result): bool |
||
| 223 | { |
||
| 224 | if (class_exists('PDO')) { |
||
| 225 | |||
| 226 | $db = [ |
||
| 227 | 'host' => $this->getConfig('drivers.mysql.host'), |
||
| 228 | 'dbname' => $this->getConfig('drivers.mysql.dbname'), |
||
| 229 | 'user' => $this->getConfig('drivers.mysql.user'), |
||
| 230 | 'pass' => $this->getConfig('drivers.mysql.pass'), |
||
| 231 | 'charset' => $this->getConfig('drivers.mysql.charset'), |
||
| 232 | ]; |
||
| 233 | |||
| 234 | try { |
||
| 235 | new PDO( |
||
| 236 | 'mysql:host=' . $db['host'] . ';dbname=' . $db['dbname'] . ';charset=' . $db['charset'], |
||
| 237 | (string) $db['user'], |
||
| 238 | (string) $db['pass'] |
||
| 239 | ); |
||
| 240 | } catch(PDOException $e) { |
||
| 241 | |||
| 242 | $result = false; |
||
| 243 | |||
| 244 | $this->pushMessage('error', |
||
| 245 | __( |
||
| 246 | 'panel', |
||
| 247 | 'error_mysql_connection', |
||
| 248 | 'Cannot access to your MySQL database, please check your settings.' |
||
| 249 | ) |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | return $result; |
||
| 253 | } |
||
| 254 | |||
| 255 | $result = false; |
||
| 256 | |||
| 257 | $this->pushMessage('error', |
||
| 258 | __( |
||
| 259 | 'panel', |
||
| 260 | 'error_mysql_driver_not_supported', |
||
| 261 | 'Your system doesn’t support MySQL driver.' |
||
| 262 | ) |
||
| 263 | ); |
||
| 264 | |||
| 265 | return $result; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Check the settings of Data drivers. |
||
| 270 | * |
||
| 271 | * @param bool $result The result passed from previous check. |
||
| 272 | * |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | protected function saveCofigCheckDataDriverSqlLite(bool $result): bool |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Check the settings of Data drivers. |
||
| 333 | * |
||
| 334 | * @param bool $result The result passed from previous check. |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | protected function saveCofigCheckDataDriverRedis(bool $result): bool |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Check the settings of Data drivers. |
||
| 373 | * |
||
| 374 | * @param bool $result The result passed from previous check. |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | protected function saveCofigCheckDataDriverFile(bool $result): bool |
||
| 407 | } |
||
| 408 | } |
||
| 409 |