| Conditions | 3 |
| Paths | 3 |
| Total Lines | 55 |
| Code Lines | 29 |
| 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 |
||
| 56 | public function store(Request $request) |
||
| 57 | { |
||
| 58 | // Validate incoming data |
||
| 59 | $request->validate([ |
||
| 60 | 'name' => 'required', |
||
| 61 | 'file' => 'required', |
||
| 62 | 'fileType' => 'required', |
||
| 63 | 'system' => 'required' |
||
| 64 | ]); |
||
| 65 | |||
| 66 | $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request)); |
||
| 67 | |||
| 68 | // check if the upload is success, throw exception or return response you need |
||
| 69 | if ($receiver->isUploaded() === false) { |
||
| 70 | throw new UploadMissingFileException(); |
||
| 71 | } |
||
| 72 | |||
| 73 | // receive the file |
||
| 74 | $save = $receiver->receive(); |
||
| 75 | // check if the upload has finished (in chunk mode it will send smaller files) |
||
| 76 | if ($save->isFinished()) |
||
| 77 | { |
||
| 78 | // Get the folder location for the system |
||
| 79 | $folder = SystemTypes::where('name', $request['system'])->first()->folder_location; |
||
| 80 | |||
| 81 | // Set file location and clean filename for duplicates |
||
| 82 | $filePath = config('filesystems.paths.systems') |
||
| 83 | .DIRECTORY_SEPARATOR.strtolower($request['category']) |
||
| 84 | .DIRECTORY_SEPARATOR.$folder; |
||
| 85 | |||
| 86 | $fileID = $this->saveFile($save->getFile(), $filePath); |
||
| 87 | |||
| 88 | $typeID = SystemFileTypes::where('description', $request['fileType'])->first()->type_id; |
||
| 89 | $sysID = SystemTypes::where('name', $request['system'])->first()->sys_id; |
||
| 90 | |||
| 91 | // Attach file to system type |
||
| 92 | SystemFiles::create( |
||
| 93 | [ |
||
| 94 | 'sys_id' => $sysID, |
||
| 95 | 'type_id' => $typeID, |
||
| 96 | 'file_id' => $fileID, |
||
| 97 | 'name' => $request['name'], |
||
| 98 | 'description' => $request['description'], |
||
| 99 | 'user_id' => \Auth::user()->user_id |
||
| 100 | ]); |
||
| 101 | |||
| 102 | Log::info('File ID-'.$fileID.' Added For System ID-'.$sysID.' By User ID-'.Auth::user()->user_id); |
||
| 103 | |||
| 104 | } |
||
| 105 | |||
| 106 | $handler = $save->handler(); |
||
| 107 | |||
| 108 | return response()->json([ |
||
| 109 | "done" => $handler->getPercentageDone(), |
||
| 110 | 'status' => true |
||
| 111 | ]); |
||
| 206 |