| Total Complexity | 80 |
| Total Lines | 427 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PbxExtensionState 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 PbxExtensionState, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class PbxExtensionState extends Injectable |
||
| 38 | { |
||
| 39 | private array $messages; |
||
| 40 | private $lic_feature_id; |
||
| 41 | private string $moduleUniqueID; |
||
| 42 | private ?ConfigClass $configClass; |
||
| 43 | private $modulesRoot; |
||
| 44 | |||
| 45 | |||
| 46 | public function __construct(string $moduleUniqueID) |
||
| 47 | { |
||
| 48 | $this->configClass = null; |
||
| 49 | $this->messages = []; |
||
| 50 | $this->moduleUniqueID = $moduleUniqueID; |
||
| 51 | $this->modulesRoot = $this->getDI()->getShared(ConfigProvider::SERVICE_NAME)->path('core.modulesDir'); |
||
| 52 | $moduleJson = "{$this->modulesRoot}/{$this->moduleUniqueID}/module.json"; |
||
| 53 | if ( ! file_exists($moduleJson)) { |
||
| 54 | $this->messages[] = 'module.json not found for module ' . $this->moduleUniqueID; |
||
| 55 | |||
| 56 | return; |
||
| 57 | } |
||
| 58 | |||
| 59 | $jsonString = file_get_contents($moduleJson); |
||
| 60 | $jsonModuleDescription = json_decode($jsonString, true); |
||
| 61 | if ( ! is_array($jsonModuleDescription)) { |
||
| 62 | $this->messages[] = 'module.json parsing error ' . $this->moduleUniqueID; |
||
| 63 | |||
| 64 | return; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (array_key_exists('lic_feature_id', $jsonModuleDescription)) { |
||
| 68 | $this->lic_feature_id = $jsonModuleDescription['lic_feature_id']; |
||
| 69 | } else { |
||
| 70 | $this->lic_feature_id = 0; |
||
| 71 | } |
||
| 72 | $this->reloadConfigClass(); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Recreates module's ClassNameConf class |
||
| 77 | */ |
||
| 78 | private function reloadConfigClass(): void |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Enables extension module with checking relations |
||
| 91 | * |
||
| 92 | */ |
||
| 93 | public function enableModule(): bool |
||
| 94 | { |
||
| 95 | if ($this->lic_feature_id > 0) { |
||
| 96 | // Try to capture feature if it set |
||
| 97 | $result = $this->license->featureAvailable($this->lic_feature_id); |
||
| 98 | if ($result['success'] === false) { |
||
| 99 | $this->messages[] = $this->license->translateLicenseErrorMessage($result['error']); |
||
| 100 | |||
| 101 | return false; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | $success = $this->makeBeforeEnableTest(); |
||
| 105 | if ( ! $success) { |
||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | // Если ошибок нет, включаем Firewall и модуль |
||
| 110 | if ( ! $this->enableFirewallSettings()) { |
||
| 111 | $this->messages[] = 'Error on enable firewall settings'; |
||
| 112 | |||
| 113 | return false; |
||
| 114 | } |
||
| 115 | if ($this->configClass !== null |
||
| 116 | && method_exists($this->configClass, ConfigClass::ON_BEFORE_MODULE_ENABLE)) { |
||
| 117 | call_user_func([$this->configClass, ConfigClass::ON_BEFORE_MODULE_ENABLE]); |
||
| 118 | } |
||
| 119 | $module = PbxExtensionModules::findFirstByUniqid($this->moduleUniqueID); |
||
| 120 | if ($module !== null) { |
||
| 121 | $module->disabled = '0'; |
||
| 122 | $module->save(); |
||
| 123 | } |
||
| 124 | if ($this->configClass !== null |
||
| 125 | && method_exists($this->configClass, 'getMessages')) { |
||
| 126 | $this->messages = array_merge($this->messages, $this->configClass->getMessages()); |
||
| 127 | } |
||
| 128 | |||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * On enable module this method restores previous firewall settings or sets default state. |
||
| 134 | * |
||
| 135 | * @return bool |
||
| 136 | */ |
||
| 137 | protected function enableFirewallSettings(): bool |
||
| 138 | { |
||
| 139 | if ($this->configClass === null |
||
| 140 | || method_exists($this->configClass, ConfigClass::GET_DEFAULT_FIREWALL_RULES) === false |
||
| 141 | || call_user_func([$this->configClass, ConfigClass::GET_DEFAULT_FIREWALL_RULES]) === [] |
||
| 142 | ) { |
||
| 143 | return true; |
||
| 144 | } |
||
| 145 | |||
| 146 | $this->db->begin(true); |
||
| 147 | $defaultRules = call_user_func([$this->configClass, ConfigClass::GET_DEFAULT_FIREWALL_RULES]); |
||
| 148 | $previousRuleSettings = PbxSettings::findFirstByKey("{$this->moduleUniqueID}FirewallSettings"); |
||
| 149 | $previousRules = []; |
||
| 150 | if ($previousRuleSettings !== null) { |
||
| 151 | $previousRules = json_decode($previousRuleSettings->value, true); |
||
| 152 | $previousRuleSettings->delete(); |
||
| 153 | } |
||
| 154 | $errors = []; |
||
| 155 | $networks = NetworkFilters::find(); |
||
| 156 | $key = strtoupper(key($defaultRules)); |
||
| 157 | $record = $defaultRules[key($defaultRules)]; |
||
| 158 | |||
| 159 | $oldRules = FirewallRules::findByCategory($key); |
||
| 160 | if ($oldRules->count() > 0) { |
||
| 161 | $oldRules->delete(); |
||
| 162 | } |
||
| 163 | |||
| 164 | foreach ($networks as $network) { |
||
| 165 | foreach ($record['rules'] as $detailRule) { |
||
| 166 | $newRule = new FirewallRules(); |
||
| 167 | $newRule->networkfilterid = $network->id; |
||
| 168 | $newRule->protocol = $detailRule['protocol']; |
||
| 169 | $newRule->portfrom = $detailRule['portfrom']; |
||
| 170 | $newRule->portto = $detailRule['portto']; |
||
| 171 | $newRule->category = $key; |
||
| 172 | $newRule->action = $record['action']; |
||
| 173 | $newRule->portFromKey = $detailRule['portFromKey']; |
||
| 174 | $newRule->portToKey = $detailRule['portToKey']; |
||
| 175 | $newRule->description = $detailRule['name']; |
||
| 176 | |||
| 177 | if (array_key_exists($network->id, $previousRules)) { |
||
| 178 | $newRule->action = $previousRules[$network->id]; |
||
| 179 | } |
||
| 180 | if ( ! $newRule->save()) { |
||
| 181 | $errors[] = $newRule->getMessages(); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | if (count($errors) > 0) { |
||
| 186 | $this->messages[] = array_merge($this->messages, $errors); |
||
| 187 | $this->db->rollback(true); |
||
| 188 | |||
| 189 | return false; |
||
| 190 | } |
||
| 191 | |||
| 192 | $this->db->commit(true); |
||
| 193 | |||
| 194 | return true; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Disables extension module with checking relations |
||
| 199 | * |
||
| 200 | */ |
||
| 201 | public function disableModule(): bool |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Makes before disable test to check dependency |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | private function makeBeforeDisableTest(): bool |
||
| 248 | { |
||
| 249 | // Проверим, нет ли настроенных зависимостей у других модулей |
||
| 250 | // Попробуем удалить все настройки модуля |
||
| 251 | $this->db->begin(true); |
||
| 252 | $success = true; |
||
| 253 | |||
| 254 | if ($this->configClass !== null |
||
| 255 | && method_exists($this->configClass, ConfigClass::ON_BEFORE_MODULE_DISABLE) |
||
| 256 | && call_user_func([$this->configClass, ConfigClass::ON_BEFORE_MODULE_DISABLE]) === false) { |
||
| 257 | $messages = $this->configClass->getMessages(); |
||
| 258 | if ( ! empty($messages)) { |
||
| 259 | $this->messages = $messages; |
||
| 260 | } else { |
||
| 261 | $this->messages[] = 'Error on the Module enable function at onBeforeModuleDisable'; |
||
| 262 | } |
||
| 263 | $this->db->rollback(true); // Откатываем временную транзакцию |
||
| 264 | |||
| 265 | return false; |
||
| 266 | } |
||
| 267 | |||
| 268 | // Попытаемся удалить текущий модуль, если ошибок не будет, значит можно выклчать |
||
| 269 | // Например на модуль может ссылаться запись в таблице Extensions, которую надо удалить при отключении |
||
| 270 | // модуля |
||
| 271 | $modelsFiles = glob("{$this->modulesRoot}/{$this->moduleUniqueID}/Models/*.php", GLOB_NOSORT); |
||
| 272 | foreach ($modelsFiles as $file) { |
||
| 273 | $className = pathinfo($file)['filename']; |
||
| 274 | $moduleModelClass = "\\Modules\\{$this->moduleUniqueID}\\Models\\{$className}"; |
||
| 275 | try { |
||
| 276 | if ( ! class_exists($moduleModelClass)) { |
||
| 277 | continue; |
||
| 278 | } |
||
| 279 | $reflection = new ReflectionClass($moduleModelClass); |
||
| 280 | if ($reflection->isAbstract()) { |
||
| 281 | continue; |
||
| 282 | } |
||
| 283 | if (count($reflection->getProperties()) === 0) { |
||
| 284 | continue; |
||
| 285 | } |
||
| 286 | $records = $moduleModelClass::find(); |
||
| 287 | foreach ($records as $record) { |
||
| 288 | if ( ! $record->beforeDelete()) { |
||
| 289 | foreach ($record->getMessages() as $message) { |
||
| 290 | $this->messages[] = $message->getMessage(); |
||
| 291 | } |
||
| 292 | $success = false; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } catch (Throwable $exception) { |
||
| 296 | $this->messages[] = $exception->getMessage(); |
||
| 297 | $success = false; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | if ($success) { |
||
| 301 | $this->messages = []; |
||
| 302 | } |
||
| 303 | |||
| 304 | // Откатываем временную транзакцию |
||
| 305 | $this->db->rollback(true); |
||
| 306 | |||
| 307 | return $success; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Saves firewall state before disable module |
||
| 312 | * |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | protected function disableFirewallSettings(): bool |
||
| 316 | { |
||
| 317 | if ($this->configClass === null |
||
| 318 | || method_exists($this->configClass, ConfigClass::GET_DEFAULT_FIREWALL_RULES) === false |
||
| 319 | || call_user_func([$this->configClass, ConfigClass::GET_DEFAULT_FIREWALL_RULES]) === [] |
||
| 320 | ) { |
||
| 321 | return true; |
||
| 322 | } |
||
| 323 | $errors = []; |
||
| 324 | $savedState = []; |
||
| 325 | $defaultRules = call_user_func([$this->configClass, ConfigClass::GET_DEFAULT_FIREWALL_RULES]); |
||
| 326 | $key = strtoupper(key($defaultRules)); |
||
| 327 | $currentRules = FirewallRules::findByCategory($key); |
||
| 328 | foreach ($currentRules as $detailRule) { |
||
| 329 | $savedState[$detailRule->networkfilterid] = $detailRule->action; |
||
| 330 | } |
||
| 331 | $this->db->begin(true); |
||
| 332 | if ( ! $currentRules->delete()) { |
||
| 333 | $this->messages[] = $currentRules->getMessages(); |
||
| 334 | |||
| 335 | return false; |
||
| 336 | } |
||
| 337 | |||
| 338 | $previousRuleSettings = PbxSettings::findFirstByKey("{$this->moduleUniqueID}FirewallSettings"); |
||
| 339 | if ($previousRuleSettings === null) { |
||
| 340 | $previousRuleSettings = new PbxSettings(); |
||
| 341 | $previousRuleSettings->key = "{$this->moduleUniqueID}FirewallSettings"; |
||
| 342 | } |
||
| 343 | $previousRuleSettings->value = json_encode($savedState); |
||
| 344 | if ( ! $previousRuleSettings->save()) { |
||
| 345 | $errors[] = $previousRuleSettings->getMessages(); |
||
| 346 | } |
||
| 347 | if (count($errors) > 0) { |
||
| 348 | $this->messages[] = array_merge($this->messages, $errors); |
||
| 349 | $this->db->rollback(true); |
||
| 350 | |||
| 351 | return false; |
||
| 352 | } |
||
| 353 | |||
| 354 | $this->db->commit(true); |
||
| 355 | |||
| 356 | return true; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Returns messages after function or methods execution |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | public function getMessages(): array |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Makes before enable test to check dependency |
||
| 371 | * |
||
| 372 | * @return bool |
||
| 373 | */ |
||
| 374 | private function makeBeforeEnableTest(): bool |
||
| 464 | } |
||
| 465 | |||
| 466 | } |