| Conditions | 5 |
| Paths | 6 |
| Total Lines | 58 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 46 | * @param string $asyncChannelId Pub/sub nchan channel id to send response to frontend |
||
| 47 | * @param string $filePath Path to the downloaded zip archive |
||
| 48 | * @param string $fileId File id of the uploaded zip archive |
||
| 49 | */ |
||
| 50 | public function __construct(string $asyncChannelId, string $filePath, string $fileId) |
||
| 51 | { |
||
| 52 | $this->asyncChannelId = $asyncChannelId; |
||
| 53 | $this->filePath = $filePath; |
||
| 54 | $this->fileId = $fileId; |
||
| 55 | $this->moduleUniqueId = $fileId; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Main entry point to install a new module from a zip archive. |
||
| 60 | * This function handles the entire process of installing a new module. |
||
| 61 | * |
||
| 62 | */ |
||
| 63 | public function start(): void |
||
| 64 | { |
||
| 65 | // Calculate total mutex timeout and extra 5 seconds to prevent installing the same module in the second thread |
||
| 66 | $mutexTimeout = self::INSTALLATION_TIMEOUT+self::UPLOAD_TIMEOUT+5; |
||
| 67 | |||
| 68 | // Create a mutex to ensure synchronized access |
||
| 69 | $mutex = Util::createMutex(self::INSTALLATION_MUTEX, $this->moduleUniqueId, $mutexTimeout); |
||
| 70 | |||
| 71 | $res = new PBXApiResult(); |
||
| 72 | $res->processor = __METHOD__; |
||
| 73 | $res->success = true; |
||
| 74 | |||
| 75 | // Synchronize the installation process |
||
| 76 | try{ |
||
| 77 | $mutex->synchronized( |
||
| 78 | function () use (&$res): void { |
||
| 79 | |||
| 80 | // Wait until file upload and merge |
||
| 81 | list($fileUploadResult, $res->success) = $this->waitForFileUpload($this->fileId); |
||
| 82 | $this->pushMessageToBrowser(self::STAGE_I_UPLOAD_MODULE, $fileUploadResult); |
||
| 83 | if (!$res->success) { |
||
| 84 | $res->messages['error'] = $fileUploadResult; |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Install the downloaded module |
||
| 89 | list($installationResult, $res->success) = $this->installNewModule($this->filePath); |
||
| 90 | if (!$res->success) { |
||
| 91 | $res->messages['error'][] = $installationResult; |
||
| 92 | return; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Enable the module if it was previously enabled |
||
| 96 | list($enableResult, $res->success) = $this->enableModule($installationResult); |
||
| 97 | if (!$res->success) { |
||
| 98 | $res->messages['error'][] = $enableResult; |
||
| 99 | } |
||
| 100 | |||
| 101 | }); |
||
| 102 | } catch (\Throwable $e) { |
||
| 103 | $res->success = false; |
||
| 104 | $res->messages['error'][] = $e->getMessage(); |
||
| 144 | } |