Total Complexity | 64 |
Total Lines | 721 |
Duplicated Lines | 0 % |
Coverage | 66.56% |
Changes | 0 |
Complex classes like JobController 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 JobController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class JobController extends Controller |
||
39 | { |
||
40 | /** |
||
41 | * Display a listing of JobPosters. |
||
42 | * |
||
43 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
44 | */ |
||
45 | public function index() |
||
68 | ]); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * Display a listing of a manager's JobPosters. |
||
73 | * |
||
74 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
75 | */ |
||
76 | 1 | public function managerIndex() |
|
89 | ]); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Submit the Job Poster for review. |
||
94 | * |
||
95 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
96 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
97 | * |
||
98 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
99 | */ |
||
100 | 1 | public function submitForReview(Request $request, JobPoster $jobPoster) |
|
121 | ]); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Delete a draft Job Poster. |
||
126 | * |
||
127 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
128 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | public function destroy(Request $request, JobPoster $jobPoster) : void |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Display the specified job poster. |
||
139 | * |
||
140 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
141 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
142 | * |
||
143 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
144 | */ |
||
145 | 6 | public function show(Request $request, JobPoster $jobPoster) |
|
222 | ] |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Create a blank job poster for the specified manager |
||
228 | * |
||
229 | * @param Manager $manager Incoming Manager object. |
||
230 | * |
||
231 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
232 | */ |
||
233 | 2 | public function createAsManager(Manager $manager) |
|
234 | { |
||
235 | 2 | $jobPoster = new JobPoster(); |
|
236 | 2 | $jobPoster->manager_id = $manager->id; |
|
237 | |||
238 | 2 | $jobPoster->save(); |
|
239 | |||
240 | 2 | $defaultQuestions = $this->populateDefaultQuestions(); |
|
241 | 2 | if (!empty($defaultQuestions)) { |
|
242 | 2 | $jobPoster->job_poster_questions()->saveMany($defaultQuestions); |
|
243 | } |
||
244 | |||
245 | 2 | return redirect()->route('manager.jobs.edit', $jobPoster->id); |
|
1 ignored issue
–
show
|
|||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Display the form for creating a new Job Poster |
||
250 | * |
||
251 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
252 | * |
||
253 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
254 | */ |
||
255 | public function create(Request $request) |
||
256 | { |
||
257 | return $this->populateCreateView($request); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * Display the form for editing an existing Job Poster |
||
262 | * |
||
263 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
264 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
265 | * |
||
266 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
267 | */ |
||
268 | 1 | public function edit(Request $request, JobPoster $jobPoster) |
|
269 | { |
||
270 | 1 | return $this->populateCreateView($request, $jobPoster); |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * Get the manager from the request object and check if creating or editing |
||
275 | * |
||
276 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
277 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
278 | * |
||
279 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
280 | */ |
||
281 | 1 | public function populateCreateView(Request $request, JobPoster $jobPoster = null) |
|
282 | { |
||
283 | 1 | if ($jobPoster == null || $jobPoster->manager == null) { |
|
284 | $manager = $request->user() ? $request->user()->manager : null; |
||
285 | } else { |
||
286 | 1 | $manager = $jobPoster->manager; |
|
287 | } |
||
288 | |||
289 | 1 | if (isset($jobPoster)) { |
|
290 | 1 | $job = $jobPoster; |
|
291 | 1 | $route = ['manager.jobs.update', $jobPoster]; |
|
292 | 1 | $jobHeading = 'manager/job_edit'; |
|
293 | } else { |
||
294 | $job = []; |
||
295 | $defaultQuestions = $this->populateDefaultQuestions(); |
||
296 | if (!empty($defaultQuestions)) { |
||
297 | $job['job_poster_questions'] = $defaultQuestions; |
||
298 | } |
||
299 | $route = ['manager.jobs.store']; |
||
300 | $jobHeading = 'manager/job_create'; |
||
301 | } |
||
302 | |||
303 | 1 | $skillLangs = Lang::get('common/skills'); |
|
304 | |||
305 | 1 | $softSkills = Skill::whereHas( |
|
306 | 1 | 'skill_type', |
|
307 | function ($query) : void { |
||
308 | 1 | $query->where('name', '=', 'soft'); |
|
309 | 1 | } |
|
310 | 1 | )->get() |
|
311 | 1 | ->mapWithKeys( |
|
312 | function ($skill) { |
||
313 | return [ |
||
314 | 1 | $skill->id => $skill->name |
|
315 | ]; |
||
316 | 1 | } |
|
317 | ) |
||
318 | 1 | ->all(); |
|
319 | |||
320 | 1 | $hardSkills = Skill::whereHas( |
|
321 | 1 | 'skill_type', |
|
322 | function ($query) : void { |
||
323 | 1 | $query->where('name', '=', 'hard'); |
|
324 | 1 | } |
|
325 | 1 | )->get() |
|
326 | 1 | ->mapWithKeys( |
|
327 | function ($skill) { |
||
328 | return [ |
||
329 | 1 | $skill->id => $skill->name |
|
330 | ]; |
||
331 | 1 | } |
|
332 | ) |
||
333 | 1 | ->all(); |
|
334 | |||
335 | 1 | asort($softSkills, SORT_LOCALE_STRING); |
|
336 | 1 | asort($hardSkills, SORT_LOCALE_STRING); |
|
337 | |||
338 | $skills = [ |
||
339 | 'essential' => [ |
||
340 | 1 | 'hard' => $hardSkills, |
|
341 | 1 | 'soft' => $softSkills |
|
342 | ], |
||
343 | 'asset' => [ |
||
344 | 1 | 'hard' => $hardSkills, |
|
345 | 1 | 'soft' => $softSkills |
|
346 | ] |
||
347 | ]; |
||
348 | |||
349 | 1 | $skillLevelCollection = SkillLevel::all(); |
|
350 | |||
351 | 1 | $skillLevels = array(); |
|
352 | |||
353 | 1 | $skillLevels['hard'] = $skillLevelCollection->mapWithKeys( |
|
354 | function ($skillLevel) use ($skillLangs) { |
||
355 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]]; |
|
356 | 1 | } |
|
357 | 1 | )->all(); |
|
358 | |||
359 | 1 | $skillLevels['soft'] = $skillLevelCollection->mapWithKeys( |
|
360 | function ($skillLevel) use ($skillLangs) { |
||
361 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]]; |
|
362 | 1 | } |
|
363 | 1 | )->all(); |
|
364 | |||
365 | 1 | return view( |
|
366 | 1 | 'manager/job_create', |
|
367 | [ |
||
368 | /*Localization Strings*/ |
||
369 | 1 | 'job_l10n' => Lang::get('manager/job_create'), |
|
370 | |||
371 | /* Data */ |
||
372 | 1 | 'job' => Lang::get($jobHeading), |
|
373 | 1 | 'manager' => $manager, |
|
374 | 1 | 'provinces' => Province::all(), |
|
375 | 1 | 'departments' => Department::all(), |
|
376 | 1 | 'language_requirments' => LanguageRequirement::all(), |
|
377 | 1 | 'security_clearances' => SecurityClearance::all(), |
|
378 | 1 | 'job' => $job, |
|
379 | 1 | 'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore |
|
380 | 1 | 'skills' => $skills, |
|
381 | 1 | 'skill_levels' => $skillLevels, |
|
382 | 1 | 'skill_template' => $skillLangs, |
|
383 | ] |
||
384 | ); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Create a new resource in storage |
||
389 | * |
||
390 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
391 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
392 | * |
||
393 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index |
||
394 | */ |
||
395 | 6 | public function store(Request $request, JobPoster $jobPoster = null) |
|
396 | { |
||
397 | // Don't allow edits for published Job Posters |
||
398 | // Also check auth while we're at it |
||
399 | 6 | if (isset($jobPoster)) { |
|
400 | 6 | $this->authorize('update', $jobPoster); |
|
401 | 6 | JobPosterValidator::validateUnpublished($jobPoster); |
|
402 | } else { |
||
403 | $this->authorize('create', JobPoster::class); |
||
404 | } |
||
405 | |||
406 | 6 | $input = $request->input(); |
|
407 | |||
408 | 6 | $job = (isset($jobPoster) ? $jobPoster : new JobPoster()); |
|
409 | |||
410 | 6 | if ($job->manager_id == null) { |
|
411 | $job->manager_id = $request->user()->manager->id; |
||
412 | $job->save(); |
||
413 | } |
||
414 | |||
415 | 6 | $this->fillAndSaveJobPoster($input, $job); |
|
416 | |||
417 | 6 | $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster)); |
|
418 | |||
419 | 6 | $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster)); |
|
420 | |||
421 | 6 | $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster)); |
|
422 | |||
423 | 6 | return redirect(route('manager.jobs.show', $job->id)); |
|
424 | } |
||
425 | |||
426 | /** |
||
427 | * Fill Job Poster model's properties and save |
||
428 | * |
||
429 | * @param mixed[] $input Field values. |
||
430 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
431 | * |
||
432 | * @return void |
||
433 | */ |
||
434 | 6 | protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void |
|
435 | { |
||
436 | 6 | $jobPoster->fill( |
|
437 | [ |
||
438 | 6 | 'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id, |
|
439 | 6 | 'term_qty' => $input['term_qty'], |
|
440 | 6 | 'open_date_time' => ptDayStartToUtcTime($input['open_date']), |
|
441 | 6 | 'close_date_time' => ptDayEndToUtcTime($input['close_date']), |
|
442 | 6 | 'start_date_time' => ptDayStartToUtcTime($input['start_date']), |
|
443 | 6 | 'department_id' => $input['department'], |
|
444 | 6 | 'province_id' => $input['province'], |
|
445 | 6 | 'salary_min' => $input['salary_min'], |
|
446 | 6 | 'salary_max' => $input['salary_max'], |
|
447 | 6 | 'noc' => $input['noc'], |
|
448 | 6 | 'classification' => $input['classification'], |
|
449 | 6 | 'security_clearance_id' => $input['security_clearance'], |
|
450 | 6 | 'language_requirement_id' => $input['language_requirement'], |
|
451 | 6 | 'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false), |
|
452 | 'en' => [ |
||
453 | 6 | 'city' => $input['city'], |
|
454 | 6 | 'title' => $input['title']['en'], |
|
455 | 6 | 'impact' => $input['impact']['en'], |
|
456 | 6 | 'branch' => $input['branch']['en'], |
|
457 | 6 | 'division' => $input['division']['en'], |
|
458 | 6 | 'education' => $input['education']['en'], |
|
459 | ], |
||
460 | 'fr' => [ |
||
461 | 6 | 'city' => $input['city'], |
|
462 | 6 | 'title' => $input['title']['fr'], |
|
463 | 6 | 'impact' => $input['impact']['fr'], |
|
464 | 6 | 'branch' => $input['branch']['fr'], |
|
465 | 6 | 'division' => $input['division']['fr'], |
|
466 | 6 | 'education' => $input['education']['fr'], |
|
467 | ], |
||
468 | ] |
||
469 | ); |
||
470 | 6 | $jobPoster->save(); |
|
471 | 6 | } |
|
472 | |||
473 | /** |
||
474 | * Fill Job Poster's tasks and save |
||
475 | * |
||
476 | * @param mixed[] $input Field values. |
||
477 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
478 | * @param boolean $replace Remove existing relationships. |
||
479 | * |
||
480 | * @return void |
||
481 | */ |
||
482 | 6 | protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void |
|
506 | } |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Fill Job Poster's questions and save |
||
511 | * |
||
512 | * @param mixed[] $input Field values. |
||
513 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
514 | * @param boolean $replace Remove existing relationships. |
||
515 | * |
||
516 | * @return void |
||
517 | */ |
||
518 | 6 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void |
|
544 | } |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * Fill Job Poster's criteria and save |
||
549 | * |
||
550 | * @param mixed[] $input Field values. |
||
551 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
552 | * |
||
553 | * @return void |
||
554 | */ |
||
555 | 6 | protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster) : void |
|
556 | { |
||
557 | 6 | $affectedCriteriaIds = []; |
|
558 | |||
559 | 6 | if (array_key_exists('criteria', $input) && !is_array($input['criteria'])) { |
|
560 | $criteria = $input['criteria']; |
||
561 | |||
562 | // Old criteria must be updated, using the criteriaId that comes from the form element names. |
||
563 | if (!empty($criteria['old'])) { |
||
564 | foreach ($criteria['old'] as $criteriaType => $criteriaTypeInput) { |
||
565 | foreach ($criteriaTypeInput as $skillTypeInput) { |
||
566 | foreach ($skillTypeInput as $criteriaId => $criteriaInput) { |
||
567 | $updatedCriteria = $this->processCriteriaForm($jobPoster, $criteriaType, $criteriaInput, $criteriaId); |
||
568 | $affectedCriteriaIds[] = $updatedCriteria->id; |
||
569 | } |
||
570 | } |
||
571 | } |
||
572 | } |
||
573 | // New criteria must be created from scratch, and the id in the form element name can be disregarded. |
||
574 | if (!empty($criteria['new'])) { |
||
575 | foreach ($criteria['new'] as $criteriaType => $criteriaTypeInput) { |
||
576 | foreach ($criteriaTypeInput as $skillTypeInput) { |
||
577 | foreach ($skillTypeInput as $criteriaInput) { |
||
578 | $newCriteria = $this->processCriteriaForm($jobPoster, $criteriaType, $criteriaInput, null); |
||
579 | $affectedCriteriaIds[] = $newCriteria->id; |
||
580 | } |
||
581 | } |
||
582 | } |
||
583 | } |
||
584 | } |
||
585 | |||
586 | // Existing criteria which were not resubmitted must be deleted. |
||
587 | 6 | $deleteCriteria = $jobPoster->criteria()->whereNotIn('id', $affectedCriteriaIds)->get(); |
|
588 | 6 | foreach ($deleteCriteria as $criteria) { |
|
589 | 6 | $this->deleteCriteria($criteria); |
|
590 | } |
||
591 | 6 | } |
|
592 | |||
593 | /** |
||
594 | * Process intput representing a single criteria from Job Poster form. |
||
595 | * |
||
596 | * @param JobPoster $jobPoster |
||
1 ignored issue
–
show
|
|||
597 | * @param string $criteriaType |
||
1 ignored issue
–
show
|
|||
598 | * @param array $criteriaInput |
||
1 ignored issue
–
show
|
|||
599 | * @param integer|null $criteriaId |
||
1 ignored issue
–
show
|
|||
600 | * @return Criteria |
||
601 | */ |
||
602 | protected function processCriteriaForm(JobPoster $jobPoster, string $criteriaType, array $criteriaInput, ?int $criteriaId): Criteria |
||
603 | { |
||
604 | $skillId = $criteriaInput['skill_id']; |
||
605 | |||
606 | //If no description was provided, use the default skill description |
||
607 | $descriptionEn = $criteriaInput['description']['en'] ? |
||
608 | $criteriaInput['description']['en'] : Skill::find($skillId)->getTranslation('description', 'en'); |
||
609 | $descriptionFr = $criteriaInput['description']['fr'] ? |
||
610 | $criteriaInput['description']['fr'] : Skill::find($criteriaInput['skill_id'])->getTranslation('description', 'fr'); |
||
611 | $data = [ |
||
612 | 'skill_id' => $criteriaInput['skill_id'], |
||
613 | 'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id, |
||
614 | 'skill_level_id' => $criteriaInput['skill_level_id'], |
||
615 | 'en' => [ |
||
616 | 'description' => $descriptionEn, |
||
617 | ], |
||
618 | 'fr' => [ |
||
619 | 'description' => $descriptionFr, |
||
620 | ], |
||
621 | ]; |
||
622 | |||
623 | if ($criteriaId) { |
||
624 | $existingCriteria = Criteria::find($criteriaId); |
||
625 | $this->updateCriteria($existingCriteria, $data); |
||
626 | return $existingCriteria; |
||
627 | } else { |
||
628 | $newCriteria = $this->createCriteria($jobPoster, $skillId, $data); |
||
629 | return $newCriteria; |
||
630 | } |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * Create a Job Criteria |
||
635 | * |
||
636 | * @param JobPoster $jobPoster |
||
1 ignored issue
–
show
|
|||
637 | * @param integer $skillId |
||
1 ignored issue
–
show
|
|||
638 | * @param array $data |
||
1 ignored issue
–
show
|
|||
639 | * @return Criteria |
||
640 | */ |
||
641 | protected function createCriteria(JobPoster $jobPoster, int $skillId, array $data): Criteria |
||
656 | } |
||
657 | |||
658 | /** |
||
659 | * Update an existing Job Criteria |
||
660 | * |
||
661 | * @param Criteria $criteria |
||
1 ignored issue
–
show
|
|||
662 | * @param array $data |
||
1 ignored issue
–
show
|
|||
663 | * @return void |
||
664 | */ |
||
665 | protected function updateCriteria(Criteria $criteria, array $data): void |
||
679 | } |
||
680 | |||
681 | /** |
||
682 | * Delete existing Job Criteria |
||
683 | * |
||
684 | * @param Criteria $criteria |
||
1 ignored issue
–
show
|
|||
685 | * @return void |
||
686 | */ |
||
687 | 6 | protected function deleteCriteria(Criteria $criteria): void |
|
688 | { |
||
689 | 6 | $notification = $notification = $this->makeAssessmentPlanNotification( |
|
690 | 6 | 'DELETE', |
|
691 | 6 | $criteria |
|
692 | ); |
||
693 | 6 | $notification->save(); |
|
694 | |||
695 | // Delete assessments related to this criteria |
||
696 | 6 | Assessment::where("criterion_id", $criteria->id)->delete(); |
|
697 | 6 | $criteria->delete(); |
|
698 | 6 | } |
|
699 | |||
700 | /** |
||
701 | * Create a new AssessmentPlanNotification for a modification to a Criteria |
||
702 | * |
||
703 | * @param string $type Can be CREATE, UPDATE or DELETE. |
||
704 | * @param Criteria $criteria |
||
1 ignored issue
–
show
|
|||
705 | * @param integer|null $newSkillId Only used for UPDATE type notifications. |
||
706 | * @param integer|null $newSkillLevelId Only used for UPDATE type notifications. |
||
707 | * @return AssessmentPlanNotification |
||
708 | */ |
||
709 | 6 | protected function makeAssessmentPlanNotification(string $type, Criteria $criteria, $newSkillId = null, $newSkillLevelId = null): AssessmentPlanNotification |
|
710 | { |
||
711 | 6 | $notification = new AssessmentPlanNotification(); |
|
712 | 6 | $notification->job_poster_id = $criteria->job_poster_id; |
|
713 | 6 | $notification->type = $type; |
|
714 | 6 | $notification->criteria_id = $criteria->id; |
|
715 | 6 | $notification->skill_id = $criteria->skill_id; |
|
716 | 6 | $notification->criteria_type_id = $criteria->criteria_type_id; |
|
717 | 6 | $notification->skill_level_id = $criteria->skill_level_id; |
|
718 | 6 | $notification->skill_id_new = $newSkillId; |
|
719 | 6 | $notification->skill_level_id_new = $newSkillLevelId; |
|
720 | 6 | $notification->acknowledged = false; |
|
721 | 6 | return $notification; |
|
722 | } |
||
723 | |||
724 | /** |
||
725 | * Get the localized default questions and add them to an array. |
||
726 | * |
||
727 | * @return mixed[]|void |
||
728 | */ |
||
729 | 2 | protected function populateDefaultQuestions() |
|
759 | } |
||
760 | } |
||
761 |