| Total Complexity | 65 |
| Total Lines | 468 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SystemManagementProcessor 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 SystemManagementProcessor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class SystemManagementProcessor extends Injectable |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Processes System requests |
||
| 39 | * |
||
| 40 | * @param array $request |
||
| 41 | * |
||
| 42 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult |
||
| 43 | * |
||
| 44 | * @throws \Exception |
||
| 45 | */ |
||
| 46 | public static function callBack(array $request): PBXApiResult |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Upgrade MikoPBX from uploaded IMG file |
||
| 158 | * |
||
| 159 | * @param string $tempFilename path to uploaded image |
||
| 160 | * |
||
| 161 | * @return PBXApiResult |
||
| 162 | */ |
||
| 163 | public static function upgradeFromImg(string $tempFilename): PBXApiResult |
||
| 164 | { |
||
| 165 | $res = new PBXApiResult(); |
||
| 166 | $res->processor = __METHOD__; |
||
| 167 | $res->success = true; |
||
| 168 | $res->data['message'] = 'In progress...'; |
||
| 169 | |||
| 170 | |||
| 171 | if ( ! file_exists($tempFilename)) { |
||
| 172 | $res->success = false; |
||
| 173 | $res->messages[] = "Update file '{$tempFilename}' not found."; |
||
| 174 | |||
| 175 | return $res; |
||
| 176 | } |
||
| 177 | |||
| 178 | if ( ! file_exists('/var/etc/cfdevice')) { |
||
| 179 | $res->success = false; |
||
| 180 | $res->messages[] = "The system is not installed"; |
||
| 181 | |||
| 182 | return $res; |
||
| 183 | } |
||
| 184 | $dev = trim(file_get_contents('/var/etc/cfdevice')); |
||
| 185 | |||
| 186 | $link = '/tmp/firmware_update.img'; |
||
| 187 | Util::createUpdateSymlink($tempFilename, $link); |
||
| 188 | $mikopbx_firmwarePath = Util::which('mikopbx_firmware'); |
||
| 189 | Util::mwExecBg("{$mikopbx_firmwarePath} recover_upgrade {$link} /dev/{$dev}"); |
||
| 190 | |||
| 191 | return $res; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Deletes all settings and uploaded files |
||
| 196 | */ |
||
| 197 | private static function restoreDefaultSettings(): PBXApiResult |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Install new additional extension module |
||
| 309 | * |
||
| 310 | * @param $filePath |
||
| 311 | * |
||
| 312 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult |
||
| 313 | * |
||
| 314 | */ |
||
| 315 | public static function installModuleFromFile($filePath): PBXApiResult |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Install module from file |
||
| 332 | * |
||
| 333 | * @param string $filePath |
||
| 334 | * |
||
| 335 | * @param string $moduleUniqueID |
||
| 336 | * |
||
| 337 | * @return PBXApiResult |
||
| 338 | */ |
||
| 339 | public static function installModule(string $filePath, string $moduleUniqueID): PBXApiResult |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Uninstall module |
||
| 377 | * |
||
| 378 | * @param string $moduleUniqueID |
||
| 379 | * |
||
| 380 | * @param bool $keepSettings |
||
| 381 | * |
||
| 382 | * @return PBXApiResult |
||
| 383 | */ |
||
| 384 | public static function uninstallModule(string $moduleUniqueID, bool $keepSettings): PBXApiResult |
||
| 385 | { |
||
| 386 | $res = new PBXApiResult(); |
||
| 387 | $res->processor = __METHOD__; |
||
| 388 | $currentModuleDir = PbxExtensionUtils::getModuleDir($moduleUniqueID); |
||
| 389 | // Kill all module processes |
||
| 390 | if (is_dir("{$currentModuleDir}/bin")) { |
||
| 391 | $busyboxPath = Util::which('busybox'); |
||
| 392 | $killPath = Util::which('kill'); |
||
| 393 | $lsofPath = Util::which('lsof'); |
||
| 394 | $grepPath = Util::which('grep'); |
||
| 395 | $awkPath = Util::which('awk'); |
||
| 396 | $uniqPath = Util::which('uniq'); |
||
| 397 | Util::mwExec( |
||
| 398 | "{$busyboxPath} {$killPath} -9 $({$lsofPath} {$currentModuleDir}/bin/* | {$busyboxPath} {$grepPath} -v COMMAND | {$busyboxPath} {$awkPath} '{ print $2}' | {$busyboxPath} {$uniqPath})" |
||
| 399 | ); |
||
| 400 | } |
||
| 401 | // Uninstall module with keep settings and backup db |
||
| 402 | $moduleClass = "\\Modules\\{$moduleUniqueID}\\Setup\\PbxExtensionSetup"; |
||
| 403 | |||
| 404 | try { |
||
| 405 | if (class_exists($moduleClass) |
||
| 406 | && method_exists($moduleClass, 'uninstallModule')) { |
||
| 407 | $setup = new $moduleClass($moduleUniqueID); |
||
| 408 | } else { |
||
| 409 | // Заглушка которая позволяет удалить модуль из базы данных, которого нет на диске |
||
| 410 | $moduleClass = PbxExtensionSetupFailure::class; |
||
| 411 | $setup = new $moduleClass($moduleUniqueID); |
||
| 412 | } |
||
| 413 | $setup->uninstallModule($keepSettings); |
||
| 414 | } finally { |
||
| 415 | if (is_dir($currentModuleDir)) { |
||
| 416 | // Broken or very old module. Force uninstall. |
||
| 417 | $rmPath = Util::which('rm'); |
||
| 418 | Util::mwExec("{$rmPath} -rf {$currentModuleDir}"); |
||
| 419 | |||
| 420 | $moduleClass = PbxExtensionSetupFailure::class; |
||
| 421 | $setup = new $moduleClass($moduleUniqueID); |
||
| 422 | $setup->unregisterModule(); |
||
| 423 | } |
||
| 424 | } |
||
| 425 | $res->success = true; |
||
| 426 | $res->data['needRestartWorkers'] = true; |
||
| 427 | |||
| 428 | return $res; |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Converts file to wav file with 8000 bitrate |
||
| 433 | * |
||
| 434 | * @param $filename |
||
| 435 | * |
||
| 436 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult |
||
| 437 | */ |
||
| 438 | public static function convertAudioFile($filename): PBXApiResult |
||
| 439 | { |
||
| 440 | $res = new PBXApiResult(); |
||
| 441 | $res->processor = __METHOD__; |
||
| 442 | if ( ! file_exists($filename)) { |
||
| 443 | $res->success = false; |
||
| 444 | $res->messages[] = "File '{$filename}' not found."; |
||
| 445 | |||
| 446 | return $res; |
||
| 447 | } |
||
| 448 | $out = []; |
||
| 449 | $tmp_filename = '/tmp/' . time() . "_" . basename($filename); |
||
| 450 | if (false === copy($filename, $tmp_filename)) { |
||
| 451 | $res->success = false; |
||
| 452 | $res->messages[] = "Unable to create temporary file '{$tmp_filename}'."; |
||
| 453 | |||
| 454 | return $res; |
||
| 455 | } |
||
| 456 | |||
| 457 | // Принудительно устанавливаем расширение файла в wav. |
||
| 458 | $n_filename = Util::trimExtensionForFile($filename) . ".wav"; |
||
| 459 | $n_filename_mp3 = Util::trimExtensionForFile($filename) . ".mp3"; |
||
| 460 | // Конвертируем файл. |
||
| 461 | $tmp_filename = escapeshellcmd($tmp_filename); |
||
| 462 | $n_filename = escapeshellcmd($n_filename); |
||
| 463 | $soxPath = Util::which('sox'); |
||
| 464 | Util::mwExec("{$soxPath} -v 0.99 -G '{$tmp_filename}' -c 1 -r 8000 -b 16 '{$n_filename}'", $out); |
||
| 465 | $result_str = implode('', $out); |
||
| 466 | |||
| 467 | $lamePath = Util::which('lame'); |
||
| 468 | Util::mwExec("{$lamePath} -b 32 --silent '{$n_filename}' '{$n_filename_mp3}'", $out); |
||
| 469 | $result_mp3 = implode('', $out); |
||
| 470 | |||
| 471 | // Чистим мусор. |
||
| 472 | unlink($tmp_filename); |
||
| 473 | if ($result_str !== '' && $result_mp3 !== '') { |
||
| 474 | // Ошибка выполнения конвертации. |
||
| 475 | $res->success = false; |
||
| 476 | $res->messages[] = $result_str; |
||
| 477 | |||
| 478 | return $res; |
||
| 479 | } |
||
| 480 | |||
| 481 | if (file_exists($filename) |
||
| 482 | && $filename !== $n_filename |
||
| 483 | && $filename !== $n_filename_mp3) { |
||
| 484 | unlink($filename); |
||
| 485 | } |
||
| 486 | |||
| 487 | $res->success = true; |
||
| 488 | $res->data[] = $n_filename_mp3; |
||
| 489 | |||
| 490 | return $res; |
||
| 491 | } |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Changes core language |
||
| 495 | */ |
||
| 496 | private static function updateCoreLanguageAction(): void |
||
| 503 | } |
||
| 504 | } |