| Total Complexity | 67 |
| Total Lines | 503 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| 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 |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sends test email to admin address |
||
| 121 | * |
||
| 122 | * @param $data |
||
| 123 | * |
||
| 124 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult $res |
||
| 125 | */ |
||
| 126 | private static function sendMail($data): PBXApiResult |
||
| 127 | { |
||
| 128 | $res = new PBXApiResult(); |
||
| 129 | $res->processor = __METHOD__; |
||
| 130 | if (isset($data['email']) && isset($data['subject']) && isset($data['body'])) { |
||
| 131 | if (isset($data['encode']) && $data['encode'] === 'base64') { |
||
| 132 | $data['subject'] = base64_decode($data['subject']); |
||
| 133 | $data['body'] = base64_decode($data['body']); |
||
| 134 | } |
||
| 135 | $result = Notifications::sendMail($data['email'], $data['subject'], $data['body']); |
||
| 136 | if ($result === true) { |
||
| 137 | $res->success = true; |
||
| 138 | } else { |
||
| 139 | $res->success = false; |
||
| 140 | $res->messages[] = $result; |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | $res->success = false; |
||
| 144 | $res->messages[] = 'Not all query parameters were set'; |
||
| 145 | } |
||
| 146 | |||
| 147 | return $res; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Upgrade MikoPBX from uploaded IMG file |
||
| 152 | * |
||
| 153 | * @param string $tempFilename path to uploaded image |
||
| 154 | * |
||
| 155 | * @return PBXApiResult |
||
| 156 | */ |
||
| 157 | public static function upgradeFromImg(string $tempFilename): PBXApiResult |
||
| 158 | { |
||
| 159 | $res = new PBXApiResult(); |
||
| 160 | $res->processor = __METHOD__; |
||
| 161 | $res->success = true; |
||
| 162 | $res->data['message'] = 'In progress...'; |
||
| 163 | |||
| 164 | |||
| 165 | if ( ! file_exists($tempFilename)) { |
||
| 166 | $res->success = false; |
||
| 167 | $res->messages[] = "Update file '{$tempFilename}' not found."; |
||
| 168 | |||
| 169 | return $res; |
||
| 170 | } |
||
| 171 | |||
| 172 | if ( ! file_exists('/var/etc/cfdevice')) { |
||
| 173 | $res->success = false; |
||
| 174 | $res->messages[] = "The system is not installed"; |
||
| 175 | |||
| 176 | return $res; |
||
| 177 | } |
||
| 178 | $dev = trim(file_get_contents('/var/etc/cfdevice')); |
||
| 179 | |||
| 180 | $link = '/tmp/firmware_update.img'; |
||
| 181 | Util::createUpdateSymlink($tempFilename, $link); |
||
| 182 | $mikopbx_firmwarePath = Util::which('mikopbx_firmware'); |
||
| 183 | Util::mwExecBg("{$mikopbx_firmwarePath} recover_upgrade {$link} /dev/{$dev}"); |
||
| 184 | |||
| 185 | return $res; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Install new additional extension module |
||
| 190 | * |
||
| 191 | * @param $filePath |
||
| 192 | * |
||
| 193 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult |
||
| 194 | * |
||
| 195 | */ |
||
| 196 | public static function installModuleFromFile($filePath): PBXApiResult |
||
| 197 | { |
||
| 198 | $res = new PBXApiResult(); |
||
| 199 | $res->processor = __METHOD__; |
||
| 200 | $moduleMetadata = FilesManagementProcessor::getMetadataFromModuleFile($filePath); |
||
| 201 | if ( ! $moduleMetadata->success) { |
||
| 202 | return $moduleMetadata; |
||
| 203 | } else { |
||
| 204 | $moduleUniqueID = $moduleMetadata->data['uniqid']; |
||
| 205 | $res = self::installModule($filePath, $moduleUniqueID); |
||
| 206 | } |
||
| 207 | |||
| 208 | return $res; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Install module from file |
||
| 213 | * |
||
| 214 | * @param string $filePath |
||
| 215 | * |
||
| 216 | * @param string $moduleUniqueID |
||
| 217 | * |
||
| 218 | * @return PBXApiResult |
||
| 219 | */ |
||
| 220 | public static function installModule(string $filePath, string $moduleUniqueID): PBXApiResult |
||
| 221 | { |
||
| 222 | $res = new PBXApiResult(); |
||
| 223 | $res->processor = __METHOD__; |
||
| 224 | $res->success = true; |
||
| 225 | $currentModuleDir = PbxExtensionUtils::getModuleDir($moduleUniqueID); |
||
| 226 | $needBackup = is_dir($currentModuleDir); |
||
| 227 | |||
| 228 | if ($needBackup) { |
||
| 229 | self::uninstallModule($moduleUniqueID, true); |
||
| 230 | } |
||
| 231 | |||
| 232 | $semZaPath = Util::which('7za'); |
||
| 233 | Util::mwExec("{$semZaPath} e -spf -aoa -o{$currentModuleDir} {$filePath}"); |
||
| 234 | Util::addRegularWWWRights($currentModuleDir); |
||
| 235 | |||
| 236 | $pbxExtensionSetupClass = "\\Modules\\{$moduleUniqueID}\\Setup\\PbxExtensionSetup"; |
||
| 237 | if (class_exists($pbxExtensionSetupClass) |
||
| 238 | && method_exists($pbxExtensionSetupClass, 'installModule')) { |
||
| 239 | $setup = new $pbxExtensionSetupClass($moduleUniqueID); |
||
| 240 | if ( ! $setup->installModule()) { |
||
| 241 | $res->success = false; |
||
| 242 | $res->messages[] = $setup->getMessages(); |
||
| 243 | } |
||
| 244 | } else { |
||
| 245 | $res->success = false; |
||
| 246 | $res->messages[] = "Install error: the class {$pbxExtensionSetupClass} not exists"; |
||
| 247 | } |
||
| 248 | |||
| 249 | if ($res->success) { |
||
| 250 | $res->data['needRestartWorkers'] = true; |
||
| 251 | } |
||
| 252 | |||
| 253 | return $res; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Uninstall module |
||
| 258 | * |
||
| 259 | * @param string $moduleUniqueID |
||
| 260 | * |
||
| 261 | * @param bool $keepSettings |
||
| 262 | * |
||
| 263 | * @return PBXApiResult |
||
| 264 | */ |
||
| 265 | public static function uninstallModule(string $moduleUniqueID, bool $keepSettings): PBXApiResult |
||
| 266 | { |
||
| 267 | $res = new PBXApiResult(); |
||
| 268 | $res->processor = __METHOD__; |
||
| 269 | $currentModuleDir = PbxExtensionUtils::getModuleDir($moduleUniqueID); |
||
| 270 | // Kill all module processes |
||
| 271 | if (is_dir("{$currentModuleDir}/bin")) { |
||
| 272 | $busyboxPath = Util::which('busybox'); |
||
| 273 | $killPath = Util::which('kill'); |
||
| 274 | $lsofPath = Util::which('lsof'); |
||
| 275 | $grepPath = Util::which('grep'); |
||
| 276 | $awkPath = Util::which('awk'); |
||
| 277 | $uniqPath = Util::which('uniq'); |
||
| 278 | Util::mwExec( |
||
| 279 | "{$busyboxPath} {$killPath} -9 $({$lsofPath} {$currentModuleDir}/bin/* | {$busyboxPath} {$grepPath} -v COMMAND | {$busyboxPath} {$awkPath} '{ print $2}' | {$busyboxPath} {$uniqPath})" |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | // Uninstall module with keep settings and backup db |
||
| 283 | $moduleClass = "\\Modules\\{$moduleUniqueID}\\Setup\\PbxExtensionSetup"; |
||
| 284 | |||
| 285 | try { |
||
| 286 | if (class_exists($moduleClass) |
||
| 287 | && method_exists($moduleClass, 'uninstallModule')) { |
||
| 288 | $setup = new $moduleClass($moduleUniqueID); |
||
| 289 | } else { |
||
| 290 | // Заглушка которая позволяет удалить модуль из базы данных, которого нет на диске |
||
| 291 | $moduleClass = PbxExtensionSetupFailure::class; |
||
| 292 | $setup = new $moduleClass($moduleUniqueID); |
||
| 293 | } |
||
| 294 | $setup->uninstallModule($keepSettings); |
||
| 295 | } finally { |
||
| 296 | if (is_dir($currentModuleDir)) { |
||
| 297 | // Broken or very old module. Force uninstall. |
||
| 298 | $rmPath = Util::which('rm'); |
||
| 299 | Util::mwExec("{$rmPath} -rf {$currentModuleDir}"); |
||
| 300 | |||
| 301 | $moduleClass = PbxExtensionSetupFailure::class; |
||
| 302 | $setup = new $moduleClass($moduleUniqueID); |
||
| 303 | $setup->unregisterModule(); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | $res->success = true; |
||
| 307 | $res->data['needRestartWorkers'] = true; |
||
| 308 | |||
| 309 | return $res; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Enables extension module |
||
| 314 | * |
||
| 315 | * @param string $moduleUniqueID |
||
| 316 | * |
||
| 317 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult $res |
||
| 318 | */ |
||
| 319 | private static function enableModule(string $moduleUniqueID): PBXApiResult |
||
| 320 | { |
||
| 321 | $res = new PBXApiResult(); |
||
| 322 | $res->processor = __METHOD__; |
||
| 323 | $moduleStateProcessor = new PbxExtensionState($moduleUniqueID); |
||
| 324 | if ($moduleStateProcessor->enableModule() === false) { |
||
| 325 | $res->success = false; |
||
| 326 | $res->messages = $moduleStateProcessor->getMessages(); |
||
| 327 | } else { |
||
| 328 | PBXConfModulesProvider::recreateModulesProvider(); |
||
| 329 | $res->data = $moduleStateProcessor->getMessages(); |
||
| 330 | $res->data['needRestartWorkers'] = true; |
||
| 331 | $res->success = true; |
||
| 332 | } |
||
| 333 | |||
| 334 | return $res; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Вшыфидуы extension module |
||
| 339 | * |
||
| 340 | * @param string $moduleUniqueID |
||
| 341 | * |
||
| 342 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult $res |
||
| 343 | */ |
||
| 344 | private static function disableModule(string $moduleUniqueID): PBXApiResult |
||
| 345 | { |
||
| 346 | $res = new PBXApiResult(); |
||
| 347 | $res->processor = __METHOD__; |
||
| 348 | $moduleStateProcessor = new PbxExtensionState($moduleUniqueID); |
||
| 349 | if ($moduleStateProcessor->disableModule() === false) { |
||
| 350 | $res->success = false; |
||
| 351 | $res->messages = $moduleStateProcessor->getMessages(); |
||
| 352 | } else { |
||
| 353 | PBXConfModulesProvider::recreateModulesProvider(); |
||
| 354 | $res->data = $moduleStateProcessor->getMessages(); |
||
| 355 | $res->data['needRestartWorkers'] = true; //TODO:: Проверить надо ли это |
||
| 356 | $res->success = true; |
||
| 357 | } |
||
| 358 | |||
| 359 | return $res; |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Deletes all settings and uploaded files |
||
| 364 | */ |
||
| 365 | private static function restoreDefaultSettings(): PBXApiResult |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Converts file to wav file with 8000 bitrate |
||
| 480 | * |
||
| 481 | * @param $filename |
||
| 482 | * |
||
| 483 | * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult |
||
| 484 | */ |
||
| 485 | public static function convertAudioFile($filename): PBXApiResult |
||
| 540 | } |