| Conditions | 3 |
| Paths | 3 |
| Total Lines | 53 |
| Code Lines | 27 |
| 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 |
||
| 29 | public function store(Request $request) |
||
| 30 | { |
||
| 31 | $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
||
| 32 | |||
| 33 | // Verify that the upload is valid and being processed |
||
| 34 | if($receiver->isUploaded() === false) |
||
| 35 | { |
||
| 36 | Log::error('Upload File Missing - '.$request->toArray()); |
||
| 37 | throw new UploadMissingFileException(); |
||
| 38 | } |
||
| 39 | |||
| 40 | // Recieve and process the file |
||
| 41 | $save = $receiver->receive(); |
||
| 42 | |||
| 43 | // See if the uploade has finished |
||
| 44 | if($save->isFinished()) |
||
| 45 | { |
||
| 46 | $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$request->linkID; |
||
| 47 | $file = $save->getFile(); |
||
| 48 | |||
| 49 | // Clean the file and store it |
||
| 50 | $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
||
| 51 | $file->storeAs($filePath, $fileName); |
||
| 52 | |||
| 53 | // Place file in Files table of DB |
||
| 54 | $newFile = Files::create([ |
||
| 55 | 'file_name' => $fileName, |
||
| 56 | 'file_link' => $filePath.DIRECTORY_SEPARATOR |
||
| 57 | ]); |
||
| 58 | $fileID = $newFile->file_id; |
||
| 59 | |||
| 60 | // Place the file in the file link files table of DB |
||
| 61 | FileLinkFiles::create([ |
||
| 62 | 'link_id' => $request->linkID, |
||
| 63 | 'file_id' => $fileID, |
||
| 64 | 'user_id' => Auth::user()->user_id, |
||
| 65 | 'upload' => 0 |
||
| 66 | ]); |
||
| 67 | |||
| 68 | // Log stored file |
||
| 69 | Log::info('File Stored', ['file_id' => $fileID, 'file_path' => $filePath.DIRECTORY_SEPARATOR.$fileName]); |
||
| 70 | |||
| 71 | return response()->json(['success' => true]); ; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Get the current progress |
||
| 75 | $handler = $save->handler(); |
||
| 76 | |||
| 77 | Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
||
| 78 | Log::debug('File being uploaded. Percentage done - '.$handler->getPercentageDone()); |
||
| 79 | return response()->json([ |
||
| 80 | 'done' => $handler->getPercentageDone(), |
||
| 81 | 'status' => true |
||
| 82 | ]); |
||
| 162 |