Total Complexity | 49 |
Total Lines | 607 |
Duplicated Lines | 0 % |
Coverage | 73.9% |
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 |
||
36 | class JobController extends Controller |
||
37 | { |
||
38 | /** |
||
39 | * Display a listing of JobPosters. |
||
40 | * |
||
41 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
42 | */ |
||
43 | public function index() |
||
44 | { |
||
45 | $now = Carbon::now(); |
||
46 | |||
47 | // Find published jobs that are currently open for applications. |
||
48 | // Eager load required relationships: Department, Province, JobTerm. |
||
49 | // Eager load the count of submitted applications, to prevent the relationship |
||
50 | // from being actually loaded and firing off events. |
||
51 | $jobs = JobPoster::where('open_date_time', '<=', $now) |
||
52 | ->where('close_date_time', '>=', $now) |
||
53 | ->where('published', true) |
||
54 | ->with([ |
||
55 | 'department', |
||
56 | 'province', |
||
57 | 'job_term', |
||
58 | ]) |
||
59 | ->withCount([ |
||
60 | 'submitted_applications', |
||
61 | ]) |
||
62 | ->get(); |
||
63 | return view('applicant/job_index', [ |
||
64 | 'job_index' => Lang::get('applicant/job_index'), |
||
65 | 'jobs' => $jobs |
||
66 | ]); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Display a listing of a manager's JobPosters. |
||
71 | * |
||
72 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
73 | */ |
||
74 | 1 | public function managerIndex() |
|
75 | { |
||
76 | 1 | $manager = Auth::user()->manager; |
|
|
|||
77 | 1 | $jobs = JobPoster::where('manager_id', $manager->id) |
|
78 | 1 | ->withCount('submitted_applications') |
|
79 | 1 | ->get(); |
|
80 | |||
81 | 1 | return view('manager/job_index', [ |
|
82 | /*Localization Strings*/ |
||
83 | 1 | 'jobs_l10n' => Lang::get('manager/job_index'), |
|
84 | |||
85 | /* Data */ |
||
86 | 1 | 'jobs' => $jobs, |
|
87 | ]); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Submit the Job Poster for review. |
||
92 | * |
||
93 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
94 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
95 | * |
||
96 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
97 | */ |
||
98 | 1 | public function submitForReview(Request $request, JobPoster $jobPoster) |
|
99 | { |
||
100 | // Update review request timestamp |
||
101 | 1 | $jobPoster->review_requested_at = new Date(); |
|
102 | 1 | $jobPoster->save(); |
|
103 | |||
104 | // Refresh model instance with updated DB values. |
||
105 | 1 | $jobPoster = JobPoster::withCount('submitted_applications')->where('id', $jobPoster->id)->first(); |
|
106 | |||
107 | // Send email |
||
108 | 1 | $reviewer_email = config('mail.reviewer_email'); |
|
109 | 1 | if (isset($reviewer_email)) { |
|
110 | 1 | Mail::to($reviewer_email)->send(new JobPosterReviewRequested($jobPoster, Auth::user())); |
|
111 | } else { |
||
112 | Log::error('The reviewer email environment variable is not set.'); |
||
113 | } |
||
114 | |||
115 | 1 | return view('manager/job_index/job', [ |
|
116 | /*Localization Strings*/ |
||
117 | 1 | 'jobs_l10n' => Lang::get('manager/job_index'), |
|
118 | 1 | 'job' => $jobPoster |
|
119 | ]); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Delete a draft Job Poster. |
||
124 | * |
||
125 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
126 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
127 | * |
||
128 | * @return void |
||
129 | */ |
||
130 | public function destroy(Request $request, JobPoster $jobPoster) : void |
||
131 | { |
||
132 | $jobPoster->delete(); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Display the specified job poster. |
||
137 | * |
||
138 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
139 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
140 | * |
||
141 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
142 | */ |
||
143 | 4 | public function show(Request $request, JobPoster $jobPoster) |
|
220 | ] |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Create a blank job poster for the specified manager |
||
226 | * |
||
227 | * @param Manager $manager |
||
1 ignored issue
–
show
|
|||
228 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
229 | */ |
||
230 | 2 | public function createAsManager(Manager $manager) |
|
231 | { |
||
232 | 2 | $jobPoster = new JobPoster(); |
|
233 | 2 | $jobPoster->manager_id = $manager->id; |
|
234 | 2 | $managerEn = $manager->translate('en'); |
|
235 | 2 | $managerFr = $manager->translate('fr'); |
|
236 | |||
237 | 2 | $jobPoster->fill([ |
|
238 | 2 | 'department_id' => $manager->department_id, |
|
239 | 'en' => [ |
||
240 | 2 | 'branch' => $managerEn->branch, |
|
241 | 2 | 'division' => $managerEn->division, |
|
242 | ], |
||
243 | 'fr' => [ |
||
244 | 2 | 'branch' => $managerFr->branch, |
|
245 | 2 | 'division' => $managerFr->division, |
|
246 | ] |
||
247 | ]); |
||
248 | 2 | $jobPoster->save(); |
|
249 | |||
250 | 2 | $defaultQuestions = $this->populateDefaultQuestions(); |
|
251 | 2 | if (!empty($defaultQuestions)) { |
|
252 | 2 | $jobPoster->job_poster_questions()->saveMany($defaultQuestions); |
|
253 | } |
||
254 | |||
255 | 2 | return redirect()->route('manager.jobs.edit', $jobPoster->id); |
|
1 ignored issue
–
show
|
|||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Display the form for creating a new Job Poster |
||
260 | * |
||
261 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
262 | * |
||
263 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
264 | */ |
||
265 | public function create(Request $request) |
||
266 | { |
||
267 | return $this->populateCreateView($request); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Display the form for editing an existing Job Poster |
||
272 | * |
||
273 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
274 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
275 | * |
||
276 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
277 | */ |
||
278 | 1 | public function edit(Request $request, JobPoster $jobPoster) |
|
279 | { |
||
280 | 1 | return $this->populateCreateView($request, $jobPoster); |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * Get the manager from the request object and check if creating or editing |
||
285 | * |
||
286 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
287 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
288 | * |
||
289 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
290 | */ |
||
291 | 1 | public function populateCreateView(Request $request, JobPoster $jobPoster = null) |
|
292 | { |
||
293 | 1 | $manager = $request->user() ? $request->user()->manager : null; |
|
294 | 1 | if (isset($jobPoster)) { |
|
295 | 1 | $job = $jobPoster; |
|
296 | 1 | $route = ['manager.jobs.update', $jobPoster]; |
|
297 | 1 | $jobHeading = 'manager/job_edit'; |
|
298 | } else { |
||
299 | $job = []; |
||
300 | $defaultQuestions = $this->populateDefaultQuestions(); |
||
301 | if (!empty($defaultQuestions)) { |
||
302 | $job['job_poster_questions'] = $defaultQuestions; |
||
303 | } |
||
304 | $route = ['manager.jobs.store']; |
||
305 | $jobHeading = 'manager/job_create'; |
||
306 | } |
||
307 | |||
308 | 1 | $skillLangs = Lang::get('common/skills'); |
|
309 | |||
310 | 1 | $softSkills = Skill::whereHas( |
|
311 | 1 | 'skill_type', |
|
312 | function ($query) : void { |
||
313 | 1 | $query->where('name', '=', 'soft'); |
|
314 | 1 | } |
|
315 | 1 | )->get() |
|
316 | 1 | ->mapWithKeys( |
|
317 | function ($skill) { |
||
318 | return [ |
||
319 | 1 | $skill->id => $skill->name |
|
320 | ]; |
||
321 | 1 | } |
|
322 | ) |
||
323 | 1 | ->all(); |
|
324 | |||
325 | 1 | $hardSkills = Skill::whereHas( |
|
326 | 1 | 'skill_type', |
|
327 | function ($query) : void { |
||
328 | 1 | $query->where('name', '=', 'hard'); |
|
329 | 1 | } |
|
330 | 1 | )->get() |
|
331 | 1 | ->mapWithKeys( |
|
332 | function ($skill) { |
||
333 | return [ |
||
334 | 1 | $skill->id => $skill->name |
|
335 | ]; |
||
336 | 1 | } |
|
337 | ) |
||
338 | 1 | ->all(); |
|
339 | |||
340 | 1 | asort($softSkills, SORT_LOCALE_STRING); |
|
341 | 1 | asort($hardSkills, SORT_LOCALE_STRING); |
|
342 | |||
343 | $skills = [ |
||
344 | 'essential' => [ |
||
345 | 1 | 'hard' => $hardSkills, |
|
346 | 1 | 'soft' => $softSkills |
|
347 | ], |
||
348 | 'asset' => [ |
||
349 | 1 | 'hard' => $hardSkills, |
|
350 | 1 | 'soft' => $softSkills |
|
351 | ] |
||
352 | ]; |
||
353 | |||
354 | 1 | $skillLevelCollection = SkillLevel::all(); |
|
355 | |||
356 | 1 | $skillLevels = array(); |
|
357 | |||
358 | 1 | $skillLevels['hard'] = $skillLevelCollection->mapWithKeys( |
|
359 | function ($skillLevel) use ($skillLangs) { |
||
360 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]]; |
|
361 | 1 | } |
|
362 | 1 | )->all(); |
|
363 | |||
364 | 1 | $skillLevels['soft'] = $skillLevelCollection->mapWithKeys( |
|
365 | function ($skillLevel) use ($skillLangs) { |
||
366 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]]; |
|
367 | 1 | } |
|
368 | 1 | )->all(); |
|
369 | |||
370 | 1 | return view( |
|
371 | 1 | 'manager/job_create', |
|
372 | [ |
||
373 | /*Localization Strings*/ |
||
374 | 1 | 'job_l10n' => Lang::get('manager/job_create'), |
|
375 | |||
376 | /* Data */ |
||
377 | 1 | 'job' => Lang::get($jobHeading), |
|
378 | 1 | 'manager' => $manager, |
|
379 | 1 | 'provinces' => Province::all(), |
|
380 | 1 | 'departments' => Department::all(), |
|
381 | 1 | 'language_requirments' => LanguageRequirement::all(), |
|
382 | 1 | 'security_clearances' => SecurityClearance::all(), |
|
383 | 1 | 'job' => $job, |
|
384 | 1 | 'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore |
|
385 | 1 | 'skills' => $skills, |
|
386 | 1 | 'skill_levels' => $skillLevels, |
|
387 | 1 | 'skill_template' => $skillLangs, |
|
388 | ] |
||
389 | ); |
||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Create a new resource in storage |
||
394 | * |
||
395 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
396 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
397 | * |
||
398 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index |
||
399 | */ |
||
400 | 2 | public function store(Request $request, JobPoster $jobPoster = null) |
|
401 | { |
||
402 | // Don't allow edits for published Job Posters |
||
403 | // Also check auth while we're at it |
||
404 | 2 | if (isset($jobPoster)) { |
|
405 | 2 | $this->authorize('update', $jobPoster); |
|
406 | 2 | JobPosterValidator::validateUnpublished($jobPoster); |
|
407 | } else { |
||
408 | $this->authorize('create', JobPoster::class); |
||
409 | } |
||
410 | |||
411 | 2 | $input = $request->input(); |
|
412 | |||
413 | 2 | $job = (isset($jobPoster) ? $jobPoster : new JobPoster()); |
|
414 | |||
415 | 2 | $job->manager_id = $request->user()->manager->id; |
|
416 | |||
417 | 2 | $this->fillAndSaveJobPoster($input, $job); |
|
418 | |||
419 | 2 | $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster)); |
|
420 | |||
421 | 2 | $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster)); |
|
422 | |||
423 | 2 | $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster)); |
|
424 | |||
425 | 2 | return redirect(route('manager.jobs.show', $job->id)); |
|
426 | } |
||
427 | |||
428 | /** |
||
429 | * Fill Job Poster model's properties and save |
||
430 | * |
||
431 | * @param mixed[] $input Field values. |
||
432 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
433 | * |
||
434 | * @return void |
||
435 | */ |
||
436 | 2 | protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void |
|
437 | { |
||
438 | 2 | $closeDate = new Date($input['close_date']); |
|
439 | 2 | $openDate = new Date($input['open_date']); |
|
440 | 2 | $startDate = new Date($input['start_date']); |
|
441 | |||
442 | 2 | $jobPoster->fill( |
|
443 | [ |
||
444 | 2 | 'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id, |
|
445 | 2 | 'term_qty' => $input['term_qty'], |
|
446 | 2 | 'open_date_time' => pstDayStartToUtcTime($openDate->year, $openDate->month, $openDate->day), |
|
447 | 2 | 'close_date_time' => pstDayEndToUtcTime($closeDate->year, $closeDate->month, $closeDate->day), |
|
448 | 2 | 'start_date_time' => pstDayStartToUtcTime($startDate->year, $startDate->month, $startDate->day), |
|
449 | 2 | 'department_id' => $input['department'], |
|
450 | 2 | 'province_id' => $input['province'], |
|
451 | 2 | 'salary_min' => $input['salary_min'], |
|
452 | 2 | 'salary_max' => $input['salary_max'], |
|
453 | 2 | 'noc' => $input['noc'], |
|
454 | 2 | 'classification' => $input['classification'], |
|
455 | 2 | 'security_clearance_id' => $input['security_clearance'], |
|
456 | 2 | 'language_requirement_id' => $input['language_requirement'], |
|
457 | 2 | 'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false), |
|
458 | 'en' => [ |
||
459 | 2 | 'city' => $input['city'], |
|
460 | 2 | 'title' => $input['title']['en'], |
|
461 | 2 | 'impact' => $input['impact']['en'], |
|
462 | 2 | 'branch' => $input['branch']['en'], |
|
463 | 2 | 'division' => $input['division']['en'], |
|
464 | 2 | 'education' => $input['education']['en'], |
|
465 | ], |
||
466 | 'fr' => [ |
||
467 | 2 | 'city' => $input['city'], |
|
468 | 2 | 'title' => $input['title']['fr'], |
|
469 | 2 | 'impact' => $input['impact']['fr'], |
|
470 | 2 | 'branch' => $input['branch']['fr'], |
|
471 | 2 | 'division' => $input['division']['fr'], |
|
472 | 2 | 'education' => $input['education']['fr'], |
|
473 | ], |
||
474 | ] |
||
475 | ); |
||
476 | 2 | $jobPoster->save(); |
|
477 | 2 | } |
|
478 | |||
479 | /** |
||
480 | * Fill Job Poster's tasks and save |
||
481 | * |
||
482 | * @param mixed[] $input Field values. |
||
483 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
484 | * @param boolean $replace Remove existing relationships. |
||
485 | * |
||
486 | * @return void |
||
487 | */ |
||
488 | 2 | protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void |
|
512 | } |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * Fill Job Poster's questions and save |
||
517 | * |
||
518 | * @param mixed[] $input Field values. |
||
519 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
520 | * @param boolean $replace Remove existing relationships. |
||
521 | * |
||
522 | * @return void |
||
523 | */ |
||
524 | 2 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void |
|
525 | { |
||
526 | 2 | if ($replace) { |
|
527 | 2 | $jobPoster->job_poster_questions()->delete(); |
|
528 | } |
||
529 | |||
530 | 2 | if (!array_key_exists('question', $input) || !is_array($input['question'])) { |
|
531 | 2 | return; |
|
532 | } |
||
533 | |||
534 | foreach ($input['question'] as $question) { |
||
535 | $jobQuestion = new JobPosterQuestion(); |
||
536 | $jobQuestion->job_poster_id = $jobPoster->id; |
||
537 | $jobQuestion->fill( |
||
538 | [ |
||
539 | 'en' => [ |
||
540 | 'question' => $question['question']['en'], |
||
541 | 'description' => $question['description']['en'] |
||
542 | ], |
||
543 | 'fr' => [ |
||
544 | 'question' => $question['question']['fr'], |
||
545 | 'description' => $question['description']['fr'] |
||
546 | ] |
||
547 | ] |
||
548 | ); |
||
549 | $jobQuestion->save(); |
||
550 | } |
||
551 | } |
||
552 | |||
553 | /** |
||
554 | * Fill Job Poster's criteria and save |
||
555 | * |
||
556 | * @param mixed[] $input Field values. |
||
557 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
558 | * @param boolean $replace Remove existing relationships. |
||
559 | * |
||
560 | * @return void |
||
561 | */ |
||
562 | 2 | protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void |
|
563 | { |
||
564 | 2 | if ($replace) { |
|
565 | 2 | $jobPoster->criteria()->delete(); |
|
566 | } |
||
567 | |||
568 | 2 | if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) { |
|
569 | 2 | return; |
|
570 | } |
||
571 | |||
572 | $criteria = $input['criteria']; |
||
573 | |||
574 | $combinedCriteria = []; |
||
575 | if (isset($criteria['old'])) { |
||
576 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']); |
||
577 | } |
||
578 | if (isset($criteria['new'])) { |
||
579 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']); |
||
580 | } |
||
581 | |||
582 | if (! empty($combinedCriteria)) { |
||
583 | foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) { |
||
584 | foreach ($criteriaTypeInput as $skillType => $skillTypeInput) { |
||
585 | foreach ($skillTypeInput as $criteriaInput) { |
||
586 | $criteria = new Criteria(); |
||
587 | $criteria->job_poster_id = $jobPoster->id; |
||
588 | $criteria->fill( |
||
589 | [ |
||
590 | 'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id, |
||
591 | 'skill_id' => $criteriaInput['skill_id'], |
||
592 | 'skill_level_id' => $criteriaInput['skill_level_id'], |
||
593 | 'en' => [ |
||
594 | 'description' => $criteriaInput['description']['en'], |
||
595 | ], |
||
596 | 'fr' => [ |
||
597 | 'description' => $criteriaInput['description']['fr'], |
||
598 | ], |
||
599 | ] |
||
600 | ); |
||
601 | $criteria->save(); |
||
602 | } |
||
603 | } |
||
604 | } |
||
605 | } |
||
606 | } |
||
607 | |||
608 | /** |
||
609 | * Get the localized default questions and add them to an array. |
||
610 | * |
||
611 | * @return mixed[]|void |
||
612 | */ |
||
613 | 2 | protected function populateDefaultQuestions() |
|
643 | } |
||
644 | } |
||
645 |