| Total Complexity | 51 | 
| Total Lines | 589 | 
| Duplicated Lines | 0 % | 
| Changes | 6 | ||
| Bugs | 0 | Features | 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 | ||
| 25 | class JobController extends Controller | ||
| 26 | { | ||
| 27 | /** | ||
| 28 | * Display a listing of JobPosters. | ||
| 29 | * | ||
| 30 | * @return \Illuminate\Http\Response | ||
| 31 | */ | ||
| 32 | public function index() | ||
| 33 |     { | ||
| 34 | // If true, show the Paused due to COVID-19 message. | ||
| 35 |         $emergency_response = config('seasonal.is_covid_emergency'); | ||
|  | |||
| 36 | |||
| 37 | // Find published jobs that are currently open for applications. | ||
| 38 | // Eager load required relationships: Department, Province, JobTerm. | ||
| 39 | // Eager load the count of submitted applications, to prevent the relationship | ||
| 40 | // from being actually loaded and firing off events. | ||
| 41 |         $jobs = JobPoster::where('internal_only', false) | ||
| 42 |             ->where('department_id', '!=', config('app.strategic_response_department_id')) | ||
| 43 |             ->where('job_poster_status_id', JobPosterStatus::where('key', 'live')->first()->id) | ||
| 44 | ->with([ | ||
| 45 | 'department', | ||
| 46 | 'province', | ||
| 47 | 'job_term', | ||
| 48 | ]) | ||
| 49 | ->withCount([ | ||
| 50 | 'submitted_applications', | ||
| 51 | ]) | ||
| 52 | ->get(); | ||
| 53 | |||
| 54 | $null_alert = $emergency_response | ||
| 55 |             ? Lang::get('applicant/job_index.index.covid_null_alert') | ||
| 56 |             : Lang::get('applicant/job_index.index.null_alert'); | ||
| 57 |         return view('applicant/job_index', [ | ||
| 58 |             'job_index' => Lang::get('applicant/job_index'), | ||
| 59 | 'null_alert' => $null_alert, | ||
| 60 | 'jobs' => $jobs | ||
| 61 | ]); | ||
| 62 | } | ||
| 63 | |||
| 64 | /** | ||
| 65 | * Display a listing of a manager's JobPosters. | ||
| 66 | * | ||
| 67 | * @return \Illuminate\Http\Response | ||
| 68 | */ | ||
| 69 | public function managerIndex() | ||
| 70 |     { | ||
| 71 | $manager = Auth::user()->manager; | ||
| 72 | |||
| 73 |         $jobs = JobPoster::where('manager_id', $manager->id) | ||
| 74 |             ->with('classification') | ||
| 75 |             ->withCount('submitted_applications') | ||
| 76 | ->get(); | ||
| 77 | |||
| 78 |         foreach ($jobs as &$job) { | ||
| 79 | // If the chosen language is null then set to english. | ||
| 80 | $chosen_lang = $job->chosen_lang ?: 'en'; | ||
| 81 | |||
| 82 | // Show chosen lang title if current title is empty. | ||
| 83 |             if (empty($job->title)) { | ||
| 84 |                 $job->title = $job->getTranslation('title', $chosen_lang); | ||
| 85 | $job->trans_required = true; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 |         return view('manager/job_index', [ | ||
| 90 | // Localization Strings. | ||
| 91 |             'jobs_l10n' => Lang::get('manager/job_index'), | ||
| 92 | // Data. | ||
| 93 | 'jobs' => $jobs, | ||
| 94 | ]); | ||
| 95 | } | ||
| 96 | |||
| 97 | /** | ||
| 98 | * Display a listing of a hr advisor's JobPosters. | ||
| 99 | * | ||
| 100 | * @param \Illuminate\Http\Request $request Incoming request object. | ||
| 101 | * @return \Illuminate\Http\Response | ||
| 102 | */ | ||
| 103 | public function hrIndex(Request $request) | ||
| 104 |     { | ||
| 105 | $hrAdvisor = $request->user()->hr_advisor; | ||
| 106 |         return view('hr_advisor/job_index', [ | ||
| 107 |             'jobs_l10n' => Lang::get('hr_advisor/job_index'), | ||
| 108 | 'hr_advisor_id' => $hrAdvisor->id | ||
| 109 | ]); | ||
| 110 | } | ||
| 111 | |||
| 112 | /** | ||
| 113 | * Delete a draft Job Poster. | ||
| 114 | * | ||
| 115 | * @param \Illuminate\Http\Request $request Incoming request object. | ||
| 116 | * @param \App\Models\JobPoster $jobPoster Job Poster object. | ||
| 117 | * @return \Illuminate\Http\Response | ||
| 118 | */ | ||
| 119 | public function destroy(Request $request, JobPoster $jobPoster) | ||
| 122 | } | ||
| 123 | |||
| 124 | /** | ||
| 125 | * Display the specified job poster. | ||
| 126 | * | ||
| 127 | * @param \Illuminate\Http\Request $request Incoming request object. | ||
| 128 | * @param \App\Models\JobPoster $jobPoster Job Poster object. | ||
| 129 | * @return \Illuminate\Http\Response | ||
| 130 | */ | ||
| 131 | public function show(Request $request, JobPoster $jobPoster) | ||
| 132 |     { | ||
| 133 | $jobPoster->load([ | ||
| 134 | 'department', | ||
| 135 | 'criteria.skill.skill_type', | ||
| 136 | 'manager.team_culture', | ||
| 137 | 'manager.work_environment' | ||
| 138 | ]); | ||
| 139 | |||
| 140 | $user = Auth::user(); | ||
| 141 | |||
| 142 | // TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model. | ||
| 143 | $workplacePhotos = []; | ||
| 144 |         foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) { | ||
| 145 | $workplacePhotos[] = [ | ||
| 146 | 'description' => $photoCaption->description, | ||
| 147 | 'url' => '/images/user.png' | ||
| 148 | ]; | ||
| 149 | } | ||
| 150 | |||
| 151 |         // TODO: replace route('manager.show',manager.id) in templates with link using slug. | ||
| 152 | $essential = $jobPoster->criteria->filter( | ||
| 153 |             function ($value, $key) { | ||
| 154 | return $value->criteria_type->name == 'essential'; | ||
| 155 | } | ||
| 156 |         )->sortBy('id'); | ||
| 157 | $asset = $jobPoster->criteria->filter( | ||
| 158 |             function ($value, $key) { | ||
| 159 | return $value->criteria_type->name == 'asset'; | ||
| 160 | } | ||
| 161 |         )->sortBy('id'); | ||
| 162 | $criteria = [ | ||
| 163 | 'essential' => $essential, | ||
| 164 | 'asset' => $asset, | ||
| 165 | ]; | ||
| 166 | |||
| 167 |         $jobLang = Lang::get('applicant/job_post'); | ||
| 168 | |||
| 169 |         $skillsLang = Lang::get('common/skills'); | ||
| 170 | |||
| 171 | $applyButton = []; | ||
| 172 |         if (WhichPortal::isManagerPortal()) { | ||
| 173 | $applyButton = [ | ||
| 174 |                 'href' => route('manager.jobs.edit', $jobPoster->id), | ||
| 175 | 'title' => $jobLang['apply']['edit_link_title'], | ||
| 176 | 'text' => $jobLang['apply']['edit_link_label'], | ||
| 177 | ]; | ||
| 178 |         } elseif (WhichPortal::isHrPortal()) { | ||
| 179 |             if ($jobPoster->hr_advisors->contains('user_id', $user->id)) { | ||
| 180 | $applyButton = [ | ||
| 181 |                     'href' => route('hr_advisor.jobs.summary', $jobPoster->id), | ||
| 182 | 'title' => null, | ||
| 183 |                     'text' => Lang::get('hr_advisor/job_summary.summary_title'), | ||
| 184 | ]; | ||
| 185 |             } else { | ||
| 186 | $applyButton = [ | ||
| 187 |                     'href' => route('hr_advisor.jobs.index'), | ||
| 188 | 'title' => null, | ||
| 189 |                     'text' => Lang::get('hr_advisor/job_index.title'), | ||
| 190 | ]; | ||
| 191 | } | ||
| 192 |         } elseif (Auth::check() && $jobPoster->isOpen()) { | ||
| 193 |             $application = JobApplication::where('applicant_id', Auth::user()->applicant->id) | ||
| 194 |                 ->where('job_poster_id', $jobPoster->id)->first(); | ||
| 195 | // If applicants job application is not draft anymore then link to application preview page. | ||
| 196 |             if ($application != null && $application->application_status->name != 'draft') { | ||
| 197 | $applyButton = [ | ||
| 198 |                     'href' => route('applications.show', $application->id), | ||
| 199 | 'title' => $jobLang['apply']['view_link_title'], | ||
| 200 | 'text' => $jobLang['apply']['view_link_label'], | ||
| 201 | ]; | ||
| 202 |             } else { | ||
| 203 | $applyButton = [ | ||
| 204 |                     'href' => route('job.application.edit.1', $jobPoster->id), | ||
| 205 | 'title' => $jobLang['apply']['apply_link_title'], | ||
| 206 | 'text' => $jobLang['apply']['apply_link_label'], | ||
| 207 | ]; | ||
| 208 | } | ||
| 209 |         } elseif (Auth::guest() && $jobPoster->isOpen()) { | ||
| 210 | $applyButton = [ | ||
| 211 |                 'href' => route('job.application.edit.1', $jobPoster->id), | ||
| 212 | 'title' => $jobLang['apply']['login_link_title'], | ||
| 213 | 'text' => $jobLang['apply']['login_link_label'], | ||
| 214 | ]; | ||
| 215 |         } else { | ||
| 216 | $applyButton = [ | ||
| 217 | 'href' => null, | ||
| 218 | 'title' => null, | ||
| 219 | 'text' => $jobLang['apply']['job_closed_label'], | ||
| 220 | ]; | ||
| 221 | } | ||
| 222 | |||
| 223 |         $jpb_release_date = strtotime('2019-08-21 16:18:17'); | ||
| 224 | $job_created_at = strtotime($jobPoster->created_at); | ||
| 225 | |||
| 226 | $custom_breadcrumbs = [ | ||
| 227 |             'home' => route('home'), | ||
| 228 |             'jobs' => route(WhichPortal::prefixRoute('jobs.index')), | ||
| 229 |             $jobPoster->title ?: 'job-title-missing' => route(WhichPortal::prefixRoute('jobs.summary'), $jobPoster), | ||
| 230 | 'preview' => '', | ||
| 231 | ]; | ||
| 232 | |||
| 233 | // If the poster is part of the Strategic Talent Response dept, use the talent stream template. | ||
| 234 | // Else, If the job poster is created after the release of the JPB. | ||
| 235 | // Then, render with updated poster template. | ||
| 236 | // Else, render with old poster template. | ||
| 237 |         if ($jobPoster->isInStrategicResponseDepartment()) { | ||
| 238 | return view( | ||
| 239 | 'applicant/strategic_response_job_post', | ||
| 240 | [ | ||
| 241 | 'job_post' => $jobLang, | ||
| 242 |                     'frequencies' => Lang::get('common/lookup/frequency'), | ||
| 243 | 'skill_template' => $skillsLang, | ||
| 244 | 'job' => $jobPoster, | ||
| 245 | 'manager' => $jobPoster->manager, | ||
| 246 | 'criteria' => $criteria, | ||
| 247 | 'apply_button' => $applyButton, | ||
| 248 | 'custom_breadcrumbs' => $custom_breadcrumbs, | ||
| 249 | ] | ||
| 250 | ); | ||
| 251 |         } elseif ($job_created_at > $jpb_release_date) { | ||
| 252 | // Updated job poster (JPB). | ||
| 253 | return view( | ||
| 254 | 'applicant/jpb_job_post', | ||
| 255 | [ | ||
| 256 | 'job_post' => $jobLang, | ||
| 257 |                     'frequencies' => Lang::get('common/lookup/frequency'), | ||
| 258 | 'skill_template' => $skillsLang, | ||
| 259 | 'job' => $jobPoster, | ||
| 260 | 'manager' => $jobPoster->manager, | ||
| 261 | 'criteria' => $criteria, | ||
| 262 | 'apply_button' => $applyButton, | ||
| 263 | 'custom_breadcrumbs' => $custom_breadcrumbs, | ||
| 264 | 'structured_data' => $this->buildStructuredData($jobPoster), | ||
| 265 | ] | ||
| 266 | ); | ||
| 267 |         } else { | ||
| 268 | // Old job poster. | ||
| 269 | return view( | ||
| 270 | 'applicant/job_post', | ||
| 271 | [ | ||
| 272 | 'job_post' => $jobLang, | ||
| 273 |                     'frequencies' => Lang::get('common/lookup/frequency'), | ||
| 274 | 'manager' => $jobPoster->manager, | ||
| 275 | 'manager_profile_photo_url' => '/images/user.png', // TODO get real photo. | ||
| 276 | 'team_culture' => $jobPoster->manager->team_culture, | ||
| 277 | 'work_environment' => $jobPoster->manager->work_environment, | ||
| 278 | 'workplace_photos' => $workplacePhotos, | ||
| 279 | 'job' => $jobPoster, | ||
| 280 | 'criteria' => $criteria, | ||
| 281 | 'apply_button' => $applyButton, | ||
| 282 |                     'skill_template' => Lang::get('common/skills'), | ||
| 283 | 'custom_breadcrumbs' => $custom_breadcrumbs, | ||
| 284 | ] | ||
| 285 | ); | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | /** | ||
| 290 | * Display the form for editing an existing Job Poster | ||
| 291 | * Only allows editing fields that don't appear on the react-built Job Poster Builder. | ||
| 292 | * | ||
| 293 | * @param \Illuminate\Http\Request $request Incoming request object. | ||
| 294 | * @param \App\Models\JobPoster $jobPoster Job Poster object. | ||
| 295 | * @return \Illuminate\Http\Response | ||
| 296 | */ | ||
| 297 | public function edit(Request $request, JobPoster $jobPoster) | ||
| 313 | ] | ||
| 314 | ); | ||
| 315 | } | ||
| 316 | |||
| 317 | /** | ||
| 318 | * Create a blank job poster for the specified manager | ||
| 319 | * | ||
| 320 | * @param \App\Models\Manager $manager Incoming Manager object. | ||
| 321 | * @return \Illuminate\Http\Response Job Create view | ||
| 322 | */ | ||
| 323 | public function createAsManager(Manager $manager) | ||
| 340 | } | ||
| 341 | |||
| 342 | /** | ||
| 343 | * Update a resource in storage | ||
| 344 | * NOTE: Only saves fields that are not on the react-built Job Poster Builder | ||
| 345 | * | ||
| 346 | * @param \Illuminate\Http\Request $request Incoming request object. | ||
| 347 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. | ||
| 348 | * @return \Illuminate\Http\Response | ||
| 349 | */ | ||
| 350 | public function store(Request $request, JobPoster $jobPoster) | ||
| 351 |     { | ||
| 352 | // Don't allow edits for published Job Posters | ||
| 353 | // Also check auth while we're at it. | ||
| 354 |         $this->authorize('update', $jobPoster); | ||
| 355 | JobPosterValidator::validateUnpublished($jobPoster); | ||
| 356 | |||
| 357 | $input = $request->input(); | ||
| 358 | |||
| 359 |         if ($jobPoster->manager_id == null) { | ||
| 360 | $jobPoster->manager_id = $request->user()->manager->id; | ||
| 361 | $jobPoster->save(); | ||
| 362 | } | ||
| 363 | |||
| 364 |         if ($request->input('question')) { | ||
| 365 |             $validator = Validator::make($request->input('question'), [ | ||
| 366 | '*.question.*' => 'required|string', | ||
| 367 | ], [ | ||
| 368 |                 'required' => Lang::get('validation.custom.job_poster_question.required'), | ||
| 369 |                 'string' => Lang::get('validation.custom.job_poster_question.string') | ||
| 370 | ]); | ||
| 371 | |||
| 372 |             if ($validator->fails()) { | ||
| 373 |                 $request->session()->flash('errors', $validator->errors()); | ||
| 374 |                 return redirect(route('admin.jobs.edit', $jobPoster->id)); | ||
| 375 | } | ||
| 376 | } | ||
| 377 | |||
| 378 | $this->fillAndSaveJobPosterQuestions($input, $jobPoster, true); | ||
| 379 | |||
| 380 |         return redirect(route('manager.jobs.preview', $jobPoster->id)); | ||
| 381 | } | ||
| 382 | |||
| 383 | /** | ||
| 384 | * Fill Job Poster's questions and save | ||
| 385 | * | ||
| 386 | * @param mixed[] $input Field values. | ||
| 387 | * @param \App\Models\JobPoster $jobPoster Job Poster object. | ||
| 388 | * @param boolean $replace Remove existing relationships. | ||
| 389 | * @return void | ||
| 390 | */ | ||
| 391 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void | ||
| 419 | } | ||
| 420 | } | ||
| 421 | |||
| 422 | /** | ||
| 423 | * Downloads a CSV file with the applicants who have applied to the job poster. | ||
| 424 | * | ||
| 425 | * @param \App\Models\JobPoster $jobPoster Job Poster object. | ||
| 426 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse | ||
| 427 | */ | ||
| 428 | protected function downloadApplicants(JobPoster $jobPoster) | ||
| 479 | } | ||
| 480 | |||
| 481 | /** | ||
| 482 | * Build Job Poster's structured data | ||
| 483 | * | ||
| 484 | * @param \App\Models\JobPoster $jobPoster Job Poster object. | ||
| 485 | * @return array | ||
| 486 | */ | ||
| 487 | protected function buildStructuredData(JobPoster $jobPoster) | ||
| 614 | } | ||
| 615 | } | ||
| 616 |