Total Complexity | 53 |
Total Lines | 623 |
Duplicated Lines | 0 % |
Coverage | 71.07% |
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 | 4 | 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 |
||
1 ignored issue
–
show
|
|||
230 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
231 | */ |
||
232 | 2 | public function createAsManager(Manager $manager) |
|
233 | { |
||
234 | 2 | $jobPoster = new JobPoster(); |
|
235 | 2 | $jobPoster->manager_id = $manager->id; |
|
236 | 2 | $managerEn = $manager->translate('en'); |
|
237 | 2 | $managerFr = $manager->translate('fr'); |
|
238 | |||
239 | try { |
||
240 | 2 | $jobPoster->fill([ |
|
241 | 2 | 'department_id' => $manager->department_id, |
|
242 | 'en' => [ |
||
243 | 2 | 'branch' => $managerEn->branch, |
|
244 | 2 | 'division' => $managerEn->division, |
|
245 | ], |
||
246 | 'fr' => [ |
||
247 | 2 | 'branch' => $managerFr->branch, |
|
248 | 2 | 'division' => $managerFr->division, |
|
249 | ] |
||
250 | ]); |
||
251 | 2 | $jobPoster->save(); |
|
252 | } catch (\Exception $e) { |
||
253 | throw new AdminException( |
||
254 | 'Manager missing the following required fields: Department, Branch and Division.', |
||
255 | 500, |
||
256 | null, |
||
257 | [ |
||
258 | 'Back to Admin' => '/admin', |
||
259 | 'Manager Profile' => "/manager/profile/$manager->id/edit" |
||
260 | ] |
||
261 | ); |
||
262 | } |
||
263 | |||
264 | 2 | $defaultQuestions = $this->populateDefaultQuestions(); |
|
265 | 2 | if (!empty($defaultQuestions)) { |
|
266 | 2 | $jobPoster->job_poster_questions()->saveMany($defaultQuestions); |
|
267 | } |
||
268 | |||
269 | 2 | return redirect()->route('manager.jobs.edit', $jobPoster->id); |
|
1 ignored issue
–
show
|
|||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Display the form for creating a new Job Poster |
||
274 | * |
||
275 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
276 | * |
||
277 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
278 | */ |
||
279 | public function create(Request $request) |
||
280 | { |
||
281 | return $this->populateCreateView($request); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Display the form for editing an existing Job Poster |
||
286 | * |
||
287 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
288 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
289 | * |
||
290 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
291 | */ |
||
292 | 1 | public function edit(Request $request, JobPoster $jobPoster) |
|
293 | { |
||
294 | 1 | return $this->populateCreateView($request, $jobPoster); |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * Get the manager from the request object and check if creating or editing |
||
299 | * |
||
300 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
301 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
302 | * |
||
303 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
304 | */ |
||
305 | 1 | public function populateCreateView(Request $request, JobPoster $jobPoster = null) |
|
306 | { |
||
307 | 1 | if ($jobPoster == null || $jobPoster->manager == null) { |
|
308 | $manager = $request->user() ? $request->user()->manager : null; |
||
309 | } else { |
||
310 | 1 | $manager = $jobPoster->manager; |
|
311 | } |
||
312 | |||
313 | 1 | if (isset($jobPoster)) { |
|
314 | 1 | $job = $jobPoster; |
|
315 | 1 | $route = ['manager.jobs.update', $jobPoster]; |
|
316 | 1 | $jobHeading = 'manager/job_edit'; |
|
317 | } else { |
||
318 | $job = []; |
||
319 | $defaultQuestions = $this->populateDefaultQuestions(); |
||
320 | if (!empty($defaultQuestions)) { |
||
321 | $job['job_poster_questions'] = $defaultQuestions; |
||
322 | } |
||
323 | $route = ['manager.jobs.store']; |
||
324 | $jobHeading = 'manager/job_create'; |
||
325 | } |
||
326 | |||
327 | 1 | $skillLangs = Lang::get('common/skills'); |
|
328 | |||
329 | 1 | $softSkills = Skill::whereHas( |
|
330 | 1 | 'skill_type', |
|
331 | function ($query) : void { |
||
332 | 1 | $query->where('name', '=', 'soft'); |
|
333 | 1 | } |
|
334 | 1 | )->get() |
|
335 | 1 | ->mapWithKeys( |
|
336 | function ($skill) { |
||
337 | return [ |
||
338 | 1 | $skill->id => $skill->name |
|
339 | ]; |
||
340 | 1 | } |
|
341 | ) |
||
342 | 1 | ->all(); |
|
343 | |||
344 | 1 | $hardSkills = Skill::whereHas( |
|
345 | 1 | 'skill_type', |
|
346 | function ($query) : void { |
||
347 | 1 | $query->where('name', '=', 'hard'); |
|
348 | 1 | } |
|
349 | 1 | )->get() |
|
350 | 1 | ->mapWithKeys( |
|
351 | function ($skill) { |
||
352 | return [ |
||
353 | 1 | $skill->id => $skill->name |
|
354 | ]; |
||
355 | 1 | } |
|
356 | ) |
||
357 | 1 | ->all(); |
|
358 | |||
359 | 1 | asort($softSkills, SORT_LOCALE_STRING); |
|
360 | 1 | asort($hardSkills, SORT_LOCALE_STRING); |
|
361 | |||
362 | $skills = [ |
||
363 | 'essential' => [ |
||
364 | 1 | 'hard' => $hardSkills, |
|
365 | 1 | 'soft' => $softSkills |
|
366 | ], |
||
367 | 'asset' => [ |
||
368 | 1 | 'hard' => $hardSkills, |
|
369 | 1 | 'soft' => $softSkills |
|
370 | ] |
||
371 | ]; |
||
372 | |||
373 | 1 | $skillLevelCollection = SkillLevel::all(); |
|
374 | |||
375 | 1 | $skillLevels = array(); |
|
376 | |||
377 | 1 | $skillLevels['hard'] = $skillLevelCollection->mapWithKeys( |
|
378 | function ($skillLevel) use ($skillLangs) { |
||
379 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]]; |
|
380 | 1 | } |
|
381 | 1 | )->all(); |
|
382 | |||
383 | 1 | $skillLevels['soft'] = $skillLevelCollection->mapWithKeys( |
|
384 | function ($skillLevel) use ($skillLangs) { |
||
385 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]]; |
|
386 | 1 | } |
|
387 | 1 | )->all(); |
|
388 | |||
389 | 1 | return view( |
|
390 | 1 | 'manager/job_create', |
|
391 | [ |
||
392 | /*Localization Strings*/ |
||
393 | 1 | 'job_l10n' => Lang::get('manager/job_create'), |
|
394 | |||
395 | /* Data */ |
||
396 | 1 | 'job' => Lang::get($jobHeading), |
|
397 | 1 | 'manager' => $manager, |
|
398 | 1 | 'provinces' => Province::all(), |
|
399 | 1 | 'departments' => Department::all(), |
|
400 | 1 | 'language_requirments' => LanguageRequirement::all(), |
|
401 | 1 | 'security_clearances' => SecurityClearance::all(), |
|
402 | 1 | 'job' => $job, |
|
403 | 1 | 'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore |
|
404 | 1 | 'skills' => $skills, |
|
405 | 1 | 'skill_levels' => $skillLevels, |
|
406 | 1 | 'skill_template' => $skillLangs, |
|
407 | ] |
||
408 | ); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Create a new resource in storage |
||
413 | * |
||
414 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
415 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
416 | * |
||
417 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index |
||
418 | */ |
||
419 | 3 | public function store(Request $request, JobPoster $jobPoster = null) |
|
420 | { |
||
421 | // Don't allow edits for published Job Posters |
||
422 | // Also check auth while we're at it |
||
423 | 3 | if (isset($jobPoster)) { |
|
424 | 3 | $this->authorize('update', $jobPoster); |
|
425 | 3 | JobPosterValidator::validateUnpublished($jobPoster); |
|
426 | } else { |
||
427 | $this->authorize('create', JobPoster::class); |
||
428 | } |
||
429 | |||
430 | 3 | $input = $request->input(); |
|
431 | |||
432 | 3 | $job = (isset($jobPoster) ? $jobPoster : new JobPoster()); |
|
433 | |||
434 | 3 | if ($job->manager_id == null) { |
|
435 | $job->manager_id = $request->user()->manager->id; |
||
436 | $job->save(); |
||
437 | } |
||
438 | |||
439 | 3 | $this->fillAndSaveJobPoster($input, $job); |
|
440 | |||
441 | 3 | $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster)); |
|
442 | |||
443 | 3 | $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster)); |
|
444 | |||
445 | 3 | $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster)); |
|
446 | |||
447 | 3 | return redirect(route('manager.jobs.show', $job->id)); |
|
448 | } |
||
449 | |||
450 | /** |
||
451 | * Fill Job Poster model's properties and save |
||
452 | * |
||
453 | * @param mixed[] $input Field values. |
||
454 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
455 | * |
||
456 | * @return void |
||
457 | */ |
||
458 | 3 | protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void |
|
459 | { |
||
460 | 3 | $jobPoster->fill( |
|
461 | [ |
||
462 | 3 | 'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id, |
|
463 | 3 | 'term_qty' => $input['term_qty'], |
|
464 | 3 | 'open_date_time' => ptDayStartToUtcTime($input['open_date']), |
|
465 | 3 | 'close_date_time' => ptDayEndToUtcTime($input['close_date']), |
|
466 | 3 | 'start_date_time' => ptDayStartToUtcTime($input['start_date']), |
|
467 | 3 | 'department_id' => $input['department'], |
|
468 | 3 | 'province_id' => $input['province'], |
|
469 | 3 | 'salary_min' => $input['salary_min'], |
|
470 | 3 | 'salary_max' => $input['salary_max'], |
|
471 | 3 | 'noc' => $input['noc'], |
|
472 | 3 | 'classification' => $input['classification'], |
|
473 | 3 | 'security_clearance_id' => $input['security_clearance'], |
|
474 | 3 | 'language_requirement_id' => $input['language_requirement'], |
|
475 | 3 | 'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false), |
|
476 | 'en' => [ |
||
477 | 3 | 'city' => $input['city'], |
|
478 | 3 | 'title' => $input['title']['en'], |
|
479 | 3 | 'impact' => $input['impact']['en'], |
|
480 | 3 | 'branch' => $input['branch']['en'], |
|
481 | 3 | 'division' => $input['division']['en'], |
|
482 | 3 | 'education' => $input['education']['en'], |
|
483 | ], |
||
484 | 'fr' => [ |
||
485 | 3 | 'city' => $input['city'], |
|
486 | 3 | 'title' => $input['title']['fr'], |
|
487 | 3 | 'impact' => $input['impact']['fr'], |
|
488 | 3 | 'branch' => $input['branch']['fr'], |
|
489 | 3 | 'division' => $input['division']['fr'], |
|
490 | 3 | 'education' => $input['education']['fr'], |
|
491 | ], |
||
492 | ] |
||
493 | ); |
||
494 | 3 | $jobPoster->save(); |
|
495 | 3 | } |
|
496 | |||
497 | /** |
||
498 | * Fill Job Poster's tasks and save |
||
499 | * |
||
500 | * @param mixed[] $input Field values. |
||
501 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
502 | * @param boolean $replace Remove existing relationships. |
||
503 | * |
||
504 | * @return void |
||
505 | */ |
||
506 | 3 | protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void |
|
530 | } |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Fill Job Poster's questions and save |
||
535 | * |
||
536 | * @param mixed[] $input Field values. |
||
537 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
538 | * @param boolean $replace Remove existing relationships. |
||
539 | * |
||
540 | * @return void |
||
541 | */ |
||
542 | 3 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void |
|
543 | { |
||
544 | 3 | if ($replace) { |
|
545 | 3 | $jobPoster->job_poster_questions()->delete(); |
|
546 | } |
||
547 | |||
548 | 3 | if (!array_key_exists('question', $input) || !is_array($input['question'])) { |
|
549 | 3 | return; |
|
550 | } |
||
551 | |||
552 | foreach ($input['question'] as $question) { |
||
553 | $jobQuestion = new JobPosterQuestion(); |
||
554 | $jobQuestion->job_poster_id = $jobPoster->id; |
||
555 | $jobQuestion->fill( |
||
556 | [ |
||
557 | 'en' => [ |
||
558 | 'question' => $question['question']['en'], |
||
559 | 'description' => $question['description']['en'] |
||
560 | ], |
||
561 | 'fr' => [ |
||
562 | 'question' => $question['question']['fr'], |
||
563 | 'description' => $question['description']['fr'] |
||
564 | ] |
||
565 | ] |
||
566 | ); |
||
567 | $jobQuestion->save(); |
||
568 | } |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Fill Job Poster's criteria and save |
||
573 | * |
||
574 | * @param mixed[] $input Field values. |
||
575 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
576 | * @param boolean $replace Remove existing relationships. |
||
577 | * |
||
578 | * @return void |
||
579 | */ |
||
580 | 3 | protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void |
|
581 | { |
||
582 | 3 | if ($replace) { |
|
583 | 3 | $jobPoster->criteria()->delete(); |
|
584 | } |
||
585 | |||
586 | 3 | if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) { |
|
587 | 3 | return; |
|
588 | } |
||
589 | |||
590 | $criteria = $input['criteria']; |
||
591 | |||
592 | $combinedCriteria = []; |
||
593 | if (isset($criteria['old'])) { |
||
594 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']); |
||
595 | } |
||
596 | if (isset($criteria['new'])) { |
||
597 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']); |
||
598 | } |
||
599 | |||
600 | if (! empty($combinedCriteria)) { |
||
601 | foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) { |
||
602 | foreach ($criteriaTypeInput as $skillType => $skillTypeInput) { |
||
603 | foreach ($skillTypeInput as $criteriaInput) { |
||
604 | $criteria = new Criteria(); |
||
605 | $criteria->job_poster_id = $jobPoster->id; |
||
606 | $criteria->fill( |
||
607 | [ |
||
608 | 'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id, |
||
609 | 'skill_id' => $criteriaInput['skill_id'], |
||
610 | 'skill_level_id' => $criteriaInput['skill_level_id'], |
||
611 | 'en' => [ |
||
612 | 'description' => $criteriaInput['description']['en'], |
||
613 | ], |
||
614 | 'fr' => [ |
||
615 | 'description' => $criteriaInput['description']['fr'], |
||
616 | ], |
||
617 | ] |
||
618 | ); |
||
619 | $criteria->save(); |
||
620 | } |
||
621 | } |
||
622 | } |
||
623 | } |
||
624 | } |
||
625 | |||
626 | /** |
||
627 | * Get the localized default questions and add them to an array. |
||
628 | * |
||
629 | * @return mixed[]|void |
||
630 | */ |
||
631 | 2 | protected function populateDefaultQuestions() |
|
661 | } |
||
662 | } |
||
663 |