Total Complexity | 52 |
Total Lines | 599 |
Duplicated Lines | 0 % |
Coverage | 72.08% |
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 Incoming Manager object. |
||
228 | * |
||
229 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
230 | */ |
||
231 | 2 | public function createAsManager(Manager $manager) |
|
232 | { |
||
233 | 2 | $jobPoster = new JobPoster(); |
|
234 | 2 | $jobPoster->manager_id = $manager->id; |
|
235 | |||
236 | 2 | $jobPoster->save(); |
|
237 | |||
238 | 2 | $defaultQuestions = $this->populateDefaultQuestions(); |
|
239 | 2 | if (!empty($defaultQuestions)) { |
|
240 | 2 | $jobPoster->job_poster_questions()->saveMany($defaultQuestions); |
|
241 | } |
||
242 | |||
243 | 2 | return redirect()->route('manager.jobs.edit', $jobPoster->id); |
|
1 ignored issue
–
show
|
|||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Display the form for creating a new Job Poster |
||
248 | * |
||
249 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
250 | * |
||
251 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
252 | */ |
||
253 | public function create(Request $request) |
||
254 | { |
||
255 | return $this->populateCreateView($request); |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Display the form for editing an existing Job Poster |
||
260 | * |
||
261 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
262 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
263 | * |
||
264 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
265 | */ |
||
266 | 1 | public function edit(Request $request, JobPoster $jobPoster) |
|
267 | { |
||
268 | 1 | return $this->populateCreateView($request, $jobPoster); |
|
269 | } |
||
270 | |||
271 | /** |
||
272 | * Get the manager from the request object and check if creating or editing |
||
273 | * |
||
274 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
275 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
276 | * |
||
277 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
278 | */ |
||
279 | 1 | public function populateCreateView(Request $request, JobPoster $jobPoster = null) |
|
280 | { |
||
281 | 1 | if ($jobPoster == null || $jobPoster->manager == null) { |
|
282 | $manager = $request->user() ? $request->user()->manager : null; |
||
283 | } else { |
||
284 | 1 | $manager = $jobPoster->manager; |
|
285 | } |
||
286 | |||
287 | 1 | if (isset($jobPoster)) { |
|
288 | 1 | $job = $jobPoster; |
|
289 | 1 | $route = ['manager.jobs.update', $jobPoster]; |
|
290 | 1 | $jobHeading = 'manager/job_edit'; |
|
291 | } else { |
||
292 | $job = []; |
||
293 | $defaultQuestions = $this->populateDefaultQuestions(); |
||
294 | if (!empty($defaultQuestions)) { |
||
295 | $job['job_poster_questions'] = $defaultQuestions; |
||
296 | } |
||
297 | $route = ['manager.jobs.store']; |
||
298 | $jobHeading = 'manager/job_create'; |
||
299 | } |
||
300 | |||
301 | 1 | $skillLangs = Lang::get('common/skills'); |
|
302 | |||
303 | 1 | $softSkills = Skill::whereHas( |
|
304 | 1 | 'skill_type', |
|
305 | function ($query) : void { |
||
306 | 1 | $query->where('name', '=', 'soft'); |
|
307 | 1 | } |
|
308 | 1 | )->get() |
|
309 | 1 | ->mapWithKeys( |
|
310 | function ($skill) { |
||
311 | return [ |
||
312 | 1 | $skill->id => $skill->name |
|
313 | ]; |
||
314 | 1 | } |
|
315 | ) |
||
316 | 1 | ->all(); |
|
317 | |||
318 | 1 | $hardSkills = Skill::whereHas( |
|
319 | 1 | 'skill_type', |
|
320 | function ($query) : void { |
||
321 | 1 | $query->where('name', '=', 'hard'); |
|
322 | 1 | } |
|
323 | 1 | )->get() |
|
324 | 1 | ->mapWithKeys( |
|
325 | function ($skill) { |
||
326 | return [ |
||
327 | 1 | $skill->id => $skill->name |
|
328 | ]; |
||
329 | 1 | } |
|
330 | ) |
||
331 | 1 | ->all(); |
|
332 | |||
333 | 1 | asort($softSkills, SORT_LOCALE_STRING); |
|
334 | 1 | asort($hardSkills, SORT_LOCALE_STRING); |
|
335 | |||
336 | $skills = [ |
||
337 | 'essential' => [ |
||
338 | 1 | 'hard' => $hardSkills, |
|
339 | 1 | 'soft' => $softSkills |
|
340 | ], |
||
341 | 'asset' => [ |
||
342 | 1 | 'hard' => $hardSkills, |
|
343 | 1 | 'soft' => $softSkills |
|
344 | ] |
||
345 | ]; |
||
346 | |||
347 | 1 | $skillLevelCollection = SkillLevel::all(); |
|
348 | |||
349 | 1 | $skillLevels = array(); |
|
350 | |||
351 | 1 | $skillLevels['hard'] = $skillLevelCollection->mapWithKeys( |
|
352 | function ($skillLevel) use ($skillLangs) { |
||
353 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]]; |
|
354 | 1 | } |
|
355 | 1 | )->all(); |
|
356 | |||
357 | 1 | $skillLevels['soft'] = $skillLevelCollection->mapWithKeys( |
|
358 | function ($skillLevel) use ($skillLangs) { |
||
359 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]]; |
|
360 | 1 | } |
|
361 | 1 | )->all(); |
|
362 | |||
363 | 1 | return view( |
|
364 | 1 | 'manager/job_create', |
|
365 | [ |
||
366 | /*Localization Strings*/ |
||
367 | 1 | 'job_l10n' => Lang::get('manager/job_create'), |
|
368 | |||
369 | /* Data */ |
||
370 | 1 | 'job' => Lang::get($jobHeading), |
|
371 | 1 | 'manager' => $manager, |
|
372 | 1 | 'provinces' => Province::all(), |
|
373 | 1 | 'departments' => Department::all(), |
|
374 | 1 | 'language_requirments' => LanguageRequirement::all(), |
|
375 | 1 | 'security_clearances' => SecurityClearance::all(), |
|
376 | 1 | 'job' => $job, |
|
377 | 1 | 'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore |
|
378 | 1 | 'skills' => $skills, |
|
379 | 1 | 'skill_levels' => $skillLevels, |
|
380 | 1 | 'skill_template' => $skillLangs, |
|
381 | ] |
||
382 | ); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Create a new resource in storage |
||
387 | * |
||
388 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
389 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
390 | * |
||
391 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index |
||
392 | */ |
||
393 | 3 | public function store(Request $request, JobPoster $jobPoster = null) |
|
394 | { |
||
395 | // Don't allow edits for published Job Posters |
||
396 | // Also check auth while we're at it |
||
397 | 3 | if (isset($jobPoster)) { |
|
398 | 3 | $this->authorize('update', $jobPoster); |
|
399 | 3 | JobPosterValidator::validateUnpublished($jobPoster); |
|
400 | } else { |
||
401 | $this->authorize('create', JobPoster::class); |
||
402 | } |
||
403 | |||
404 | 3 | $input = $request->input(); |
|
405 | |||
406 | 3 | $job = (isset($jobPoster) ? $jobPoster : new JobPoster()); |
|
407 | |||
408 | 3 | if ($job->manager_id == null) { |
|
409 | $job->manager_id = $request->user()->manager->id; |
||
410 | $job->save(); |
||
411 | } |
||
412 | |||
413 | 3 | $this->fillAndSaveJobPoster($input, $job); |
|
414 | |||
415 | 3 | $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster)); |
|
416 | |||
417 | 3 | $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster)); |
|
418 | |||
419 | 3 | $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster)); |
|
420 | |||
421 | 3 | return redirect(route('manager.jobs.show', $job->id)); |
|
422 | } |
||
423 | |||
424 | /** |
||
425 | * Fill Job Poster model's properties and save |
||
426 | * |
||
427 | * @param mixed[] $input Field values. |
||
428 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
429 | * |
||
430 | * @return void |
||
431 | */ |
||
432 | 3 | protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void |
|
433 | { |
||
434 | 3 | $jobPoster->fill( |
|
435 | [ |
||
436 | 3 | 'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id, |
|
437 | 3 | 'term_qty' => $input['term_qty'], |
|
438 | 3 | 'open_date_time' => ptDayStartToUtcTime($input['open_date']), |
|
439 | 3 | 'close_date_time' => ptDayEndToUtcTime($input['close_date']), |
|
440 | 3 | 'start_date_time' => ptDayStartToUtcTime($input['start_date']), |
|
441 | 3 | 'department_id' => $input['department'], |
|
442 | 3 | 'province_id' => $input['province'], |
|
443 | 3 | 'salary_min' => $input['salary_min'], |
|
444 | 3 | 'salary_max' => $input['salary_max'], |
|
445 | 3 | 'noc' => $input['noc'], |
|
446 | 3 | 'classification' => $input['classification'], |
|
447 | 3 | 'security_clearance_id' => $input['security_clearance'], |
|
448 | 3 | 'language_requirement_id' => $input['language_requirement'], |
|
449 | 3 | 'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false), |
|
450 | 'en' => [ |
||
451 | 3 | 'city' => $input['city'], |
|
452 | 3 | 'title' => $input['title']['en'], |
|
453 | 3 | 'impact' => $input['impact']['en'], |
|
454 | 3 | 'branch' => $input['branch']['en'], |
|
455 | 3 | 'division' => $input['division']['en'], |
|
456 | 3 | 'education' => $input['education']['en'], |
|
457 | ], |
||
458 | 'fr' => [ |
||
459 | 3 | 'city' => $input['city'], |
|
460 | 3 | 'title' => $input['title']['fr'], |
|
461 | 3 | 'impact' => $input['impact']['fr'], |
|
462 | 3 | 'branch' => $input['branch']['fr'], |
|
463 | 3 | 'division' => $input['division']['fr'], |
|
464 | 3 | 'education' => $input['education']['fr'], |
|
465 | ], |
||
466 | ] |
||
467 | ); |
||
468 | 3 | $jobPoster->save(); |
|
469 | 3 | } |
|
470 | |||
471 | /** |
||
472 | * Fill Job Poster's tasks and save |
||
473 | * |
||
474 | * @param mixed[] $input Field values. |
||
475 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
476 | * @param boolean $replace Remove existing relationships. |
||
477 | * |
||
478 | * @return void |
||
479 | */ |
||
480 | 3 | protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void |
|
504 | } |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Fill Job Poster's questions and save |
||
509 | * |
||
510 | * @param mixed[] $input Field values. |
||
511 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
512 | * @param boolean $replace Remove existing relationships. |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | 3 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void |
|
542 | } |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Fill Job Poster's criteria and save |
||
547 | * |
||
548 | * @param mixed[] $input Field values. |
||
549 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
550 | * @param boolean $replace Remove existing relationships. |
||
551 | * |
||
552 | * @return void |
||
553 | */ |
||
554 | 3 | protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void |
|
594 | } |
||
595 | } |
||
596 | } |
||
597 | } |
||
598 | } |
||
599 | |||
600 | /** |
||
601 | * Get the localized default questions and add them to an array. |
||
602 | * |
||
603 | * @return mixed[]|void |
||
604 | */ |
||
605 | 2 | protected function populateDefaultQuestions() |
|
635 | } |
||
636 | } |
||
637 |