| Total Complexity | 40 |
| Total Lines | 461 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| 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 | $chosen_lang = $job->chosen_lang; |
||
| 80 | |||
| 81 | // Show chosen lang title if current title is empty. |
||
| 82 | if (empty($job->title)) { |
||
| 83 | $job->title = $job->getTranslation('title', $chosen_lang); |
||
| 84 | $job->trans_required = true; |
||
| 85 | } |
||
| 86 | |||
| 87 | // Always preview and edit in the chosen language. |
||
| 88 | $job->preview_link = LaravelLocalization::getLocalizedURL($chosen_lang, route('manager.jobs.show', $job)); |
||
| 89 | $job->edit_link = LaravelLocalization::getLocalizedURL($chosen_lang, route('manager.jobs.edit', $job)); |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | return view('manager/job_index', [ |
||
| 94 | // Localization Strings. |
||
| 95 | 'jobs_l10n' => Lang::get('manager/job_index'), |
||
| 96 | // Data. |
||
| 97 | 'jobs' => $jobs, |
||
| 98 | ]); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Display a listing of a hr advisor's JobPosters. |
||
| 103 | * |
||
| 104 | * @return \Illuminate\Http\Response |
||
| 105 | */ |
||
| 106 | public function hrIndex(Request $request) |
||
| 107 | { |
||
| 108 | $hrAdvisor = $request->user()->hr_advisor; |
||
| 109 | return view('hr_advisor/job_index', [ |
||
| 110 | 'title' => Lang::get('hr_advisor/job_index.title'), |
||
| 111 | 'hr_advisor_id' => $hrAdvisor->id |
||
| 112 | ]); |
||
| 113 | } |
||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * Delete a draft Job Poster. |
||
| 120 | * |
||
| 121 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
| 122 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 123 | * @return \Illuminate\Http\Response |
||
| 124 | */ |
||
| 125 | public function destroy(Request $request, JobPoster $jobPoster) |
||
| 126 | { |
||
| 127 | $jobPoster->delete(); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Display the specified job poster. |
||
| 132 | * |
||
| 133 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
| 134 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 135 | * @return \Illuminate\Http\Response |
||
| 136 | */ |
||
| 137 | public function show(Request $request, JobPoster $jobPoster) |
||
| 138 | { |
||
| 139 | $jobPoster->load([ |
||
| 140 | 'department', |
||
| 141 | 'criteria.skill.skill_type', |
||
| 142 | 'manager.team_culture', |
||
| 143 | 'manager.work_environment' |
||
| 144 | ]); |
||
| 145 | |||
| 146 | $user = Auth::user(); |
||
| 147 | |||
| 148 | // TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model. |
||
| 149 | $workplacePhotos = []; |
||
| 150 | foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) { |
||
| 151 | $workplacePhotos[] = [ |
||
| 152 | 'description' => $photoCaption->description, |
||
| 153 | 'url' => '/images/user.png' |
||
| 154 | ]; |
||
| 155 | } |
||
| 156 | |||
| 157 | // TODO: replace route('manager.show',manager.id) in templates with link using slug. |
||
| 158 | $essential_hard = $jobPoster->criteria->filter( |
||
| 159 | function ($value, $key) { |
||
| 160 | return $value->criteria_type->name == 'essential' && |
||
| 161 | $value->skill->skill_type->name == 'hard'; |
||
| 162 | } |
||
| 163 | )->sortBy('skill.name'); |
||
| 164 | $essential_soft = $jobPoster->criteria->filter( |
||
| 165 | function ($value, $key) { |
||
| 166 | return $value->criteria_type->name == 'essential' && |
||
| 167 | $value->skill->skill_type->name == 'soft'; |
||
| 168 | } |
||
| 169 | )->sortBy('skill.name'); |
||
| 170 | $asset_hard = $jobPoster->criteria->filter( |
||
| 171 | function ($value, $key) { |
||
| 172 | return $value->criteria_type->name == 'asset' && |
||
| 173 | $value->skill->skill_type->name == 'hard'; |
||
| 174 | } |
||
| 175 | )->sortBy('skill.name'); |
||
| 176 | $asset_soft = $jobPoster->criteria->filter( |
||
| 177 | function ($value, $key) { |
||
| 178 | return $value->criteria_type->name == 'asset' && |
||
| 179 | $value->skill->skill_type->name == 'soft'; |
||
| 180 | } |
||
| 181 | )->sortBy('skill.name'); |
||
| 182 | $criteria = [ |
||
| 183 | 'essential' => $essential_hard->merge($essential_soft), |
||
| 184 | 'asset' => $asset_hard->merge($asset_soft), |
||
| 185 | ]; |
||
| 186 | |||
| 187 | $jobLang = Lang::get('applicant/job_post'); |
||
| 188 | |||
| 189 | $applyButton = []; |
||
| 190 | if (WhichPortal::isManagerPortal()) { |
||
| 191 | $applyButton = [ |
||
| 192 | 'href' => route('manager.jobs.edit', $jobPoster->id), |
||
| 193 | 'title' => $jobLang['apply']['edit_link_title'], |
||
| 194 | 'text' => $jobLang['apply']['edit_link_label'], |
||
| 195 | ]; |
||
| 196 | } elseif (WhichPortal::isHrPortal()) { |
||
| 197 | if ($jobPoster->hr_advisors->contains('user_id', $user->id)) { |
||
| 198 | $applyButton = [ |
||
| 199 | 'href' => route('hr_advisor.jobs.summary', $jobPoster->id), |
||
| 200 | 'title' => null, |
||
| 201 | 'text' => Lang::get('hr_advisor/job_summary.summary_title'), |
||
| 202 | ]; |
||
| 203 | } else { |
||
| 204 | $applyButton = [ |
||
| 205 | 'href' => route('hr_advisor.jobs.index'), |
||
| 206 | 'title' => null, |
||
| 207 | 'text' => Lang::get('hr_advisor/job_index.title'), |
||
| 208 | ]; |
||
| 209 | } |
||
| 210 | } elseif (Auth::check() && $jobPoster->isOpen()) { |
||
| 211 | $application = JobApplication::where('applicant_id', Auth::user()->applicant->id) |
||
| 212 | ->where('job_poster_id', $jobPoster->id)->first(); |
||
| 213 | // If applicants job application is not draft anymore then link to application preview page. |
||
| 214 | if ($application != null && $application->application_status->name != 'draft') { |
||
| 215 | $applyButton = [ |
||
| 216 | 'href' => route('applications.show', $application->id), |
||
| 217 | 'title' => $jobLang['apply']['view_link_title'], |
||
| 218 | 'text' => $jobLang['apply']['view_link_label'], |
||
| 219 | ]; |
||
| 220 | } else { |
||
| 221 | $applyButton = [ |
||
| 222 | 'href' => route('job.application.edit.1', $jobPoster->id), |
||
| 223 | 'title' => $jobLang['apply']['apply_link_title'], |
||
| 224 | 'text' => $jobLang['apply']['apply_link_label'], |
||
| 225 | ]; |
||
| 226 | } |
||
| 227 | } elseif (Auth::guest() && $jobPoster->isOpen()) { |
||
| 228 | $applyButton = [ |
||
| 229 | 'href' => route('job.application.edit.1', $jobPoster->id), |
||
| 230 | 'title' => $jobLang['apply']['login_link_title'], |
||
| 231 | 'text' => $jobLang['apply']['login_link_label'], |
||
| 232 | ]; |
||
| 233 | } else { |
||
| 234 | $applyButton = [ |
||
| 235 | 'href' => null, |
||
| 236 | 'title' => null, |
||
| 237 | 'text' => $jobLang['apply']['job_closed_label'], |
||
| 238 | ]; |
||
| 239 | } |
||
| 240 | |||
| 241 | $jpb_release_date = strtotime('2019-08-21 16:18:17'); |
||
| 242 | $job_created_at = strtotime($jobPoster->created_at); |
||
| 243 | |||
| 244 | // If the poster is part of the Strategic Talent Response dept, use the talent stream template. |
||
| 245 | // Else, If the job poster is created after the release of the JPB. |
||
| 246 | // Then, render with updated poster template. |
||
| 247 | // Else, render with old poster template. |
||
| 248 | if ($jobPoster->isInStrategicResponseDepartment()) { |
||
| 249 | return view( |
||
| 250 | 'applicant/strategic_response_job_post', |
||
| 251 | [ |
||
| 252 | 'job_post' => $jobLang, |
||
| 253 | 'frequencies' => Lang::get('common/lookup/frequency'), |
||
| 254 | 'skill_template' => Lang::get('common/skills'), |
||
| 255 | 'job' => $jobPoster, |
||
| 256 | 'manager' => $jobPoster->manager, |
||
| 257 | 'criteria' => $criteria, |
||
| 258 | 'apply_button' => $applyButton, |
||
| 259 | ] |
||
| 260 | ); |
||
| 261 | } elseif ($job_created_at > $jpb_release_date) { |
||
| 262 | // Updated job poster (JPB). |
||
| 263 | return view( |
||
| 264 | 'applicant/jpb_job_post', |
||
| 265 | [ |
||
| 266 | 'job_post' => $jobLang, |
||
| 267 | 'frequencies' => Lang::get('common/lookup/frequency'), |
||
| 268 | 'skill_template' => Lang::get('common/skills'), |
||
| 269 | 'job' => $jobPoster, |
||
| 270 | 'manager' => $jobPoster->manager, |
||
| 271 | 'criteria' => $criteria, |
||
| 272 | 'apply_button' => $applyButton, |
||
| 273 | ] |
||
| 274 | ); |
||
| 275 | } else { |
||
| 276 | // Old job poster. |
||
| 277 | return view( |
||
| 278 | 'applicant/job_post', |
||
| 279 | [ |
||
| 280 | 'job_post' => $jobLang, |
||
| 281 | 'frequencies' => Lang::get('common/lookup/frequency'), |
||
| 282 | 'manager' => $jobPoster->manager, |
||
| 283 | 'manager_profile_photo_url' => '/images/user.png', // TODO get real photo. |
||
| 284 | 'team_culture' => $jobPoster->manager->team_culture, |
||
| 285 | 'work_environment' => $jobPoster->manager->work_environment, |
||
| 286 | 'workplace_photos' => $workplacePhotos, |
||
| 287 | 'job' => $jobPoster, |
||
| 288 | 'criteria' => $criteria, |
||
| 289 | 'apply_button' => $applyButton, |
||
| 290 | 'skill_template' => Lang::get('common/skills'), |
||
| 291 | ] |
||
| 292 | ); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Display the form for editing an existing Job Poster |
||
| 298 | * Only allows editing fields that don't appear on the react-built Job Poster Builder. |
||
| 299 | * |
||
| 300 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
| 301 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 302 | * @return \Illuminate\Http\Response |
||
| 303 | */ |
||
| 304 | public function edit(Request $request, JobPoster $jobPoster) |
||
| 305 | { |
||
| 306 | $manager = $jobPoster->manager; |
||
| 307 | |||
| 308 | $defaultQuestionManager = new JobPosterDefaultQuestions(); |
||
| 309 | $defaultQuestionManager->initializeQuestionsIfEmpty($jobPoster); |
||
| 310 | |||
| 311 | return view( |
||
| 312 | 'manager/job_create', |
||
| 313 | [ |
||
| 314 | // Localization Strings. |
||
| 315 | 'job_l10n' => Lang::get('manager/job_edit'), |
||
| 316 | // Data. |
||
| 317 | 'manager' => $manager, |
||
| 318 | 'job' => $jobPoster, |
||
| 319 | 'form_action_url' => route('admin.jobs.update', $jobPoster), |
||
| 320 | ] |
||
| 321 | ); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Create a blank job poster for the specified manager |
||
| 326 | * |
||
| 327 | * @param \App\Models\Manager $manager Incoming Manager object. |
||
| 328 | * @return \Illuminate\Http\Response Job Create view |
||
| 329 | */ |
||
| 330 | public function createAsManager(Manager $manager) |
||
| 331 | { |
||
| 332 | $jobPoster = new JobPoster(); |
||
| 333 | $jobPoster->manager_id = $manager->id; |
||
| 334 | |||
| 335 | // Save manager-specific info to the job poster - equivalent to the intro step of the JPB |
||
| 336 | $divisionEn = $manager->getTranslation('division', 'en'); |
||
| 337 | $divisionFr = $manager->getTranslation('division', 'fr'); |
||
| 338 | $jobPoster->fill([ |
||
| 339 | 'department_id' => $manager->user->department_id, |
||
| 340 | 'division' => ['en' => $divisionEn], |
||
| 341 | 'division' => ['fr' => $divisionFr], |
||
| 342 | ]); |
||
| 343 | |||
| 344 | $jobPoster->save(); |
||
| 345 | |||
| 346 | return redirect()->route('manager.jobs.edit', $jobPoster->id); |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Update a resource in storage |
||
| 351 | * NOTE: Only saves fields that are not on the react-built Job Poster Builder |
||
| 352 | * |
||
| 353 | * @param \Illuminate\Http\Request $request Incoming request object. |
||
| 354 | * @param \App\Models\JobPoster $jobPoster Optional Job Poster object. |
||
| 355 | * @return \Illuminate\Http\Response |
||
| 356 | */ |
||
| 357 | public function store(Request $request, JobPoster $jobPoster) |
||
| 358 | { |
||
| 359 | // Don't allow edits for published Job Posters |
||
| 360 | // Also check auth while we're at it. |
||
| 361 | $this->authorize('update', $jobPoster); |
||
| 362 | JobPosterValidator::validateUnpublished($jobPoster); |
||
| 363 | |||
| 364 | $input = $request->input(); |
||
| 365 | |||
| 366 | if ($jobPoster->manager_id == null) { |
||
| 367 | $jobPoster->manager_id = $request->user()->manager->id; |
||
| 368 | $jobPoster->save(); |
||
| 369 | } |
||
| 370 | |||
| 371 | if ($request->input('question')) { |
||
| 372 | $validator = Validator::make($request->input('question'), [ |
||
| 373 | '*.question.*' => 'required|string', |
||
| 374 | ], [ |
||
| 375 | 'required' => Lang::get('validation.custom.job_poster_question.required'), |
||
| 376 | 'string' => Lang::get('validation.custom.job_poster_question.string') |
||
| 377 | ]); |
||
| 378 | |||
| 379 | if ($validator->fails()) { |
||
| 380 | $request->session()->flash('errors', $validator->errors()); |
||
| 381 | return redirect(route('admin.jobs.edit', $jobPoster->id)); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | |||
| 385 | $this->fillAndSaveJobPosterQuestions($input, $jobPoster, true); |
||
| 386 | |||
| 387 | return redirect(route('manager.jobs.show', $jobPoster->id)); |
||
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Fill Job Poster's questions and save |
||
| 392 | * |
||
| 393 | * @param mixed[] $input Field values. |
||
| 394 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 395 | * @param boolean $replace Remove existing relationships. |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void |
||
| 399 | { |
||
| 400 | if ($replace) { |
||
| 401 | $jobPoster->job_poster_questions()->delete(); |
||
| 402 | } |
||
| 403 | |||
| 404 | if (!array_key_exists('question', $input) || !is_array($input['question'])) { |
||
| 405 | return; |
||
| 406 | } |
||
| 407 | |||
| 408 | foreach ($input['question'] as $question) { |
||
| 409 | $jobQuestion = new JobPosterQuestion(); |
||
| 410 | $jobQuestion->job_poster_id = $jobPoster->id; |
||
| 411 | $jobQuestion->fill( |
||
| 412 | [ |
||
| 413 | 'question' => [ |
||
| 414 | 'en' => $question['question']['en'], |
||
| 415 | 'fr' => $question['question']['fr'] |
||
| 416 | |||
| 417 | ], |
||
| 418 | 'description' => [ |
||
| 419 | 'en' => $question['description']['en'], |
||
| 420 | 'fr' => $question['description']['fr'] |
||
| 421 | ] |
||
| 422 | ] |
||
| 423 | ); |
||
| 424 | $jobPoster->save(); |
||
| 425 | $jobQuestion->save(); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Downloads a CSV file with the applicants who have applied to the job poster. |
||
| 431 | * |
||
| 432 | * @param \App\Models\JobPoster $jobPoster Job Poster object. |
||
| 433 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
| 434 | */ |
||
| 435 | protected function downloadApplicants(JobPoster $jobPoster) |
||
| 486 | } |
||
| 487 | } |
||
| 488 |