Passed
Push — feature/response-screening ( e0fe27...dc9872 )
by Chris
04:25 queued 11s
created

JobController::show()   D

Complexity

Conditions 17
Paths 42

Size

Total Lines 154
Code Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 111
c 1
b 0
f 0
dl 0
loc 154
rs 4.1733
cc 17
nc 42
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Http\Request;
8
use App\Http\Controllers\Controller;
9
use App\Models\JobApplication;
10
use Carbon\Carbon;
11
use App\Models\JobPoster;
12
use App\Models\JobPosterQuestion;
13
use App\Models\Lookup\ApplicationStatus;
14
use App\Models\Lookup\CitizenshipDeclaration;
15
use App\Models\Lookup\JobPosterStatus;
16
use App\Models\Lookup\VeteranStatus;
17
use App\Models\Manager;
18
use App\Services\JobPosterDefaultQuestions;
19
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
20
use App\Services\Validation\JobPosterValidator;
21
use Facades\App\Services\WhichPortal;
22
use Illuminate\Support\Facades\Response;
23
use Illuminate\Support\Facades\Validator;
24
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');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $emergency_response = /** @scrutinizer ignore-call */ config('seasonal.is_covid_emergency');
Loading history...
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', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        return /** @scrutinizer ignore-call */ view('applicant/job_index', [
Loading history...
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));
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
            $job->preview_link = LaravelLocalization::getLocalizedURL($chosen_lang, /** @scrutinizer ignore-call */ route('manager.jobs.show', $job));
Loading history...
89
            $job->edit_link = LaravelLocalization::getLocalizedURL($chosen_lang, route('manager.jobs.edit', $job));
90
        }
91
92
93
        return view('manager/job_index', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        return /** @scrutinizer ignore-call */ view('manager/job_index', [
Loading history...
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', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
        return /** @scrutinizer ignore-call */ view('hr_advisor/job_index', [
Loading history...
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),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

192
                'href' => /** @scrutinizer ignore-call */ route('manager.jobs.edit', $jobPoster->id),
Loading history...
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(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

249
            return /** @scrutinizer ignore-call */ view(
Loading history...
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(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

311
        return /** @scrutinizer ignore-call */ view(
Loading history...
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),
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

319
                'form_action_url' => /** @scrutinizer ignore-call */ route('admin.jobs.update', $jobPoster),
Loading history...
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);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

346
        return /** @scrutinizer ignore-call */ redirect()->route('manager.jobs.edit', $jobPoster->id);
Loading history...
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));
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

381
                return /** @scrutinizer ignore-call */ redirect(route('admin.jobs.edit', $jobPoster->id));
Loading history...
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

381
                return redirect(/** @scrutinizer ignore-call */ route('admin.jobs.edit', $jobPoster->id));
Loading history...
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)
436
    {
437
        $tables = [];
438
        // The first row in the array represents the names of the columns in the spreadsheet.
439
        $tables[0] = ['Status', 'Applicant Name', 'Email', 'Language'];
440
441
        $application_status_id = ApplicationStatus::where('name', 'submitted')->first()->id;
442
        $applications = JobApplication::where('job_poster_id', $jobPoster->id)
443
            ->where('application_status_id', $application_status_id)
444
            ->get();
445
446
        $index = 1;
447
        foreach ($applications as $application) {
448
            $status = '';
449
            $username = $application->user_name;
450
            $user_email = $application->user_email;
451
            $language = strtoupper($application->preferred_language->name);
452
            // If the applicants veteran status name is NOT 'none' then set status to veteran.
453
            $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
454
            if ($application->veteran_status_id != $non_veteran) {
455
                $status = 'Veteran';
456
            } else {
457
                // Check if the applicant is a canadian citizen.
458
                $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
459
                if ($application->citizenship_declaration->id == $canadian_citizen) {
460
                    $status = 'Citizen';
461
                } else {
462
                    $status = 'Non-citizen';
463
                }
464
            }
465
            $tables[$index] = [$status, $username, $user_email, $language];
466
            $index++;
467
        }
468
469
        $filename = $jobPoster->id . '-' . 'applicants-data.csv';
470
471
        // Open file.
472
        $file = fopen($filename, 'w');
473
        // Iterate through tables and add each line to csv file.
474
        foreach ($tables as $line) {
475
            fputcsv($file, $line);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

475
            fputcsv(/** @scrutinizer ignore-type */ $file, $line);
Loading history...
476
        }
477
        // Close open file.
478
        fclose($file);
0 ignored issues
show
Bug introduced by
It seems like $file can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

478
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
479
480
        $headers = [
481
            'Content-Type' => 'text/csv',
482
            'Content-Disposition' => 'attachment; filename=' . $filename,
483
        ];
484
485
        return Response::download($filename, $filename, $headers);
486
    }
487
}
488