Total Complexity | 71 |
Total Lines | 756 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 0 |
Complex classes like ApplicationByJobController 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 ApplicationByJobController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ApplicationByJobController extends Controller |
||
29 | { |
||
30 | /** |
||
31 | * Display a listing of the resource. |
||
32 | * |
||
33 | * @return \Illuminate\Http\Response |
||
34 | */ |
||
35 | public function index() |
||
37 | // |
||
38 | } |
||
39 | |||
40 | protected function getApplicationFromJob(JobPoster $jobPoster) { |
||
41 | $application = JobApplication::where('applicant_id', Auth::user()->applicant->id) |
||
42 | ->where('job_poster_id', $jobPoster->id)->first(); |
||
43 | if ($application == null) { |
||
44 | $application = new JobApplication(); |
||
45 | $application->job_poster_id = $jobPoster->id; |
||
46 | $application->applicant_id = Auth::user()->applicant->id; |
||
47 | $application->application_status_id = ApplicationStatus::where('name', 'draft')->firstOrFail()->id; |
||
48 | $application->save(); |
||
49 | } |
||
50 | return $application; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Show the form for editing Application basics for the specified job. |
||
55 | * |
||
56 | * @param \App\Models\JobPoster $jobPoster |
||
57 | * @return \Illuminate\Http\Response |
||
58 | */ |
||
59 | public function edit_basics(JobPoster $jobPoster) |
||
91 | |||
92 | ]); |
||
93 | |||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Show the form for editing Application Experience for the specified job. |
||
98 | * |
||
99 | * @param \App\Models\JobPoster $jobPoster |
||
100 | * @return \Illuminate\Http\Response |
||
101 | */ |
||
102 | public function edit_experience(JobPoster $jobPoster) |
||
128 | |||
129 | ]); |
||
130 | |||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Show the form for editing Application Essential Skills for the specified job. |
||
135 | * |
||
136 | * @param \App\Models\JobPoster $jobPoster |
||
137 | * @return \Illuminate\Http\Response |
||
138 | */ |
||
139 | public function edit_essential_skills(JobPoster $jobPoster) |
||
140 | { |
||
141 | |||
142 | $applicant = Auth::user()->applicant; |
||
143 | |||
144 | $application = $this->getApplicationFromJob($jobPoster); |
||
145 | |||
146 | //Ensure user has permissions to view and update application |
||
147 | $this->authorize('view', $application); |
||
148 | $this->authorize('update', $application); |
||
149 | |||
150 | $criteria = [ |
||
151 | 'essential' => $jobPoster->criteria->filter(function($value, $key) { |
||
152 | return $value->criteria_type->name == 'essential'; |
||
153 | }), |
||
154 | 'asset' => $jobPoster->criteria->filter(function($value, $key) { |
||
155 | return $value->criteria_type->name == 'asset'; |
||
156 | }), |
||
157 | ]; |
||
158 | |||
159 | return view('applicant/application_post_03', [ |
||
1 ignored issue
–
show
|
|||
160 | |||
161 | /* Application Template Data */ |
||
162 | "application_step" => 3, |
||
163 | "application_template" => Lang::get("applicant/application_template"), |
||
164 | |||
165 | /* Job Data */ |
||
166 | "job" => $jobPoster, |
||
167 | |||
168 | /* Skills Data */ |
||
169 | "skills" => Skill::all(), |
||
170 | "skill_template" => Lang::get("common/skills"), |
||
171 | "criteria" => $criteria, |
||
172 | |||
173 | /* Applicant Data */ |
||
174 | "applicant" => $applicant, |
||
175 | "job_application" => $application, |
||
176 | |||
177 | /* Submission */ |
||
178 | "form_submit_action" => route('job.application.update.3', $jobPoster) |
||
179 | |||
180 | ]); |
||
181 | |||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Show the form for editing Application Asset Skills for the specified job. |
||
186 | * |
||
187 | * @param \App\Models\JobPoster $jobPoster |
||
188 | * @return \Illuminate\Http\Response |
||
189 | */ |
||
190 | public function edit_asset_skills(JobPoster $jobPoster) |
||
230 | |||
231 | ]); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Show the Application Preview for the application for the specified job. |
||
236 | * |
||
237 | * @param \App\Models\JobPoster $jobPoster |
||
238 | * @return \Illuminate\Http\Response |
||
239 | */ |
||
240 | public function preview(JobPoster $jobPoster) { |
||
241 | |||
242 | $applicant = Auth::user()->applicant; |
||
243 | |||
244 | $application = $this->getApplicationFromJob($jobPoster); |
||
245 | |||
246 | $this->authorize('view', $application); |
||
247 | |||
248 | $criteria = [ |
||
249 | 'essential' => $jobPoster->criteria->filter(function($value, $key) { |
||
250 | return $value->criteria_type->name == 'essential'; |
||
251 | }), |
||
252 | 'asset' => $jobPoster->criteria->filter(function($value, $key) { |
||
253 | return $value->criteria_type->name == 'asset'; |
||
254 | }), |
||
255 | ]; |
||
256 | |||
257 | return view('applicant/application_post_05', [ |
||
1 ignored issue
–
show
|
|||
258 | |||
259 | /* Application Template Data */ |
||
260 | "application_step" => 5, |
||
261 | "application_template" => Lang::get("applicant/application_template"), |
||
262 | "preferred_language_template" => Lang::get('common/preferred_language'), |
||
263 | "citizenship_declaration_template" => Lang::get('common/citizenship_declaration'), |
||
264 | "veteran_status_template" => Lang::get('common/veteran_status'), |
||
265 | |||
266 | /* Job Data */ |
||
267 | "job" => $jobPoster, |
||
268 | |||
269 | /* Skills Data */ |
||
270 | "skills" => Skill::all(), |
||
271 | "skill_template" => Lang::get("common/skills"), |
||
272 | "criteria" => $criteria, |
||
273 | |||
274 | /* Applicant Data */ |
||
275 | "applicant" => $applicant, |
||
276 | "job_application" => $application, |
||
277 | ]); |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * Show the Confirm Submit page for the application for the specified job. |
||
282 | * |
||
283 | * @param \App\Models\JobPoster $jobPoster |
||
284 | * @return \Illuminate\Http\Response |
||
285 | */ |
||
286 | public function confirm(JobPoster $jobPoster) |
||
287 | { |
||
288 | |||
289 | $applicant = Auth::user()->applicant; |
||
290 | |||
291 | $application = $this->getApplicationFromJob($jobPoster); |
||
292 | |||
293 | $this->authorize('update', $application); |
||
294 | |||
295 | return view('applicant/application_post_06', [ |
||
1 ignored issue
–
show
|
|||
296 | /* Application Template Data */ |
||
297 | "application_step" => 6, |
||
298 | "application_template" => Lang::get("applicant/application_template"), |
||
299 | |||
300 | /* Used by tracker partial */ |
||
301 | "job" => $jobPoster, |
||
302 | "job_application" => $application, |
||
303 | |||
304 | /* Submission */ |
||
305 | "form_submit_action" => route('job.application.submit', $jobPoster) |
||
306 | ]); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Show the application submission information. |
||
311 | * |
||
312 | * @param \App\Models\JobPoster $jobPoster |
||
313 | * @return \Illuminate\Http\Response |
||
314 | */ |
||
315 | public function complete(JobPoster $jobPoster) { |
||
316 | |||
317 | /* Include Applicant Data */ |
||
318 | |||
319 | $applicant = Auth::user()->applicant; |
||
320 | |||
321 | /* Include Application Data */ |
||
322 | |||
323 | $application = $this->getApplicationFromJob($jobPoster); |
||
324 | |||
325 | //Ensure user has permissions to view application |
||
326 | $this->authorize('view', $application); |
||
327 | |||
328 | /* Return the Completion View */ |
||
329 | |||
330 | return view('applicant/application_post_complete', [ |
||
1 ignored issue
–
show
|
|||
331 | |||
332 | /* Application Template Data */ |
||
333 | "application_template" => Lang::get("applicant/application_template"), |
||
334 | |||
335 | /* Job Data */ |
||
336 | "job" => $jobPoster, |
||
337 | |||
338 | /* Applicant Data */ |
||
339 | "applicant" => $applicant, |
||
340 | "job_application" => $application |
||
341 | |||
342 | ]); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Update the Application Basics in storage for the specified job. |
||
347 | * |
||
348 | * @param \Illuminate\Http\Request $request |
||
349 | * @param \App\Models\JobPoster $jobPoster |
||
350 | * @return \Illuminate\Http\Response |
||
351 | */ |
||
352 | public function update_basics(Request $request, JobPoster $jobPoster) |
||
353 | { |
||
354 | $applicant = Auth::user()->applicant; |
||
355 | $application = $this->getApplicationFromJob($jobPoster); |
||
356 | |||
357 | //Ensure user has permissions to update this application |
||
358 | $this->authorize('update', $application); |
||
359 | |||
360 | $application->fill([ |
||
361 | 'citizenship_declaration_id' => $request->input('citizenship_declaration_id'), |
||
362 | 'veteran_status_id' => $request->input('veteran_status_id'), |
||
363 | 'preferred_language_id' => $request->input('preferred_language_id'), |
||
364 | ]); |
||
365 | $application->save(); |
||
366 | |||
367 | $questions = $jobPoster->job_poster_questions; |
||
368 | $questionsInput = $request->input('questions'); |
||
369 | foreach($questions as $question) { |
||
370 | $answer = null; |
||
371 | if (isset($questionsInput[$question->id])) { |
||
372 | $answer = $questionsInput[$question->id]; |
||
373 | } |
||
374 | $answerObj = $application->job_application_answers |
||
375 | ->firstWhere('job_poster_question_id', $question->id); |
||
376 | if ($answerObj == null) { |
||
377 | $answerObj = new JobApplicationAnswer(); |
||
378 | $answerObj->job_poster_question_id = $question->id; |
||
379 | $answerObj->job_application_id = $application->id; |
||
380 | } |
||
381 | $answerObj->answer = $answer; |
||
382 | $answerObj->save(); |
||
383 | } |
||
384 | |||
385 | //Redirect to correct page |
||
386 | switch($request->input('submit')) { |
||
387 | case 'save_and_quit': |
||
388 | case 'previous': |
||
389 | return redirect()->route('applications.index'); |
||
1 ignored issue
–
show
|
|||
390 | break; |
||
391 | case 'save_and_continue': |
||
392 | case 'next': |
||
393 | return redirect()->route('job.application.edit.2', $jobPoster); |
||
1 ignored issue
–
show
|
|||
394 | break; |
||
395 | default: |
||
396 | return redirect()->back()->withInput(); |
||
1 ignored issue
–
show
|
|||
397 | break; |
||
398 | } |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Update the Application Basics in storage for the specified job. |
||
403 | * |
||
404 | * @param \Illuminate\Http\Request $request |
||
405 | * @param \App\Models\JobPoster $jobPoster |
||
406 | * @return \Illuminate\Http\Response |
||
407 | */ |
||
408 | public function update_experience(Request $request, JobPoster $jobPoster) |
||
409 | { |
||
410 | $applicant = Auth::user()->applicant; |
||
411 | $application = $this->getApplicationFromJob($jobPoster); |
||
412 | |||
413 | //Ensure user has permissions to update this application |
||
414 | $this->authorize('update', $application); |
||
415 | |||
416 | // Record that the user has saved their experience for this application |
||
417 | $application->experience_saved = true; |
||
418 | $application->save(); |
||
419 | |||
420 | $degrees = $request->input('degrees'); |
||
421 | |||
422 | //Save new degrees |
||
423 | if (isset($degrees['new'])) { |
||
424 | foreach($degrees['new'] as $degreeInput) { |
||
425 | $degree = new Degree(); |
||
426 | $degree->applicant_id = $applicant->id; |
||
427 | $degree->fill([ |
||
428 | 'degree_type_id' => $degreeInput['degree_type_id'], |
||
429 | 'area_of_study' => $degreeInput['area_of_study'], |
||
430 | 'institution' => $degreeInput['institution'], |
||
431 | 'thesis' => $degreeInput['thesis'], |
||
432 | 'start_date' => $degreeInput['start_date'], |
||
433 | 'end_date' => $degreeInput['end_date'] |
||
434 | ]); |
||
435 | $degree->save(); |
||
436 | } |
||
437 | } |
||
438 | |||
439 | //Update old degrees |
||
440 | if (isset($degrees['old'])) { |
||
441 | foreach($degrees['old'] as $id=>$degreeInput) { |
||
442 | //Ensure this degree belongs to this applicant |
||
443 | $degree = $applicant->degrees->firstWhere('id', $id); |
||
444 | if ($degree != null) { |
||
445 | $degree->fill([ |
||
446 | 'degree_type_id' => $degreeInput['degree_type_id'], |
||
447 | 'area_of_study' => $degreeInput['area_of_study'], |
||
448 | 'institution' => $degreeInput['institution'], |
||
449 | 'thesis' => $degreeInput['thesis'], |
||
450 | 'start_date' => $degreeInput['start_date'], |
||
451 | 'end_date' => $degreeInput['end_date'] |
||
452 | ]); |
||
453 | $degree->save(); |
||
454 | } else { |
||
455 | Log::warning('Applicant '.$applicant->id.' attempted to update degree with invalid id '.$id); |
||
456 | } |
||
457 | } |
||
458 | } |
||
459 | |||
460 | $courses = $request->input('courses'); |
||
461 | |||
462 | //Save new courses |
||
463 | if (isset($courses['new'])) { |
||
464 | foreach($courses['new'] as $courseInput) { |
||
465 | $course = new Course(); |
||
466 | $course->applicant_id = $applicant->id; |
||
467 | $course->fill([ |
||
468 | 'name' => $courseInput['name'], |
||
469 | 'institution' => $courseInput['institution'], |
||
470 | 'course_status_id' => $courseInput['course_status_id'], |
||
471 | 'start_date' => $courseInput['start_date'], |
||
472 | 'end_date' => $courseInput['end_date'] |
||
473 | ]); |
||
474 | $course->save(); |
||
475 | } |
||
476 | } |
||
477 | |||
478 | //Update old courses |
||
479 | if (isset($courses['old'])) { |
||
480 | foreach($courses['old'] as $id=>$courseInput) { |
||
481 | //Ensure this course belongs to this applicant |
||
482 | $course = $applicant->courses->firstWhere('id', $id); |
||
483 | if ($course != null) { |
||
484 | $course->fill([ |
||
485 | 'name' => $courseInput['name'], |
||
486 | 'institution' => $courseInput['institution'], |
||
487 | 'course_status_id' => $courseInput['course_status_id'], |
||
488 | 'start_date' => $courseInput['start_date'], |
||
489 | 'end_date' => $courseInput['end_date'] |
||
490 | ]); |
||
491 | $course->save(); |
||
492 | } else { |
||
493 | Log::warning('Applicant '.$applicant->id.' attempted to update course with invalid id '.$id); |
||
494 | } |
||
495 | } |
||
496 | } |
||
497 | |||
498 | $work_experiences = $request->input('work_experiences'); |
||
499 | |||
500 | //Save new work_experiences |
||
501 | if (isset($work_experiences['new'])) { |
||
502 | foreach($work_experiences['new'] as $workExperienceInput) { |
||
503 | $workExperience = new WorkExperience(); |
||
504 | $workExperience->applicant_id = $applicant->id; |
||
505 | $workExperience->fill([ |
||
506 | 'role' => $workExperienceInput['role'], |
||
507 | 'company' => $workExperienceInput['company'], |
||
508 | 'description' => $workExperienceInput['description'], |
||
509 | 'start_date' => $workExperienceInput['start_date'], |
||
510 | 'end_date' => $workExperienceInput['end_date'] |
||
511 | ]); |
||
512 | $workExperience->save(); |
||
513 | } |
||
514 | } |
||
515 | |||
516 | //Update old work_experiences |
||
517 | if (isset($work_experiences['old'])) { |
||
518 | foreach($work_experiences['old'] as $id=>$workExperienceInput) { |
||
519 | //Ensure this work_experience belongs to this applicant |
||
520 | $workExperience = $applicant->work_experiences->firstWhere('id', $id); |
||
521 | if ($workExperience != null) { |
||
522 | $workExperience->fill([ |
||
523 | 'role' => $workExperienceInput['role'], |
||
524 | 'company' => $workExperienceInput['company'], |
||
525 | 'description' => $workExperienceInput['description'], |
||
526 | 'start_date' => $workExperienceInput['start_date'], |
||
527 | 'end_date' => $workExperienceInput['end_date'] |
||
528 | ]); |
||
529 | $workExperience->save(); |
||
530 | } else { |
||
531 | Log::warning('Applicant '.$applicant->id.' attempted to update work_experience with invalid id '.$id); |
||
532 | } |
||
533 | } |
||
534 | } |
||
535 | |||
536 | //Redirect to correct page |
||
537 | switch($request->input('submit')) { |
||
538 | case 'save_and_quit': |
||
539 | return redirect()->route('applications.index'); |
||
1 ignored issue
–
show
|
|||
540 | break; |
||
541 | case 'save_and_continue': |
||
542 | case 'next': |
||
543 | return redirect()->route('job.application.edit.3', $jobPoster); |
||
1 ignored issue
–
show
|
|||
544 | break; |
||
545 | case 'previous': |
||
546 | return redirect()->route('job.application.edit.1', $jobPoster); |
||
1 ignored issue
–
show
|
|||
547 | break; |
||
548 | default: |
||
549 | return redirect()->back()->withInput(); |
||
1 ignored issue
–
show
|
|||
550 | break; |
||
551 | } |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Update the Application Essential Skills in storage for the specified job. |
||
556 | * |
||
557 | * @param \Illuminate\Http\Request $request |
||
558 | * @param \App\Models\JobPoster $jobPoster |
||
559 | * @return \Illuminate\Http\Response |
||
560 | */ |
||
561 | public function update_essential_skills(Request $request, JobPoster $jobPoster) |
||
562 | { |
||
563 | $applicant = Auth::user()->applicant; |
||
564 | $application = $this->getApplicationFromJob($jobPoster); |
||
565 | |||
566 | //Ensure user has permissions to update this application |
||
567 | $this->authorize('update', $application); |
||
568 | |||
569 | $skillDeclarations = $request->input('skill_declarations'); |
||
570 | $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id; |
||
571 | |||
572 | //Save new skill declarartions |
||
573 | if (isset($skillDeclarations['new'])) { |
||
574 | foreach($skillDeclarations['new'] as $skillType => $typeInput) { |
||
575 | foreach($typeInput as $criterion_id=>$skillDeclarationInput) { |
||
576 | $skillDeclaration = new SkillDeclaration(); |
||
577 | $skillDeclaration->applicant_id = $applicant->id; |
||
578 | $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id; |
||
579 | $skillDeclaration->skill_status_id = $claimedStatusId; |
||
580 | $skillDeclaration->fill([ |
||
581 | 'description' => $skillDeclarationInput['description'], |
||
582 | 'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
||
583 | ]); |
||
584 | $skillDeclaration->save(); |
||
585 | |||
586 | $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
||
587 | $skillDeclaration->references()->sync($referenceIds); |
||
588 | |||
589 | $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
||
590 | $skillDeclaration->work_samples()->sync($sampleIds); |
||
591 | } |
||
592 | } |
||
593 | } |
||
594 | |||
595 | //Update old declarations |
||
596 | if (isset($skillDeclarations['old'])) { |
||
597 | foreach($skillDeclarations['old'] as $skillType => $typeInput) { |
||
598 | foreach($typeInput as $id=>$skillDeclarationInput) { |
||
599 | //Ensure this declaration belongs to this applicant |
||
600 | $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id); |
||
601 | if ($skillDeclaration != null) { |
||
602 | //skill_id and skill_status cannot be changed |
||
603 | $skillDeclaration->fill([ |
||
604 | 'description' => $skillDeclarationInput['description'], |
||
605 | 'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
||
606 | ]); |
||
607 | $skillDeclaration->save(); |
||
608 | |||
609 | $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
||
610 | $skillDeclaration->references()->sync($referenceIds); |
||
611 | |||
612 | $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
||
613 | $skillDeclaration->work_samples()->sync($sampleIds); |
||
614 | } else { |
||
615 | Log::warning('Applicant '.$applicant->id.' attempted to update skill declaration with invalid id '.$id); |
||
616 | } |
||
617 | } |
||
618 | } |
||
619 | } |
||
620 | |||
621 | //Redirect to correct page |
||
622 | switch($request->input('submit')) { |
||
623 | case 'save_and_quit': |
||
624 | return redirect()->route('applications.index'); |
||
1 ignored issue
–
show
|
|||
625 | break; |
||
626 | case 'save_and_continue': |
||
627 | case 'next': |
||
628 | return redirect()->route('job.application.edit.4', $jobPoster); |
||
1 ignored issue
–
show
|
|||
629 | break; |
||
630 | case 'previous': |
||
631 | return redirect()->route('job.application.edit.2', $jobPoster); |
||
1 ignored issue
–
show
|
|||
632 | break; |
||
633 | default: |
||
634 | return redirect()->back()->withInput(); |
||
1 ignored issue
–
show
|
|||
635 | break; |
||
636 | } |
||
637 | } |
||
638 | |||
639 | /** |
||
640 | * Update the Application Asset Skills in storage for the specified job. |
||
641 | * |
||
642 | * @param \Illuminate\Http\Request $request |
||
643 | * @param \App\Models\JobPoster $jobPoster |
||
644 | * @return \Illuminate\Http\Response |
||
645 | */ |
||
646 | public function update_asset_skills(Request $request, JobPoster $jobPoster) |
||
721 | } |
||
722 | } |
||
723 | |||
724 | /** |
||
725 | * Submit the Application for the specified job. |
||
726 | * |
||
727 | * @param \Illuminate\Http\Request $request |
||
728 | * @param \App\Models\JobPoster $jobPoster |
||
729 | * @return \Illuminate\Http\Response |
||
730 | */ |
||
731 | public function submit(Request $request, JobPoster $jobPoster) |
||
784 | } |
||
785 | } |
||
786 | } |
||
787 |