| Conditions | 9 | 
| Paths | 9 | 
| Total Lines | 103 | 
| Code Lines | 62 | 
| 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 | ||
| 123 | public function store(Request $request) | ||
| 124 |     { | ||
| 125 | $request->validate([ | ||
| 126 | 'subject' => 'required', | ||
| 127 | 'systems' => 'required', | ||
| 128 | 'tipType' => 'required', | ||
| 129 | 'tip' => 'required', | ||
| 130 | ]); | ||
| 131 | |||
| 132 |         $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); | ||
| 133 | |||
| 134 | // Verify if there is a file to be processed or not (only Tech Tips can be processed without file) | ||
| 135 | if($receiver->isUploaded() === false && $request->tipType === 'Tech Tip') | ||
| 136 |         { | ||
| 137 | $tipID = $this->createTip($request); | ||
| 138 |             Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); | ||
| 139 |             return response()->json(['url' => route('tips.details', [$tipID, urlencode($request->subject)])]); | ||
| 140 | } | ||
| 141 | else if($receiver->isUploaded() === false) | ||
| 142 |         { | ||
| 143 |             Log::error('Upload File Missing - '.$request->toArray()); | ||
| 144 | throw new UploadMissingFileException(); | ||
| 145 | } | ||
| 146 | |||
| 147 | // Receive and process the file | ||
| 148 | $save = $receiver->receive(); | ||
| 149 | |||
| 150 | if($save->isFinished()) | ||
| 151 |         { | ||
| 152 | if($request->tipType === 'Tech Tip') | ||
| 153 |             { | ||
| 154 |                 if(!$request->session()->has('newTechTip')) | ||
| 155 |                 { | ||
| 156 | $tipID = $this->createTip($request); | ||
| 157 |                     $request->session()->put('newTechTip', $tipID); | ||
| 158 | } | ||
| 159 | |||
| 160 |                 $tipID = session('newTechTip'); | ||
| 161 | |||
| 162 | $file = $save->getFile(); | ||
| 163 |                 $path     = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID; | ||
| 164 | $fileName = Files::cleanFilename($path, $file->getClientOriginalName()); | ||
| 165 | $file->storeAs($path, $fileName); | ||
| 166 | |||
| 167 | $newFile = Files::create([ | ||
| 168 | 'file_name' => $fileName, | ||
| 169 | 'file_link' => $path.DIRECTORY_SEPARATOR | ||
| 170 | ]); | ||
| 171 | |||
| 172 | TechTipFiles::create([ | ||
| 173 | 'tip_id' => $tipID, | ||
| 174 | 'file_id' => $newFile->file_id | ||
| 175 | ]); | ||
| 176 | |||
| 177 |                 Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); | ||
| 178 |                 return response()->json(['url' => route('tips.details', [$tipID, urlencode($request->subject)])]); | ||
| 179 | } | ||
| 180 | else | ||
| 181 |             { | ||
| 182 | $file = $save->getFile(); | ||
| 183 | |||
| 184 | $sysArr = is_array($request->systems) ? $request->systems : json_decode($request->systems, true); | ||
| 185 | foreach($sysArr as $sys) | ||
| 186 |                 { | ||
| 187 |                     $sysData = SystemTypes::where('sys_id', $sys['value'])->first(); | ||
| 188 |                     $catName = SystemCategories::where('cat_id', $sysData->cat_id)->first()->name; | ||
| 189 |                     $path = config('filesystems.paths.systems').DIRECTORY_SEPARATOR.strtolower($catName).DIRECTORY_SEPARATOR.$sysData->folder_location; | ||
| 190 | $fileName = Files::cleanFilename($path, $file->getClientOriginalName()); | ||
| 191 | |||
| 192 | Log::debug($fileName); | ||
| 193 | Log::debug($path); | ||
| 194 | |||
| 195 | $file->storeAs($path, $fileName); | ||
| 196 | $file = Files::create([ | ||
| 197 | 'file_name' => $fileName, | ||
| 198 | 'file_link' => $path.DIRECTORY_SEPARATOR | ||
| 199 | ]); | ||
| 200 | |||
| 201 |                     $fileType = SystemFileTypes::where('description', $request->tipType)->first()->type_id; | ||
| 202 | |||
| 203 | SystemFiles::create([ | ||
| 204 | 'sys_id' => $sysData->sys_id, | ||
| 205 | 'type_id' => $fileType, | ||
| 206 | 'file_id' => $file->file_id, | ||
| 207 | 'name' => $request->subject, | ||
| 208 | 'description' => $request->tip, | ||
| 209 | 'user_id' => Auth::user()->user_id | ||
| 210 | ]); | ||
| 211 | } | ||
| 212 | |||
| 213 |                 Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); | ||
| 214 |                 return response()->json(['url' => route('tips.details', [urlencode($sysData->name), urlencode($request->subject)])]); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | // Get the current progress | ||
| 219 | $handler = $save->handler(); | ||
| 220 | |||
| 221 |         Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); | ||
| 222 |         Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone()); | ||
| 223 | return response()->json([ | ||
| 224 | 'done' => $handler->getPercentageDone(), | ||
| 225 | 'status' => true | ||
| 226 | ]); | ||
| 387 |