| Conditions | 9 |
| Paths | 10 |
| Total Lines | 87 |
| Code Lines | 63 |
| 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 |
||
| 27 | public static function uploadResumable($parameters): array |
||
| 28 | { |
||
| 29 | $data['result'] = 'ERROR'; |
||
|
|
|||
| 30 | $di = Di::getDefault(); |
||
| 31 | if ($di === null) { |
||
| 32 | return ['result' => 'ERROR', 'data' => 'Dependency injector not initialized']; |
||
| 33 | } |
||
| 34 | $upload_id = $parameters['upload_id']; |
||
| 35 | $resumableFilename = $parameters['resumableFilename']; |
||
| 36 | $resumableIdentifier = $parameters['resumableIdentifier']; |
||
| 37 | $resumableChunkNumber = $parameters['resumableChunkNumber']; |
||
| 38 | $resumableTotalSize = $parameters['resumableTotalSize']; |
||
| 39 | $uploadPath = $di->getShared('config')->path('core.uploadPath'); |
||
| 40 | |||
| 41 | $factory = new StreamFactory(); |
||
| 42 | |||
| 43 | foreach ($parameters['files'] as $file_data) { |
||
| 44 | $stream = $factory->createStreamFromFile($file_data['file_path'], 'r'); |
||
| 45 | $file = new UploadedFile( |
||
| 46 | $stream, |
||
| 47 | $file_data['file_size'], |
||
| 48 | $file_data['file_error'], |
||
| 49 | $file_data['file_name'], |
||
| 50 | $file_data['file_type'] |
||
| 51 | ); |
||
| 52 | |||
| 53 | if (isset($resumableIdentifier) && trim($resumableIdentifier) !== '') { |
||
| 54 | $temp_dir = $uploadPath . '/' . Util::trimExtensionForFile(basename($resumableFilename)); |
||
| 55 | $temp_dst_file = $uploadPath . '/' . $upload_id . '/' . basename($resumableFilename); |
||
| 56 | $chunks_dest_file = $temp_dir . '/' . $resumableFilename . '.part' . $resumableChunkNumber; |
||
| 57 | } else { |
||
| 58 | $temp_dir = $uploadPath . '/' . $upload_id; |
||
| 59 | $temp_dst_file = $temp_dir . '/' . basename($file->getClientFilename()); |
||
| 60 | $chunks_dest_file = $temp_dst_file; |
||
| 61 | } |
||
| 62 | if ( ! Util::mwMkdir($temp_dir) || ! Util::mwMkdir(dirname($temp_dst_file))) { |
||
| 63 | Util::sysLogMsg('UploadFile', "Error create dir '$temp_dir'"); |
||
| 64 | return ['result' => 'ERROR', 'data' => "Error create dir 'temp_dir'"]; |
||
| 65 | } |
||
| 66 | $file->moveTo($chunks_dest_file); |
||
| 67 | // if (file_exists($file->)) |
||
| 68 | if ($resumableFilename) { |
||
| 69 | $data['result'] = 'Success'; |
||
| 70 | // Передача файлов частями. |
||
| 71 | $result = Util::createFileFromChunks($temp_dir, $resumableTotalSize); |
||
| 72 | if ($result === true) { |
||
| 73 | $merge_settings = [ |
||
| 74 | 'data' => [ |
||
| 75 | 'result_file' => $temp_dst_file, |
||
| 76 | 'temp_dir' => $temp_dir, |
||
| 77 | 'resumableFilename' => $resumableFilename, |
||
| 78 | 'resumableTotalChunks' => $resumableChunkNumber, |
||
| 79 | ], |
||
| 80 | 'action' => 'merge', |
||
| 81 | ]; |
||
| 82 | $settings_file = "{$temp_dir}/merge_settings"; |
||
| 83 | file_put_contents( |
||
| 84 | $settings_file, |
||
| 85 | json_encode($merge_settings, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) |
||
| 86 | ); |
||
| 87 | |||
| 88 | // Отправляем задачу на склеивание файла. |
||
| 89 | $phpPath = Util::which('php'); |
||
| 90 | $workerFilesMergerPath = Util::getFilePathByClassName(WorkerMergeUploadedFile::class); |
||
| 91 | Util::mwExecBg("{$phpPath} -f {$workerFilesMergerPath} '{$settings_file}'"); |
||
| 92 | |||
| 93 | $data['upload_id'] = $upload_id; |
||
| 94 | $data['filename'] = $temp_dst_file; |
||
| 95 | $data['d_status'] = 'INPROGRESS'; |
||
| 96 | } |
||
| 97 | } else { |
||
| 98 | $data['result'] = 'Success'; |
||
| 99 | // Передача файла целиком. |
||
| 100 | $data['upload_id'] = $upload_id; |
||
| 101 | $data['filename'] = $temp_dst_file; |
||
| 102 | $data['d_status'] = 'UPLOAD_COMPLETE'; |
||
| 103 | file_put_contents($temp_dir . '/progress', '100'); |
||
| 104 | |||
| 105 | Util::mwExecBg( |
||
| 106 | '/etc/rc/shell_functions.sh killprocesses ' . $temp_dir . ' -TERM 0;rm -rf ' . $temp_dir, |
||
| 107 | '/dev/null', |
||
| 108 | 120 |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return $data; |
||
| 114 | } |
||
| 158 | } |