| Total Complexity | 43 |
| Total Lines | 297 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UpdateConfigsUpToVer20202754 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 UpdateConfigsUpToVer20202754, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class UpdateConfigsUpToVer20202754 extends Injectable implements UpgradeSystemConfigInterface |
||
| 22 | { |
||
| 23 | public const PBX_VERSION = '2020.2.754'; |
||
| 24 | |||
| 25 | private ConfigAlias $config; |
||
| 26 | |||
| 27 | private MikoPBXConfig $mikoPBXConfig; |
||
| 28 | |||
| 29 | private bool $isLiveCD; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Class constructor. |
||
| 33 | */ |
||
| 34 | public function __construct() |
||
| 35 | { |
||
| 36 | $this->config = $this->getDI()->getShared('config'); |
||
| 37 | $this->mikoPBXConfig = new MikoPBXConfig(); |
||
| 38 | $this->isLiveCD = file_exists('/offload/livecd'); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Updates system configuration according to new rules |
||
| 43 | */ |
||
| 44 | public function processUpdate(): void |
||
| 45 | { |
||
| 46 | if ($this->isLiveCD) { |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | $this->deleteOrphanCodecs(); |
||
| 51 | $this->addCustomCategoryToSoundFiles(); |
||
| 52 | $this->cleanAstDB(); |
||
| 53 | $this->copyMohFilesToStorage(); |
||
| 54 | $this->removeOldCacheFolders(); |
||
| 55 | $this->removeOldSessionsFiles(); |
||
| 56 | $this->updateExtensionsTable(); |
||
| 57 | $this->moveReadOnlySoundsToStorage(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Deletes all not actual codecs |
||
| 62 | */ |
||
| 63 | private function deleteOrphanCodecs() |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Checks whether it correct field "disabled" for codec record. |
||
| 105 | * |
||
| 106 | * @param Codecs $codec |
||
| 107 | */ |
||
| 108 | private function checkDisabledCodec(Codecs $codec): void |
||
| 109 | { |
||
| 110 | if ( ! in_array($codec->disabled, ['0', '1'], true)) { |
||
| 111 | $codec->disabled = '0'; |
||
| 112 | $codec->save(); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Adds new codecs from $availCodecs array if it doesn't exist |
||
| 118 | * |
||
| 119 | * @param array $availCodecs |
||
| 120 | */ |
||
| 121 | private function addNewCodecs(array $availCodecs): void |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Disables unusable codecs |
||
| 150 | */ |
||
| 151 | private function disableCodecs(): void |
||
| 152 | { |
||
| 153 | $availCodecs = [ |
||
| 154 | 'h264' => 'H.264', |
||
| 155 | 'alaw' => 'G.711 A-law', |
||
| 156 | 'ulaw' => 'G.711 µ-law', |
||
| 157 | 'opus' => 'Opus', |
||
| 158 | 'ilbc' => 'ILBC', |
||
| 159 | ]; |
||
| 160 | |||
| 161 | $codecs = Codecs::find(); |
||
| 162 | // Удалим лишние кодеки |
||
| 163 | /** @var Codecs $codec */ |
||
| 164 | foreach ($codecs as $codec) { |
||
| 165 | if (array_key_exists($codec->name, $availCodecs)) { |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | $codec->disabled = '1'; |
||
| 169 | $codec->save(); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Updates category attribute on SoundFiles table |
||
| 175 | * Add custom category to all sound files |
||
| 176 | */ |
||
| 177 | private function addCustomCategoryToSoundFiles(): void |
||
| 178 | { |
||
| 179 | $soundFiles = SoundFiles::find(); |
||
| 180 | foreach ($soundFiles as $sound_file) { |
||
| 181 | $sound_file->category = SoundFiles::CATEGORY_CUSTOM; |
||
| 182 | $sound_file->update(); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Clean UserBuddyStatus on AstDB |
||
| 188 | */ |
||
| 189 | private function cleanAstDB(): void |
||
| 190 | { |
||
| 191 | $astDbPath = $this->config->path('astDatabase.dbfile'); |
||
| 192 | if (file_exists($astDbPath)) { |
||
| 193 | $table = 'astdb'; |
||
| 194 | $sql = 'DELETE FROM ' . $table . ' WHERE key LIKE "/DND/SIP%" OR key LIKE "/CF/SIP%" OR key LIKE "/UserBuddyStatus/SIP%"'; |
||
| 195 | $db = new \SQLite3($astDbPath); |
||
| 196 | try { |
||
| 197 | $db->exec($sql); |
||
| 198 | } catch (\Error $e) { |
||
| 199 | Util::sysLogMsg(__CLASS__, 'Can clean astdb from UserBuddyStatus...' . $e->getMessage()); |
||
| 200 | sleep(2); |
||
| 201 | } |
||
| 202 | $db->close(); |
||
| 203 | unset($db); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Copies MOH sound files to storage and creates record on SoundFiles table |
||
| 209 | */ |
||
| 210 | private function copyMohFilesToStorage(): void |
||
| 211 | { |
||
| 212 | $oldMohDir = $this->config->path('asterisk.astvarlibdir') . '/sounds/moh'; |
||
| 213 | if ( ! file_exists($oldMohDir)) { |
||
| 214 | return; |
||
| 215 | } |
||
| 216 | $currentMohDir = $this->config->path('asterisk.mohdir'); |
||
| 217 | if ( ! Util::mwMkdir($currentMohDir)) { |
||
| 218 | return; |
||
| 219 | } |
||
| 220 | |||
| 221 | $files = scandir($oldMohDir); |
||
| 222 | foreach ($files as $file) { |
||
| 223 | if (in_array($file, ['.', '..'])) { |
||
| 224 | continue; |
||
| 225 | } |
||
| 226 | if (copy($oldMohDir . '/' . $file, $currentMohDir . '/' . $file)) { |
||
| 227 | $sound_file = new SoundFiles(); |
||
| 228 | $sound_file->path = $currentMohDir . '/' . $file; |
||
| 229 | $sound_file->category = SoundFiles::CATEGORY_MOH; |
||
| 230 | $sound_file->name = $file; |
||
| 231 | $sound_file->save(); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Removes old cache folders |
||
| 238 | */ |
||
| 239 | private function removeOldCacheFolders(): void |
||
| 240 | { |
||
| 241 | $mediaMountPoint = $this->config->path('core.mediaMountPoint'); |
||
| 242 | $oldCacheDirs = [ |
||
| 243 | "$mediaMountPoint/mikopbx/cache_js_dir", |
||
| 244 | "$mediaMountPoint/mikopbx/cache_img_dir", |
||
| 245 | "$mediaMountPoint/mikopbx/cache_css_dir", |
||
| 246 | ]; |
||
| 247 | foreach ($oldCacheDirs as $old_cache_dir) { |
||
| 248 | if (is_dir($old_cache_dir)) { |
||
| 249 | $rmPath = Util::which('rm'); |
||
| 250 | Util::mwExec("{$rmPath} -rf $old_cache_dir"); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | /** |
||
| 255 | * Removes old sessions files |
||
| 256 | */ |
||
| 257 | private function removeOldSessionsFiles(): void |
||
| 258 | { |
||
| 259 | $mediaMountPoint = $this->config->path('core.mediaMountPoint'); |
||
| 260 | $oldCacheDirs = [ |
||
| 261 | "$mediaMountPoint/mikopbx/log/nats/license.key", |
||
| 262 | "$mediaMountPoint/mikopbx/log/nats/*.cache", |
||
| 263 | "$mediaMountPoint/mikopbx/log/pdnsd/cache", |
||
| 264 | "$mediaMountPoint/mikopbx/log/Module*", |
||
| 265 | "$mediaMountPoint/mikopbx/php_session", |
||
| 266 | "$mediaMountPoint/mikopbx/tmp/*", |
||
| 267 | "$mediaMountPoint/mikopbx/log/ProvisioningServerPnP", |
||
| 268 | ]; |
||
| 269 | foreach ($oldCacheDirs as $old_cache_dir) { |
||
| 270 | if (is_dir($old_cache_dir)) { |
||
| 271 | $rmPath = Util::which('rm'); |
||
| 272 | Util::mwExec("{$rmPath} -rf $old_cache_dir"); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | |||
| 278 | /** |
||
| 279 | * Updates show_in_phonebook attribute on Extensions table |
||
| 280 | */ |
||
| 281 | private function updateExtensionsTable(): void |
||
| 297 | } |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Moves predefined sound files to storage disk |
||
| 303 | * Changes SoundFiles records |
||
| 304 | */ |
||
| 305 | private function moveReadOnlySoundsToStorage(): void |
||
| 306 | { |
||
| 324 | } |