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