Total Complexity | 48 |
Total Lines | 605 |
Duplicated Lines | 0 % |
Coverage | 68.89% |
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 |
||
35 | class JobController extends Controller |
||
36 | { |
||
37 | /** |
||
38 | * Display a listing of JobPosters. |
||
39 | * |
||
40 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
41 | */ |
||
42 | public function index() |
||
65 | ]); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Display a listing of a manager's JobPosters. |
||
70 | * |
||
71 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
72 | */ |
||
73 | 1 | public function managerIndex() |
|
74 | { |
||
75 | 1 | $manager = Auth::user()->manager; |
|
|
|||
76 | 1 | $jobs = JobPoster::where('manager_id', $manager->id) |
|
77 | 1 | ->withCount('submitted_applications') |
|
78 | 1 | ->get(); |
|
79 | |||
80 | 1 | return view('manager/job_index', [ |
|
81 | /*Localization Strings*/ |
||
82 | 1 | 'jobs_l10n' => Lang::get('manager/job_index'), |
|
83 | |||
84 | /* Data */ |
||
85 | 1 | 'jobs' => $jobs, |
|
86 | ]); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Submit the Job Poster for review. |
||
91 | * |
||
92 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
93 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
94 | * |
||
95 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
96 | */ |
||
97 | 1 | public function submitForReview(Request $request, JobPoster $jobPoster) |
|
98 | { |
||
99 | // Update review request timestamp |
||
100 | 1 | $jobPoster->review_requested_at = new Date(); |
|
101 | 1 | $jobPoster->save(); |
|
102 | |||
103 | // Refresh model instance with updated DB values. |
||
104 | 1 | $jobPoster = JobPoster::withCount('submitted_applications')->where('id', $jobPoster->id)->first(); |
|
105 | |||
106 | // Send email |
||
107 | 1 | $reviewer_email = config('mail.reviewer_email'); |
|
108 | 1 | if (isset($reviewer_email)) { |
|
109 | 1 | Mail::to($reviewer_email)->send(new JobPosterReviewRequested($jobPoster, Auth::user())); |
|
110 | } else { |
||
111 | Log::error('The reviewer email environment variable is not set.'); |
||
112 | } |
||
113 | |||
114 | 1 | return view('manager/job_index/job', [ |
|
115 | /*Localization Strings*/ |
||
116 | 1 | 'jobs_l10n' => Lang::get('manager/job_index'), |
|
117 | 1 | 'job' => $jobPoster |
|
118 | ]); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Delete a draft Job Poster. |
||
123 | * |
||
124 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
125 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | public function destroy(Request $request, JobPoster $jobPoster) : void |
||
130 | { |
||
131 | $jobPoster->delete(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Display the specified job poster. |
||
136 | * |
||
137 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
138 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
139 | * |
||
140 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
||
141 | */ |
||
142 | 3 | public function show(Request $request, JobPoster $jobPoster) |
|
143 | { |
||
144 | 3 | $jobPoster->load([ |
|
145 | 3 | 'department', |
|
146 | 'criteria.skill.skill_type', |
||
147 | 'manager.team_culture', |
||
148 | 'manager.work_environment' |
||
149 | ]); |
||
150 | |||
151 | 3 | $user = Auth::user(); |
|
152 | |||
153 | //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model |
||
154 | 3 | $workplacePhotos = []; |
|
155 | 3 | foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) { |
|
156 | $workplacePhotos[] = [ |
||
157 | 'description' => $photoCaption->description, |
||
158 | 'url' => '/images/user.png' |
||
159 | ]; |
||
160 | } |
||
161 | |||
162 | //TODO: replace route('manager.show',manager.id) in templates with link using slug |
||
163 | |||
164 | $criteria = [ |
||
165 | 3 | 'essential' => $jobPoster->criteria->filter( |
|
166 | function ($value, $key) { |
||
167 | 2 | return $value->criteria_type->name == 'essential'; |
|
168 | 3 | } |
|
169 | ), |
||
170 | 3 | 'asset' => $jobPoster->criteria->filter( |
|
171 | function ($value, $key) { |
||
172 | 2 | return $value->criteria_type->name == 'asset'; |
|
173 | 3 | } |
|
174 | ), |
||
175 | ]; |
||
176 | |||
177 | 3 | $jobLang = Lang::get('applicant/job_post'); |
|
178 | |||
179 | 3 | $applyButton = []; |
|
180 | 3 | if (!$jobPoster->published && $this->authorize('update', $jobPoster)) { |
|
181 | $applyButton = [ |
||
182 | 1 | 'href' => route('manager.jobs.edit', $jobPoster->id), |
|
183 | 1 | 'title' => $jobLang['apply']['edit_link_title'], |
|
184 | 1 | 'text' => $jobLang['apply']['edit_link_label'], |
|
185 | ]; |
||
186 | 2 | } elseif (Auth::check() && $jobPoster->isOpen()) { |
|
187 | $applyButton = [ |
||
188 | 1 | 'href' => route('job.application.edit.1', $jobPoster->id), |
|
189 | 1 | 'title' => $jobLang['apply']['apply_link_title'], |
|
190 | 1 | 'text' => $jobLang['apply']['apply_link_label'], |
|
191 | ]; |
||
192 | 1 | } elseif (Auth::guest() && $jobPoster->isOpen()) { |
|
193 | $applyButton = [ |
||
194 | 1 | 'href' => route('job.application.edit.1', $jobPoster->id), |
|
195 | 1 | 'title' => $jobLang['apply']['login_link_title'], |
|
196 | 1 | 'text' => $jobLang['apply']['login_link_label'], |
|
197 | ]; |
||
198 | } else { |
||
199 | $applyButton = [ |
||
200 | 'href' => null, |
||
201 | 'title' => null, |
||
202 | 'text' => $jobLang['apply']['job_closed_label'], |
||
203 | ]; |
||
204 | } |
||
205 | |||
206 | 3 | return view( |
|
207 | 3 | 'applicant/job_post', |
|
208 | [ |
||
209 | 3 | 'job_post' => $jobLang, |
|
210 | 3 | 'manager' => $jobPoster->manager, |
|
211 | 3 | 'manager_profile_photo_url' => '/images/user.png', //TODO get real photo |
|
212 | 3 | 'team_culture' => $jobPoster->manager->team_culture, |
|
213 | 3 | 'work_environment' => $jobPoster->manager->work_environment, |
|
214 | 3 | 'workplace_photos' => $workplacePhotos, |
|
215 | 3 | 'job' => $jobPoster, |
|
216 | 3 | 'criteria' => $criteria, |
|
217 | 3 | 'apply_button' => $applyButton, |
|
218 | 3 | 'skill_template' => Lang::get('common/skills'), |
|
219 | ] |
||
220 | ); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Create a blank job poster for the specified manager |
||
225 | * |
||
226 | * @param Manager $manager |
||
1 ignored issue
–
show
|
|||
227 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
228 | */ |
||
229 | 1 | public function createAsManager(Manager $manager) |
|
230 | { |
||
231 | 1 | $jobPoster = new JobPoster(); |
|
232 | 1 | $jobPoster->manager_id = $manager->id; |
|
233 | 1 | $managerEn = $manager->translate('en'); |
|
234 | 1 | $managerFr = $manager->translate('fr'); |
|
235 | 1 | $jobPoster->fill([ |
|
236 | 1 | 'department_id' => $manager->department_id, |
|
237 | 'en' => [ |
||
238 | 1 | 'branch' => $managerEn->branch, |
|
239 | 1 | 'division' => $managerEn->division, |
|
240 | ], |
||
241 | 'fr' => [ |
||
242 | 1 | 'branch' => $managerFr->branch, |
|
243 | 1 | 'division' => $managerFr->division, |
|
244 | ] |
||
245 | ]); |
||
246 | 1 | $jobPoster->save(); |
|
247 | 1 | return redirect()->route('manager.jobs.edit', $jobPoster->id); |
|
1 ignored issue
–
show
|
|||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Display the form for creating a new Job Poster |
||
252 | * |
||
253 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
254 | * |
||
255 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
256 | */ |
||
257 | public function create(Request $request) |
||
258 | { |
||
259 | return $this->populateCreateView($request); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Display the form for editing an existing Job Poster |
||
264 | * |
||
265 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
266 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
267 | * |
||
268 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
269 | */ |
||
270 | 1 | public function edit(Request $request, JobPoster $jobPoster) |
|
271 | { |
||
272 | 1 | return $this->populateCreateView($request, $jobPoster); |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * Get the manager from the request object and check if creating or editing |
||
277 | * |
||
278 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
279 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
280 | * |
||
281 | * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view |
||
282 | */ |
||
283 | 1 | public function populateCreateView(Request $request, JobPoster $jobPoster = null) |
|
284 | { |
||
285 | 1 | $manager = $request->user() ? $request->user()->manager : null; |
|
286 | 1 | if (isset($jobPoster)) { |
|
287 | 1 | $job = $jobPoster; |
|
288 | 1 | $route = ['manager.jobs.update', $jobPoster]; |
|
289 | 1 | $jobHeading = 'manager/job_edit'; |
|
290 | } else { |
||
291 | $job = []; |
||
292 | $defaultQuestions = $this->populateDefaultQuestions(); |
||
293 | if (!empty($defaultQuestions)) { |
||
294 | $job['job_poster_questions'] = $defaultQuestions; |
||
295 | } |
||
296 | $route = ['manager.jobs.store']; |
||
297 | $jobHeading = 'manager/job_create'; |
||
298 | } |
||
299 | |||
300 | 1 | $skillLangs = Lang::get('common/skills'); |
|
301 | |||
302 | 1 | $softSkills = Skill::whereHas( |
|
303 | 1 | 'skill_type', |
|
304 | function ($query) : void { |
||
305 | 1 | $query->where('name', '=', 'soft'); |
|
306 | 1 | } |
|
307 | 1 | )->get() |
|
308 | 1 | ->mapWithKeys( |
|
309 | function ($skill) { |
||
310 | return [ |
||
311 | 1 | $skill->id => $skill->name |
|
312 | ]; |
||
313 | 1 | } |
|
314 | ) |
||
315 | 1 | ->all(); |
|
316 | |||
317 | 1 | $hardSkills = Skill::whereHas( |
|
318 | 1 | 'skill_type', |
|
319 | function ($query) : void { |
||
320 | 1 | $query->where('name', '=', 'hard'); |
|
321 | 1 | } |
|
322 | 1 | )->get() |
|
323 | 1 | ->mapWithKeys( |
|
324 | function ($skill) { |
||
325 | return [ |
||
326 | 1 | $skill->id => $skill->name |
|
327 | ]; |
||
328 | 1 | } |
|
329 | ) |
||
330 | 1 | ->all(); |
|
331 | |||
332 | 1 | asort($softSkills, SORT_LOCALE_STRING); |
|
333 | 1 | asort($hardSkills, SORT_LOCALE_STRING); |
|
334 | |||
335 | $skills = [ |
||
336 | 'essential' => [ |
||
337 | 1 | 'hard' => $hardSkills, |
|
338 | 1 | 'soft' => $softSkills |
|
339 | ], |
||
340 | 'asset' => [ |
||
341 | 1 | 'hard' => $hardSkills, |
|
342 | 1 | 'soft' => $softSkills |
|
343 | ] |
||
344 | ]; |
||
345 | |||
346 | 1 | $skillLevelCollection = SkillLevel::all(); |
|
347 | |||
348 | 1 | $skillLevels = array(); |
|
349 | |||
350 | 1 | $skillLevels['hard'] = $skillLevelCollection->mapWithKeys( |
|
351 | function ($skillLevel) use ($skillLangs) { |
||
352 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]]; |
|
353 | 1 | } |
|
354 | 1 | )->all(); |
|
355 | |||
356 | 1 | $skillLevels['soft'] = $skillLevelCollection->mapWithKeys( |
|
357 | function ($skillLevel) use ($skillLangs) { |
||
358 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]]; |
|
359 | 1 | } |
|
360 | 1 | )->all(); |
|
361 | |||
362 | 1 | return view( |
|
363 | 1 | 'manager/job_create', |
|
364 | [ |
||
365 | /*Localization Strings*/ |
||
366 | 1 | 'job_l10n' => Lang::get('manager/job_create'), |
|
367 | |||
368 | /* Data */ |
||
369 | 1 | 'job' => Lang::get($jobHeading), |
|
370 | 1 | 'manager' => $manager, |
|
371 | 1 | 'provinces' => Province::all(), |
|
372 | 1 | 'departments' => Department::all(), |
|
373 | 1 | 'language_requirments' => LanguageRequirement::all(), |
|
374 | 1 | 'security_clearances' => SecurityClearance::all(), |
|
375 | 1 | 'job' => $job, |
|
376 | 1 | 'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore |
|
377 | 1 | 'skills' => $skills, |
|
378 | 1 | 'skill_levels' => $skillLevels, |
|
379 | 1 | 'skill_template' => $skillLangs, |
|
380 | ] |
||
381 | ); |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * Create a new resource in storage |
||
386 | * |
||
387 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
388 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
389 | * |
||
390 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index |
||
391 | */ |
||
392 | 1 | public function store(Request $request, JobPoster $jobPoster = null) |
|
393 | { |
||
394 | // Don't allow edits for published Job Posters |
||
395 | // Also check auth while we're at it |
||
396 | 1 | if (isset($jobPoster)) { |
|
397 | 1 | $this->authorize('update', $jobPoster); |
|
398 | 1 | JobPosterValidator::validateUnpublished($jobPoster); |
|
399 | } else { |
||
400 | $this->authorize('create', JobPoster::class); |
||
401 | } |
||
402 | |||
403 | 1 | $input = $request->input(); |
|
404 | |||
405 | 1 | $job = (isset($jobPoster) ? $jobPoster : new JobPoster()); |
|
406 | |||
407 | 1 | $job->manager_id = $request->user()->manager->id; |
|
408 | |||
409 | 1 | $this->fillAndSaveJobPoster($input, $job); |
|
410 | |||
411 | 1 | $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster)); |
|
412 | |||
413 | 1 | $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster)); |
|
414 | |||
415 | 1 | $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster)); |
|
416 | |||
417 | 1 | return redirect(route('manager.jobs.show', $job->id)); |
|
418 | } |
||
419 | |||
420 | /** |
||
421 | * Fill Job Poster model's properties and save |
||
422 | * |
||
423 | * @param mixed[] $input Field values. |
||
424 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
425 | * |
||
426 | * @return void |
||
427 | */ |
||
428 | 1 | protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void |
|
469 | 1 | } |
|
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 | 1 | 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 | 1 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void |
|
517 | { |
||
518 | 1 | if ($replace) { |
|
519 | 1 | $jobPoster->job_poster_questions()->delete(); |
|
520 | } |
||
521 | |||
522 | 1 | if (!array_key_exists('question', $input) || !is_array($input['question'])) { |
|
523 | 1 | return; |
|
524 | } |
||
525 | |||
526 | foreach ($input['question'] as $question) { |
||
527 | $jobQuestion = new JobPosterQuestion(); |
||
528 | $jobQuestion->job_poster_id = $jobPoster->id; |
||
529 | $jobQuestion->fill( |
||
530 | [ |
||
531 | 'en' => [ |
||
532 | 'question' => $question['question']['en'], |
||
533 | 'description' => $question['description']['en'] |
||
534 | ], |
||
535 | 'fr' => [ |
||
536 | 'question' => $question['question']['fr'], |
||
537 | 'description' => $question['description']['fr'] |
||
538 | ] |
||
539 | ] |
||
540 | ); |
||
541 | $jobQuestion->save(); |
||
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 | 1 | protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void |
|
555 | { |
||
556 | 1 | if ($replace) { |
|
557 | 1 | $jobPoster->criteria()->delete(); |
|
558 | } |
||
559 | |||
560 | 1 | if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) { |
|
561 | 1 | return; |
|
562 | } |
||
563 | |||
564 | $criteria = $input['criteria']; |
||
565 | |||
566 | $combinedCriteria = []; |
||
567 | if (isset($criteria['old'])) { |
||
568 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']); |
||
569 | } |
||
570 | if (isset($criteria['new'])) { |
||
571 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']); |
||
572 | } |
||
573 | |||
574 | if (! empty($combinedCriteria)) { |
||
575 | foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) { |
||
576 | foreach ($criteriaTypeInput as $skillType => $skillTypeInput) { |
||
577 | foreach ($skillTypeInput as $criteriaInput) { |
||
578 | $criteria = new Criteria(); |
||
579 | $criteria->job_poster_id = $jobPoster->id; |
||
580 | $criteria->fill( |
||
581 | [ |
||
582 | 'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id, |
||
583 | 'skill_id' => $criteriaInput['skill_id'], |
||
584 | 'skill_level_id' => $criteriaInput['skill_level_id'], |
||
585 | 'en' => [ |
||
586 | 'description' => $criteriaInput['description']['en'], |
||
587 | ], |
||
588 | 'fr' => [ |
||
589 | 'description' => $criteriaInput['description']['fr'], |
||
590 | ], |
||
591 | ] |
||
592 | ); |
||
593 | $criteria->save(); |
||
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 | protected function populateDefaultQuestions() |
||
640 | } |
||
641 | } |
||
642 |