| Conditions | 9 |
| Paths | 48 |
| Total Lines | 57 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 44 | public static function main(string $moduleUniqueID): PBXApiResult |
||
| 45 | { |
||
| 46 | clearstatcache(); |
||
| 47 | $res = new PBXApiResult(); |
||
| 48 | $res->processor = __METHOD__; |
||
| 49 | $di = Di::getDefault(); |
||
| 50 | if ($di !== null) { |
||
| 51 | $tempDir = $di->getShared(ConfigProvider::SERVICE_NAME)->path('www.uploadDir'); |
||
| 52 | } else { |
||
| 53 | $tempDir = '/tmp'; |
||
| 54 | } |
||
| 55 | $moduleDirTmp = $tempDir . '/' . $moduleUniqueID; |
||
| 56 | $progress_file = $moduleDirTmp . '/progress'; |
||
| 57 | $error = ''; |
||
| 58 | if (file_exists($moduleDirTmp . '/error')) { |
||
| 59 | $error = trim(file_get_contents($moduleDirTmp . '/error')); |
||
| 60 | } |
||
| 61 | |||
| 62 | // Wait until a download process started |
||
| 63 | $d_pid = Processes::getPidOfProcess("{$moduleDirTmp}/download_settings.json"); |
||
| 64 | if (empty($d_pid)) { |
||
| 65 | usleep(500000); |
||
| 66 | } |
||
| 67 | |||
| 68 | if (!file_exists($progress_file)) { |
||
| 69 | $res->data[FilesConstants::D_STATUS_PROGRESS] = '0'; |
||
| 70 | $res->data[FilesConstants::D_STATUS] = FilesConstants::STATUS_NOT_FOUND; |
||
| 71 | $res->success = false; |
||
| 72 | } elseif ('' !== $error) { |
||
| 73 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_ERROR; |
||
| 74 | $res->data[FilesConstants::D_STATUS_PROGRESS] = file_get_contents($progress_file); |
||
| 75 | $res->data[FilesConstants::D_ERROR] = $error; |
||
| 76 | $res->messages[] = file_get_contents($moduleDirTmp . '/error'); |
||
| 77 | $res->success = false; |
||
| 78 | } elseif ('100' === file_get_contents($progress_file)) { |
||
| 79 | $res->data[FilesConstants::D_STATUS_PROGRESS] = '100'; |
||
| 80 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_COMPLETE; |
||
| 81 | $res->data[FilesConstants::FILE_PATH] = "$moduleDirTmp/modulefile.zip"; |
||
| 82 | $res->success = true; |
||
| 83 | } else { |
||
| 84 | $res->data[FilesConstants::D_STATUS_PROGRESS] = file_get_contents($progress_file); |
||
| 85 | $d_pid = Processes::getPidOfProcess($moduleDirTmp . '/download_settings.json'); |
||
| 86 | if (empty($d_pid)) { |
||
| 87 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_ERROR; |
||
| 88 | if (file_exists($moduleDirTmp . '/error')) { |
||
| 89 | $res->messages[] = file_get_contents($moduleDirTmp . '/error'); |
||
| 90 | } else { |
||
| 91 | $res->messages[] = "Download process interrupted at {$res->data['d_status_progress']}%"; |
||
| 92 | } |
||
| 93 | $res->success = false; |
||
| 94 | } else { |
||
| 95 | $res->data[FilesConstants::D_STATUS] = FilesConstants::DOWNLOAD_IN_PROGRESS; |
||
| 96 | $res->success = true; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return $res; |
||
| 101 | } |
||
| 103 | } |