| Total Complexity | 58 |
| Total Lines | 394 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UpdateSystemConfig 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 UpdateSystemConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class UpdateSystemConfig extends Di\Injectable |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var \Phalcon\Config |
||
| 33 | */ |
||
| 34 | private $config; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var MikoPBXConfig |
||
| 38 | */ |
||
| 39 | private $mikoPBXConfig; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * System constructor. |
||
| 43 | */ |
||
| 44 | public function __construct() |
||
| 45 | { |
||
| 46 | $this->config = $this->di->getShared('config'); |
||
| 47 | $this->mikoPBXConfig = new MikoPBXConfig(); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Update settings after every new release |
||
| 52 | */ |
||
| 53 | public function updateConfigs(): bool |
||
| 54 | { |
||
| 55 | $this->deleteLostModules(); |
||
| 56 | |||
| 57 | $previous_version = str_ireplace('-dev', '', $this->mikoPBXConfig->getGeneralSettings('PBXVersion')); |
||
| 58 | $current_version = str_ireplace('-dev', '', trim(file_get_contents('/etc/version'))); |
||
| 59 | if ($previous_version !== $current_version) { |
||
| 60 | |||
| 61 | if (version_compare($previous_version, '1.0.0', '<=')) { |
||
| 62 | $this->fillInitialSettings(); |
||
| 63 | $previous_version = '1.0.1'; |
||
| 64 | Util::echoWithSyslog(' - UpdateConfigs: Upgrade applications up tp '.$previous_version.' '); |
||
| 65 | Util::echoGreenDone(); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (version_compare($previous_version, '6.2.110', '<')) { |
||
| 69 | $this->updateConfigsUpToVer62110(); |
||
| 70 | $previous_version = '6.2.110'; |
||
| 71 | Util::echoWithSyslog(' - UpdateConfigs: Upgrade applications up tp '.$previous_version.' '); |
||
| 72 | Util::echoGreenDone(); |
||
| 73 | } |
||
| 74 | |||
| 75 | if (version_compare($previous_version, '6.4', '<')) { |
||
| 76 | $this->updateConfigsUpToVer64(); |
||
| 77 | $previous_version = '6.4'; |
||
| 78 | Util::echoWithSyslog(' - UpdateConfigs: Upgrade applications up tp '.$previous_version.' '); |
||
| 79 | Util::echoGreenDone(); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (version_compare($previous_version, '2020.1.62', '<')) { |
||
| 83 | $this->updateConfigsUpToVer2020162(); |
||
| 84 | $previous_version = '2020.1.62'; |
||
| 85 | Util::echoWithSyslog(' - UpdateConfigs: Upgrade applications up tp '.$previous_version.' '); |
||
| 86 | Util::echoGreenDone(); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (version_compare($previous_version, '2020.2.314', '<')) { |
||
| 90 | $this->updateConfigsUpToVer20202573(); |
||
| 91 | $previous_version = '2020.2.573'; |
||
| 92 | Util::echoWithSyslog(' - UpdateConfigs: Upgrade applications up tp '.$previous_version.' '); |
||
| 93 | Util::echoGreenDone(); |
||
| 94 | } |
||
| 95 | |||
| 96 | //... add new updates procedures above this comment line... // |
||
| 97 | |||
| 98 | $this->updateConfigEveryRelease(); |
||
| 99 | |||
| 100 | $this->mikoPBXConfig->setGeneralSettings('PBXVersion', trim(file_get_contents('/etc/version'))); |
||
| 101 | |||
| 102 | } |
||
| 103 | |||
| 104 | return true; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Delete modules, not installed on the system |
||
| 109 | */ |
||
| 110 | private function deleteLostModules(): void |
||
| 111 | { |
||
| 112 | /** @var \MikoPBX\Common\Models\PbxExtensionModules $modules */ |
||
| 113 | $modules = PbxExtensionModules::find(); |
||
| 114 | foreach ($modules as $module) { |
||
| 115 | if ( ! is_dir("{$this->config->path('core.modulesDir')}/{$module->uniqid}")) { |
||
| 116 | $module->delete(); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Every new release routines |
||
| 123 | */ |
||
| 124 | private function updateConfigEveryRelease():void |
||
| 125 | { |
||
| 126 | Storage::clearSessionsFiles(); |
||
| 127 | IptablesConf::updateFirewallRules(); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * First bootup |
||
| 132 | */ |
||
| 133 | private function fillInitialSettings(): void |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | |||
| 154 | /** |
||
| 155 | * Upgrade from * to 6.2.110 |
||
| 156 | */ |
||
| 157 | private function updateConfigsUpToVer62110(): void |
||
| 158 | { |
||
| 159 | /** @var \MikoPBX\Common\Models\Codecs $codec_g722 */ |
||
| 160 | $codec_g722 = Codecs::findFirst('name="g722"'); |
||
| 161 | if ($codec_g722 === null) { |
||
| 162 | $codec_g722 = new Codecs(); |
||
| 163 | $codec_g722->name = 'g722'; |
||
| 164 | $codec_g722->type = 'audio'; |
||
| 165 | $codec_g722->description = 'G.722'; |
||
| 166 | $codec_g722->save(); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** @var \MikoPBX\Common\Models\IvrMenu $ivrs */ |
||
| 170 | /** @var \MikoPBX\Common\Models\IvrMenu $ivr */ |
||
| 171 | $ivrs = IvrMenu::find(); |
||
| 172 | foreach ($ivrs as $ivr) { |
||
| 173 | if (empty($ivr->number_of_repeat)) { |
||
| 174 | $ivr->number_of_repeat = 5; |
||
| 175 | $ivr->save(); |
||
| 176 | } |
||
| 177 | if (empty($ivr->timeout)) { |
||
| 178 | $ivr->timeout = 7; |
||
| 179 | $ivr->save(); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | // Чистим мусор. |
||
| 184 | /** @var \MikoPBX\Common\Models\PbxExtensionModules $modules */ |
||
| 185 | $modules = PbxExtensionModules::find(); |
||
| 186 | foreach ($modules as $module) { |
||
| 187 | if ($module->version === '1.0' && empty($module->support_email) && 'МИКО' === $module->developer) { |
||
| 188 | $module->delete(); |
||
| 189 | } |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Upgrade from 6.2.110 to 6.4 |
||
| 195 | */ |
||
| 196 | private function updateConfigsUpToVer64(): void |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | private function updateConfigsUpToVer2020162(): void |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Update to 2020.2.314 |
||
| 294 | */ |
||
| 295 | private function updateConfigsUpToVer20202573(): void |
||
| 423 | } |
||
| 424 | } |
||
| 425 | |||
| 426 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..