| Total Complexity | 41 |
| Total Lines | 481 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like TechTipsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TechTipsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class TechTipsController extends Controller |
||
| 31 | { |
||
| 32 | // Only authorized users have access |
||
| 33 | public function __construct() |
||
| 34 | { |
||
| 35 | $this->middleware('auth'); |
||
| 36 | } |
||
| 37 | |||
| 38 | // Landing page brings up the tech tip search form |
||
| 39 | public function index() |
||
| 40 | { |
||
| 41 | $systems = SystemCategories::with('SystemTypes') |
||
| 42 | ->orderBy('cat_id', 'asc') |
||
| 43 | ->get(); |
||
| 44 | |||
| 45 | $sysArr = []; |
||
| 46 | foreach($systems as $sys) |
||
| 47 | { |
||
| 48 | foreach($sys->SystemTypes as $s) |
||
| 49 | { |
||
| 50 | $sysArr[$sys->name][$s->sys_id] = $s->name; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | return view('tip.index', [ |
||
| 55 | 'systems' => $sysArr |
||
| 56 | ]); |
||
| 57 | } |
||
| 58 | |||
| 59 | // Search for a tech tip |
||
| 60 | public function search(Request $request) |
||
| 80 | ]); |
||
| 81 | } |
||
| 82 | |||
| 83 | // Create a new Tech Tip |
||
| 84 | public function create() |
||
| 85 | { |
||
| 86 | // Get system types for tip tagging |
||
| 87 | $systems = SystemCategories::with('SystemTypes') |
||
| 88 | ->orderBy('cat_id', 'asc') |
||
| 89 | ->get(); |
||
| 90 | |||
| 91 | $sysArr = []; |
||
| 92 | foreach($systems as $sys) |
||
| 93 | { |
||
| 94 | foreach($sys->SystemTypes as $s) |
||
| 95 | { |
||
| 96 | $sysArr[$sys->name][$s->sys_id] = $s->name; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return view('tip.form.newTip', [ |
||
| 101 | 'systems' => $sysArr |
||
| 102 | ]); |
||
| 103 | } |
||
| 104 | |||
| 105 | // Submit the new tech tip |
||
| 106 | public function store(Request $request) |
||
| 107 | { |
||
| 108 | $request->validate([ |
||
| 109 | 'subject' => 'required', |
||
| 110 | 'details' => 'required', |
||
| 111 | 'sysTags' => 'required' |
||
| 112 | ]); |
||
| 113 | |||
| 114 | |||
| 115 | if(!empty($request->file)) |
||
| 116 | { |
||
| 117 | // create the file receiver |
||
| 118 | $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request)); |
||
| 119 | |||
| 120 | // check if the upload is success, throw exception or return response you need |
||
| 121 | if ($receiver->isUploaded() === false) { |
||
| 122 | throw new UploadMissingFileException(); |
||
| 123 | } |
||
| 124 | |||
| 125 | // receive the file |
||
| 126 | $save = $receiver->receive(); |
||
| 127 | |||
| 128 | if ($save->isFinished()) |
||
| 129 | { |
||
| 130 | $tipID = $this->createNewTechTip($request); |
||
| 131 | $filePath = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID; |
||
| 132 | $fileID = $this->saveNewTipFile($save->getFile(), $filePath); |
||
| 133 | |||
| 134 | // Place the file in the tech tip files table of DB |
||
| 135 | TechTipFiles::create([ |
||
| 136 | 'tip_id' => $tipID, |
||
| 137 | 'file_id' => $fileID |
||
| 138 | ]); |
||
| 139 | |||
| 140 | // Email the techs of the new tip |
||
| 141 | $tipData = TechTips::find($tipID); |
||
| 142 | $userList = UserSettings::where('em_tech_tip', 1)->join('users', 'user_settings.user_id', '=', 'users.user_id')->where('active', 1)->get(); |
||
| 143 | try |
||
| 144 | { |
||
| 145 | Mail::to($userList)->send(new newTechtip($tipData)); |
||
| 146 | } |
||
| 147 | catch(Exception $e) |
||
| 148 | { |
||
| 149 | report($e); |
||
| 150 | } |
||
| 151 | |||
| 152 | Log::info('Tech Tip ID-'.$tipID.' Created by Customer ID-'.Auth::user()->user_id); |
||
| 153 | |||
| 154 | return $tipID; |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | $handler = $save->handler(); |
||
| 159 | |||
| 160 | return response()->json([ |
||
| 161 | "done" => $handler->getPercentageDone(), |
||
| 162 | 'status' => true |
||
| 163 | ]); |
||
| 164 | } |
||
| 165 | else |
||
| 166 | { |
||
| 167 | $tipID = $this->createNewTechTip($request); |
||
| 168 | |||
| 169 | // Email the techs of the new tip |
||
| 170 | $tipData = TechTips::find($tipID); |
||
| 171 | $userList = UserSettings::where('em_tech_tip', 1)->join('users', 'user_settings.user_id', '=', 'users.user_id')->where('active', 1)->get(); |
||
| 172 | try |
||
| 173 | { |
||
| 174 | Mail::to($userList)->send(new newTechtip($tipData)); |
||
| 175 | } |
||
| 176 | catch(Exception $e) |
||
| 177 | { |
||
| 178 | report($e); |
||
| 179 | } |
||
| 180 | |||
| 181 | Log::info('Tech Tip ID-'.$tipID.' Created by Customer ID-'.Auth::user()->user_id); |
||
| 182 | |||
| 183 | return $tipID; |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | // // If there are any files, process them |
||
| 188 | // if(!empty($request->file)) |
||
| 189 | // { |
||
| 190 | // $filePath = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$tipID; |
||
| 191 | // foreach($request->file as $file) |
||
| 192 | // { |
||
| 193 | // // Clean the file and store it |
||
| 194 | // $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
||
| 195 | // $file->storeAs($filePath, $fileName); |
||
| 196 | // |
||
| 197 | // // Place file in Files table of DB |
||
| 198 | // $newFile = Files::create([ |
||
| 199 | // 'file_name' => $fileName, |
||
| 200 | // 'file_link' => $filePath.DIRECTORY_SEPARATOR |
||
| 201 | // ]); |
||
| 202 | // $fileID = $newFile->file_id; |
||
| 203 | // |
||
| 204 | // // Place the file in the tech tip files table of DB |
||
| 205 | // TechTipFiles::create([ |
||
| 206 | // 'tip_id' => $tipID, |
||
| 207 | // 'file_id' => $fileID |
||
| 208 | // ]); |
||
| 209 | // } |
||
| 210 | // } |
||
| 211 | // |
||
| 212 | // // Email the techs of the new tip |
||
| 213 | // $tipData = TechTips::find($tipID); |
||
| 214 | // $userList = UserSettings::where('em_tech_tip', 1)->join('users', 'user_settings.user_id', '=', 'users.user_id')->where('active', 1)->get(); |
||
| 215 | // try |
||
| 216 | // { |
||
| 217 | // Mail::to($userList)->send(new newTechtip($tipData)); |
||
| 218 | // } |
||
| 219 | // catch(Exception $e) |
||
| 220 | // { |
||
| 221 | // report($e); |
||
| 222 | // } |
||
| 223 | // |
||
| 224 | // Log::info('Tech Tip ID-'.$tipID.' Created by Customer ID-'.Auth::user()->user_id); |
||
| 225 | // |
||
| 226 | // return $tipID; |
||
| 227 | } |
||
| 228 | |||
| 229 | private function saveNewTipFile(UploadedFile $file, $filePath) |
||
| 230 | { |
||
| 231 | // Clean the file and store it |
||
| 232 | $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
||
| 233 | $file->storeAs($filePath, $fileName); |
||
| 234 | |||
| 235 | // Place file in Files table of DB |
||
| 236 | $newFile = Files::create([ |
||
| 237 | 'file_name' => $fileName, |
||
| 238 | 'file_link' => $filePath.DIRECTORY_SEPARATOR |
||
| 239 | ]); |
||
| 240 | $fileID = $newFile->file_id; |
||
| 241 | |||
| 242 | return $fileID; |
||
| 243 | } |
||
| 244 | |||
| 245 | private function createNewTechTip($tipData) |
||
| 246 | { |
||
| 247 | // Remove any forward slash (/) from the Subject field |
||
| 248 | $tipData->merge(['subject' => str_replace('/', '-', $tipData->subject)]); |
||
| 249 | |||
| 250 | // Enter the tip details and get the tip ID |
||
| 251 | $tip = TechTips::create([ |
||
| 252 | 'subject' => $tipData->subject, |
||
| 253 | 'description' => $tipData->details, |
||
| 254 | 'user_id' => Auth::user()->user_id |
||
| 255 | ]); |
||
| 256 | $tipID = $tip->tip_id; |
||
| 257 | |||
| 258 | // Enter all system tags associated with the tip |
||
| 259 | if(is_array($tipData->sysTags)) |
||
| 260 | { |
||
| 261 | foreach($tipData->sysTags as $tag) |
||
| 262 | { |
||
| 263 | TechTipSystems::create([ |
||
| 264 | 'tip_id' => $tipID, |
||
| 265 | 'sys_id' => $tag |
||
| 266 | ]); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | else |
||
| 270 | { |
||
| 271 | TechTipSystems::create([ |
||
| 272 | 'tip_id' => $tipID, |
||
| 273 | 'sys_id' => $tipData->sysTags |
||
| 274 | ]); |
||
| 275 | } |
||
| 276 | |||
| 277 | return $tipID; |
||
| 278 | } |
||
| 279 | |||
| 280 | // Process an uploaded image to the Tech Tip body |
||
| 281 | public function processImage(Request $request) |
||
| 292 | } |
||
| 293 | |||
| 294 | // Show the Tech Tip details |
||
| 295 | public function details($id, $name) |
||
| 296 | { |
||
| 297 | $tipData = TechTips::where('tip_id', $id)->with('user')->first(); |
||
| 298 | if(empty($tipData)) |
||
| 299 | { |
||
| 300 | Log::warning('User ID-'.Auth::user()->user_id.' tried to access invlaid Tech Tip ID-'.$id.' Name-'.$name); |
||
| 301 | return view('errors.tipNotFound'); |
||
| 302 | } |
||
| 303 | |||
| 304 | $tipFiles = TechTipFiles::where('tip_id', $id) |
||
| 305 | ->join('files', 'tech_tip_files.file_id', '=', 'files.file_id') |
||
| 306 | ->get(); |
||
| 307 | $tipCmts = TechTipComments::where('tip_id', $id) |
||
| 308 | ->get(); |
||
| 309 | $tipSys = TechTipSystems::where('tip_id', $id) |
||
| 310 | ->join('system_types', 'tech_tip_systems.sys_id', '=', 'system_types.sys_id') |
||
| 311 | ->get(); |
||
| 312 | $tipFav = TechTipFavs::where('user_id', Auth::user()->user_id) |
||
| 313 | ->where('tip_id', $id) |
||
| 314 | ->first(); |
||
| 315 | |||
| 316 | return view('tip.details', [ |
||
| 317 | 'data' => $tipData, |
||
| 318 | 'files' => $tipFiles, |
||
| 319 | 'systems' => $tipSys, |
||
| 320 | 'comments' => $tipCmts, |
||
| 321 | 'isFav' => $tipFav |
||
| 322 | ]); |
||
| 323 | } |
||
| 324 | |||
| 325 | // Download a note as a PDF file |
||
| 326 | public function generatePDF($id) |
||
| 327 | { |
||
| 328 | $tipData = TechTips::where('tip_id', $id)->with('user')->first(); |
||
| 329 | $tipCmts = TechTipComments::where('tip_id', $id) |
||
| 330 | ->join('users', 'tech_tip_comments.user_id', '=', 'users.user_id') |
||
| 331 | ->get(); |
||
| 332 | $tipSys = TechTipSystems::where('tip_id', $id) |
||
| 333 | ->join('system_types', 'tech_tip_systems.sys_id', '=', 'system_types.sys_id') |
||
| 334 | ->get(); |
||
| 335 | |||
| 336 | $pdf = PDF::loadView('pdf.techTip', [ |
||
| 337 | 'data' => $tipData, |
||
| 338 | 'systems' => $tipSys, |
||
| 339 | 'comments' => $tipCmts, |
||
| 340 | ]); |
||
| 341 | |||
| 342 | return $pdf->download('Tech Tip - '.$tipData->subject.'.pdf'); |
||
| 343 | } |
||
| 344 | |||
| 345 | // Toggle whether or not the customer is listed as a user favorite |
||
| 346 | public function toggleFav($action, $tipID) |
||
| 347 | { |
||
| 348 | switch ($action) |
||
| 349 | { |
||
| 350 | case 'add': |
||
| 351 | TechTipFavs::create([ |
||
| 352 | 'user_id' => Auth::user()->user_id, |
||
| 353 | 'tip_id' => $tipID |
||
| 354 | ]); |
||
| 355 | break; |
||
| 356 | case 'remove': |
||
| 357 | $tipFav = TechTipFavs::where('user_id', Auth::user()->user_id)->where('tip_id', $tipID)->first(); |
||
| 358 | $tipFav->delete(); |
||
| 359 | break; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | // Edit a Tech Tip |
||
| 364 | public function edit($id) |
||
| 365 | { |
||
| 366 | $tipData = TechTips::find($id); |
||
| 367 | if(empty($tipData)) |
||
| 368 | { |
||
| 369 | return 'tip not found'; |
||
| 370 | } |
||
| 371 | |||
| 372 | $tipFiles = TechTipFiles::where('tip_id', $id) |
||
| 373 | ->join('files', 'tech_tip_files.file_id', '=', 'files.file_id') |
||
| 374 | ->get(); |
||
| 375 | $tipCmts = TechTipComments::where('tip_id', $id) |
||
| 376 | ->get(); |
||
| 377 | $tipSys = TechTipSystems::where('tip_id', $id) |
||
| 378 | ->join('system_types', 'tech_tip_systems.sys_id', '=', 'system_types.sys_id') |
||
| 379 | ->get(); |
||
| 380 | $tipFav = TechTipFavs::where('user_id', Auth::user()->user_id) |
||
| 381 | ->where('tip_id', $id) |
||
| 382 | ->first(); |
||
| 383 | |||
| 384 | $tipS = []; |
||
| 385 | foreach($tipSys as $s) |
||
| 386 | { |
||
| 387 | $tipS[] = $s->sys_id; |
||
| 388 | } |
||
| 389 | |||
| 390 | // Get system types for tip tagging |
||
| 391 | $systems = SystemCategories::with('SystemTypes') |
||
| 392 | ->orderBy('cat_id', 'asc') |
||
| 393 | ->get(); |
||
| 394 | |||
| 395 | $sysArr = []; |
||
| 396 | foreach($systems as $sys) |
||
| 397 | { |
||
| 398 | foreach($sys->SystemTypes as $s) |
||
| 399 | { |
||
| 400 | $sysArr[$sys->name][$s->sys_id] = $s->name; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | return view('tip.form.editTip', [ |
||
| 405 | 'data' => $tipData, |
||
| 406 | 'files' => $tipFiles, |
||
| 407 | 'systems' => $tipS, |
||
| 408 | 'comments' => $tipCmts, |
||
| 409 | 'isFav' => $tipFav, |
||
| 410 | 'sysToTag' => $sysArr |
||
| 411 | ]); |
||
| 412 | } |
||
| 413 | |||
| 414 | // Update the tech tip |
||
| 415 | public function update(Request $request, $id) |
||
| 416 | { |
||
| 417 | $request->validate([ |
||
| 418 | 'subject' => 'required', |
||
| 419 | 'description' => 'required', |
||
| 420 | 'sysTags' => 'required' |
||
| 421 | ]); |
||
| 422 | |||
| 423 | // Remove any forward slash (/) from the Subject field |
||
| 424 | $request->merge(['subject' => str_replace('/', '-', $request->subject)]); |
||
| 425 | |||
| 426 | // update tip details |
||
| 427 | TechTips::find($id)->update([ |
||
| 428 | 'subject' => $request->subject, |
||
| 429 | 'description' => $request->description |
||
| 430 | ]); |
||
| 431 | |||
| 432 | // Enter all system tags associated with the tip after destroying the existing systems |
||
| 433 | TechTipSystems::where('tip_id', $id)->delete(); |
||
| 434 | if(is_array($request->sysTags)) |
||
| 435 | { |
||
| 436 | foreach($request->sysTags as $tag) |
||
| 437 | { |
||
| 438 | TechTipSystems::create([ |
||
| 439 | 'tip_id' => $id, |
||
| 440 | 'sys_id' => $tag |
||
| 441 | ]); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | else |
||
| 445 | { |
||
| 446 | TechTipSystems::create([ |
||
| 447 | 'tip_id' => $id, |
||
| 448 | 'sys_id' => $request->sysTags |
||
| 449 | ]); |
||
| 450 | } |
||
| 451 | |||
| 452 | // Determine if any files were removed |
||
| 453 | $tipFiles = TechTipFiles::where('tip_id', $id)->get(); |
||
| 454 | if(!$tipFiles->isEmpty()) |
||
| 455 | { |
||
| 456 | if(!empty($request->existingFile)) |
||
| 457 | { |
||
| 458 | foreach($tipFiles as $file) |
||
| 459 | { |
||
| 460 | if(!in_array($file->file_id, $request->existingFile)) |
||
| 461 | { |
||
| 462 | TechTipFiles::where('file_id', $file->file_id)->delete(); |
||
| 463 | Files::deleteFile($file->file_id); |
||
| 464 | } |
||
| 465 | } |
||
| 466 | } |
||
| 467 | else |
||
| 468 | { |
||
| 469 | TechTipFiles::where('tip_id', $id)->delete(); |
||
| 470 | } |
||
| 471 | } |
||
| 472 | |||
| 473 | // Process any new files |
||
| 474 | if(!empty($request->file)) |
||
| 475 | { |
||
| 476 | $filePath = config('filesystems.paths.tips').DIRECTORY_SEPARATOR.$id; |
||
| 477 | foreach($request->file as $file) |
||
| 478 | { |
||
| 479 | // Clean the file and store it |
||
| 480 | $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName()); |
||
| 481 | $file->storeAs($filePath, $fileName); |
||
| 482 | |||
| 483 | // Place file in Files table of DB |
||
| 484 | $newFile = Files::create([ |
||
| 485 | 'file_name' => $fileName, |
||
| 486 | 'file_link' => $filePath.DIRECTORY_SEPARATOR |
||
| 487 | ]); |
||
| 488 | $fileID = $newFile->file_id; |
||
| 489 | |||
| 490 | // Place the file in the tech tip files table of DB |
||
| 491 | TechTipFiles::create([ |
||
| 492 | 'tip_id' => $id, |
||
| 493 | 'file_id' => $fileID |
||
| 494 | ]); |
||
| 495 | } |
||
| 496 | } |
||
| 497 | |||
| 498 | Log::info('Tech Tip ID-'.$id.' Updated by User ID-'.Auth::user()->user_id); |
||
| 499 | |||
| 500 | return $id; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Remove the specified resource from storage. |
||
| 505 | * |
||
| 506 | * @param int $id |
||
| 507 | * @return \Illuminate\Http\Response |
||
| 508 | */ |
||
| 509 | public function destroy($id) |
||
| 511 | // |
||
| 512 | } |
||
| 513 | } |
||
| 514 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.