| Total Complexity | 52 |
| Total Lines | 615 |
| Duplicated Lines | 0 % |
| Coverage | 73.19% |
| 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) |
|
| 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) |
||
| 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 | if ($jobPoster == null || $jobPoster->manager == null) { |
|
| 294 | $manager = $request->user() ? $request->user()->manager : null; |
||
| 295 | } else { |
||
| 296 | 1 | $manager = $jobPoster->manager; |
|
| 297 | } |
||
| 298 | |||
| 299 | 1 | if (isset($jobPoster)) { |
|
| 300 | 1 | $job = $jobPoster; |
|
| 301 | 1 | $route = ['manager.jobs.update', $jobPoster]; |
|
| 302 | 1 | $jobHeading = 'manager/job_edit'; |
|
| 303 | } else { |
||
| 304 | $job = []; |
||
| 305 | $defaultQuestions = $this->populateDefaultQuestions(); |
||
| 306 | if (!empty($defaultQuestions)) { |
||
| 307 | $job['job_poster_questions'] = $defaultQuestions; |
||
| 308 | } |
||
| 309 | $route = ['manager.jobs.store']; |
||
| 310 | $jobHeading = 'manager/job_create'; |
||
| 311 | } |
||
| 312 | |||
| 313 | 1 | $skillLangs = Lang::get('common/skills'); |
|
| 314 | |||
| 315 | 1 | $softSkills = Skill::whereHas( |
|
| 316 | 1 | 'skill_type', |
|
| 317 | function ($query) : void { |
||
| 318 | 1 | $query->where('name', '=', 'soft'); |
|
| 319 | 1 | } |
|
| 320 | 1 | )->get() |
|
| 321 | 1 | ->mapWithKeys( |
|
| 322 | function ($skill) { |
||
| 323 | return [ |
||
| 324 | 1 | $skill->id => $skill->name |
|
| 325 | ]; |
||
| 326 | 1 | } |
|
| 327 | ) |
||
| 328 | 1 | ->all(); |
|
| 329 | |||
| 330 | 1 | $hardSkills = Skill::whereHas( |
|
| 331 | 1 | 'skill_type', |
|
| 332 | function ($query) : void { |
||
| 333 | 1 | $query->where('name', '=', 'hard'); |
|
| 334 | 1 | } |
|
| 335 | 1 | )->get() |
|
| 336 | 1 | ->mapWithKeys( |
|
| 337 | function ($skill) { |
||
| 338 | return [ |
||
| 339 | 1 | $skill->id => $skill->name |
|
| 340 | ]; |
||
| 341 | 1 | } |
|
| 342 | ) |
||
| 343 | 1 | ->all(); |
|
| 344 | |||
| 345 | 1 | asort($softSkills, SORT_LOCALE_STRING); |
|
| 346 | 1 | asort($hardSkills, SORT_LOCALE_STRING); |
|
| 347 | |||
| 348 | $skills = [ |
||
| 349 | 'essential' => [ |
||
| 350 | 1 | 'hard' => $hardSkills, |
|
| 351 | 1 | 'soft' => $softSkills |
|
| 352 | ], |
||
| 353 | 'asset' => [ |
||
| 354 | 1 | 'hard' => $hardSkills, |
|
| 355 | 1 | 'soft' => $softSkills |
|
| 356 | ] |
||
| 357 | ]; |
||
| 358 | |||
| 359 | 1 | $skillLevelCollection = SkillLevel::all(); |
|
| 360 | |||
| 361 | 1 | $skillLevels = array(); |
|
| 362 | |||
| 363 | 1 | $skillLevels['hard'] = $skillLevelCollection->mapWithKeys( |
|
| 364 | function ($skillLevel) use ($skillLangs) { |
||
| 365 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]]; |
|
| 366 | 1 | } |
|
| 367 | 1 | )->all(); |
|
| 368 | |||
| 369 | 1 | $skillLevels['soft'] = $skillLevelCollection->mapWithKeys( |
|
| 370 | function ($skillLevel) use ($skillLangs) { |
||
| 371 | 1 | return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]]; |
|
| 372 | 1 | } |
|
| 373 | 1 | )->all(); |
|
| 374 | |||
| 375 | 1 | return view( |
|
| 376 | 1 | 'manager/job_create', |
|
| 377 | [ |
||
| 378 | /*Localization Strings*/ |
||
| 379 | 1 | 'job_l10n' => Lang::get('manager/job_create'), |
|
| 380 | |||
| 381 | /* Data */ |
||
| 382 | 1 | 'job' => Lang::get($jobHeading), |
|
| 383 | 1 | 'manager' => $manager, |
|
| 384 | 1 | 'provinces' => Province::all(), |
|
| 385 | 1 | 'departments' => Department::all(), |
|
| 386 | 1 | 'language_requirments' => LanguageRequirement::all(), |
|
| 387 | 1 | 'security_clearances' => SecurityClearance::all(), |
|
| 388 | 1 | 'job' => $job, |
|
| 389 | 1 | 'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore |
|
| 390 | 1 | 'skills' => $skills, |
|
| 391 | 1 | 'skill_levels' => $skillLevels, |
|
| 392 | 1 | 'skill_template' => $skillLangs, |
|
| 393 | ] |
||
| 394 | ); |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Create a new resource in storage |
||
| 399 | * |
||
| 400 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
| 401 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
| 402 | * |
||
| 403 | * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index |
||
| 404 | */ |
||
| 405 | 3 | public function store(Request $request, JobPoster $jobPoster = null) |
|
| 406 | { |
||
| 407 | // Don't allow edits for published Job Posters |
||
| 408 | // Also check auth while we're at it |
||
| 409 | 3 | if (isset($jobPoster)) { |
|
| 410 | 3 | $this->authorize('update', $jobPoster); |
|
| 411 | 3 | JobPosterValidator::validateUnpublished($jobPoster); |
|
| 412 | } else { |
||
| 413 | $this->authorize('create', JobPoster::class); |
||
| 414 | } |
||
| 415 | |||
| 416 | 3 | $input = $request->input(); |
|
| 417 | |||
| 418 | 3 | $job = (isset($jobPoster) ? $jobPoster : new JobPoster()); |
|
| 419 | |||
| 420 | 3 | if ($job->manager_id == null) { |
|
| 421 | $job->manager_id = $request->user()->manager->id; |
||
| 422 | $job->save(); |
||
| 423 | } |
||
| 424 | |||
| 425 | 3 | $this->fillAndSaveJobPoster($input, $job); |
|
| 426 | |||
| 427 | 3 | $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster)); |
|
| 428 | |||
| 429 | 3 | $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster)); |
|
| 430 | |||
| 431 | 3 | $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster)); |
|
| 432 | |||
| 433 | 3 | return redirect(route('manager.jobs.show', $job->id)); |
|
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Fill Job Poster model's properties and save |
||
| 438 | * |
||
| 439 | * @param mixed[] $input Field values. |
||
| 440 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 441 | * |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | 3 | protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void |
|
| 445 | { |
||
| 446 | 3 | $closeDate = new Date($input['close_date']); |
|
| 447 | 3 | $openDate = new Date($input['open_date']); |
|
| 448 | 3 | $startDate = new Date($input['start_date']); |
|
| 449 | |||
| 450 | 3 | $jobPoster->fill( |
|
| 451 | [ |
||
| 452 | 3 | 'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id, |
|
| 453 | 3 | 'term_qty' => $input['term_qty'], |
|
| 454 | 3 | 'open_date_time' => pstDayStartToUtcTime($openDate->year, $openDate->month, $openDate->day), |
|
| 455 | 3 | 'close_date_time' => pstDayEndToUtcTime($closeDate->year, $closeDate->month, $closeDate->day), |
|
| 456 | 3 | 'start_date_time' => pstDayStartToUtcTime($startDate->year, $startDate->month, $startDate->day), |
|
| 457 | 3 | 'department_id' => $input['department'], |
|
| 458 | 3 | 'province_id' => $input['province'], |
|
| 459 | 3 | 'salary_min' => $input['salary_min'], |
|
| 460 | 3 | 'salary_max' => $input['salary_max'], |
|
| 461 | 3 | 'noc' => $input['noc'], |
|
| 462 | 3 | 'classification' => $input['classification'], |
|
| 463 | 3 | 'security_clearance_id' => $input['security_clearance'], |
|
| 464 | 3 | 'language_requirement_id' => $input['language_requirement'], |
|
| 465 | 3 | 'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false), |
|
| 466 | 'en' => [ |
||
| 467 | 3 | 'city' => $input['city'], |
|
| 468 | 3 | 'title' => $input['title']['en'], |
|
| 469 | 3 | 'impact' => $input['impact']['en'], |
|
| 470 | 3 | 'branch' => $input['branch']['en'], |
|
| 471 | 3 | 'division' => $input['division']['en'], |
|
| 472 | 3 | 'education' => $input['education']['en'], |
|
| 473 | ], |
||
| 474 | 'fr' => [ |
||
| 475 | 3 | 'city' => $input['city'], |
|
| 476 | 3 | 'title' => $input['title']['fr'], |
|
| 477 | 3 | 'impact' => $input['impact']['fr'], |
|
| 478 | 3 | 'branch' => $input['branch']['fr'], |
|
| 479 | 3 | 'division' => $input['division']['fr'], |
|
| 480 | 3 | 'education' => $input['education']['fr'], |
|
| 481 | ], |
||
| 482 | ] |
||
| 483 | ); |
||
| 484 | 3 | $jobPoster->save(); |
|
| 485 | 3 | } |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Fill Job Poster's tasks and save |
||
| 489 | * |
||
| 490 | * @param mixed[] $input Field values. |
||
| 491 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 492 | * @param boolean $replace Remove existing relationships. |
||
| 493 | * |
||
| 494 | * @return void |
||
| 495 | */ |
||
| 496 | 3 | protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void |
|
| 520 | } |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Fill Job Poster's questions and save |
||
| 525 | * |
||
| 526 | * @param mixed[] $input Field values. |
||
| 527 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 528 | * @param boolean $replace Remove existing relationships. |
||
| 529 | * |
||
| 530 | * @return void |
||
| 531 | */ |
||
| 532 | 3 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void |
|
| 533 | { |
||
| 534 | 3 | if ($replace) { |
|
| 535 | 3 | $jobPoster->job_poster_questions()->delete(); |
|
| 536 | } |
||
| 537 | |||
| 538 | 3 | if (!array_key_exists('question', $input) || !is_array($input['question'])) { |
|
| 539 | 3 | return; |
|
| 540 | } |
||
| 541 | |||
| 542 | foreach ($input['question'] as $question) { |
||
| 543 | $jobQuestion = new JobPosterQuestion(); |
||
| 544 | $jobQuestion->job_poster_id = $jobPoster->id; |
||
| 545 | $jobQuestion->fill( |
||
| 546 | [ |
||
| 547 | 'en' => [ |
||
| 548 | 'question' => $question['question']['en'], |
||
| 549 | 'description' => $question['description']['en'] |
||
| 550 | ], |
||
| 551 | 'fr' => [ |
||
| 552 | 'question' => $question['question']['fr'], |
||
| 553 | 'description' => $question['description']['fr'] |
||
| 554 | ] |
||
| 555 | ] |
||
| 556 | ); |
||
| 557 | $jobQuestion->save(); |
||
| 558 | } |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Fill Job Poster's criteria and save |
||
| 563 | * |
||
| 564 | * @param mixed[] $input Field values. |
||
| 565 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 566 | * @param boolean $replace Remove existing relationships. |
||
| 567 | * |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | 3 | protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void |
|
| 571 | { |
||
| 572 | 3 | if ($replace) { |
|
| 573 | 3 | $jobPoster->criteria()->delete(); |
|
| 574 | } |
||
| 575 | |||
| 576 | 3 | if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) { |
|
| 577 | 3 | return; |
|
| 578 | } |
||
| 579 | |||
| 580 | $criteria = $input['criteria']; |
||
| 581 | |||
| 582 | $combinedCriteria = []; |
||
| 583 | if (isset($criteria['old'])) { |
||
| 584 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']); |
||
| 585 | } |
||
| 586 | if (isset($criteria['new'])) { |
||
| 587 | $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']); |
||
| 588 | } |
||
| 589 | |||
| 590 | if (! empty($combinedCriteria)) { |
||
| 591 | foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) { |
||
| 592 | foreach ($criteriaTypeInput as $skillType => $skillTypeInput) { |
||
| 593 | foreach ($skillTypeInput as $criteriaInput) { |
||
| 594 | $criteria = new Criteria(); |
||
| 595 | $criteria->job_poster_id = $jobPoster->id; |
||
| 596 | $criteria->fill( |
||
| 597 | [ |
||
| 598 | 'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id, |
||
| 599 | 'skill_id' => $criteriaInput['skill_id'], |
||
| 600 | 'skill_level_id' => $criteriaInput['skill_level_id'], |
||
| 601 | 'en' => [ |
||
| 602 | 'description' => $criteriaInput['description']['en'], |
||
| 603 | ], |
||
| 604 | 'fr' => [ |
||
| 605 | 'description' => $criteriaInput['description']['fr'], |
||
| 606 | ], |
||
| 607 | ] |
||
| 608 | ); |
||
| 609 | $criteria->save(); |
||
| 610 | } |
||
| 611 | } |
||
| 612 | } |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get the localized default questions and add them to an array. |
||
| 618 | * |
||
| 619 | * @return mixed[]|void |
||
| 620 | */ |
||
| 621 | 2 | protected function populateDefaultQuestions() |
|
| 651 | } |
||
| 652 | } |
||
| 653 |