Passed
Push — task/job-poster-structured-dat... ( 731257 )
by
unknown
07:26
created

JobController::show()   D

Complexity

Conditions 16
Paths 168

Size

Total Lines 170
Code Lines 121

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 121
c 1
b 0
f 0
dl 0
loc 170
rs 4
cc 16
nc 168
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 App\Http\Controllers\Controller;
6
use App\Models\JobApplication;
7
use App\Models\JobPoster;
8
use App\Models\JobPosterQuestion;
9
use App\Models\Lookup\ApplicationStatus;
10
use App\Models\Lookup\CitizenshipDeclaration;
11
use App\Models\Lookup\JobPosterStatus;
12
use App\Models\Lookup\VeteranStatus;
13
use App\Models\Manager;
14
use App\Services\JobPosterDefaultQuestions;
15
use App\Services\Validation\JobPosterValidator;
16
use Carbon\Carbon;
17
use Facades\App\Services\WhichPortal;
18
use Illuminate\Http\Request;
19
use Illuminate\Support\Facades\Auth;
20
use Illuminate\Support\Facades\Lang;
21
use Illuminate\Support\Facades\Response;
22
use Illuminate\Support\Facades\Validator;
23
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
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
            // 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', [
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

89
        return /** @scrutinizer ignore-call */ view('manager/job_index', [
Loading history...
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', [
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

106
        return /** @scrutinizer ignore-call */ view('hr_advisor/job_index', [
Loading history...
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 void
118
     */
119
    public function destroy(Request $request, JobPoster $jobPoster): void
120
    {
121
        $jobPoster->delete();
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
        $skillsArray = [];
172
        foreach ($essential as $criterion_type => $criterion) {
173
            $skillsArray[] = [
174
                'skill' => $criterion['skill']['name'],
175
                'level' => $skillsLang['skill_levels'][$criterion
176
                ->skill->skill_type->name][$criterion->skill_level->name],
177
            ];
178
        }
179
        foreach ($asset as $criterion_type => $criterion) {
180
            $skillsArray[] = [
181
                'skill' => $criterion['skill']['name'],
182
                'level' => $skillsLang['skill_levels'][$criterion
183
                ->skill->skill_type->name][$criterion->skill_level->name],
184
            ];
185
        }
186
187
        $applyButton = [];
188
        if (WhichPortal::isManagerPortal()) {
189
            $applyButton = [
190
                '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

190
                'href' => /** @scrutinizer ignore-call */ route('manager.jobs.edit', $jobPoster->id),
Loading history...
191
                'title' => $jobLang['apply']['edit_link_title'],
192
                'text' => $jobLang['apply']['edit_link_label'],
193
            ];
194
        } elseif (WhichPortal::isHrPortal()) {
195
            if ($jobPoster->hr_advisors->contains('user_id', $user->id)) {
196
                $applyButton = [
197
                    'href' => route('hr_advisor.jobs.summary', $jobPoster->id),
198
                    'title' => null,
199
                    'text' => Lang::get('hr_advisor/job_summary.summary_title'),
200
                ];
201
            } else {
202
                $applyButton = [
203
                    'href' => route('hr_advisor.jobs.index'),
204
                    'title' => null,
205
                    'text' => Lang::get('hr_advisor/job_index.title'),
206
                ];
207
            }
208
        } elseif (Auth::check() && $jobPoster->isOpen()) {
209
            $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
210
                ->where('job_poster_id', $jobPoster->id)->first();
211
            // If applicants job application is not draft anymore then link to application preview page.
212
            if ($application != null && $application->application_status->name != 'draft') {
213
                $applyButton = [
214
                    'href' => route('applications.show', $application->id),
215
                    'title' => $jobLang['apply']['view_link_title'],
216
                    'text' => $jobLang['apply']['view_link_label'],
217
                ];
218
            } else {
219
                $applyButton = [
220
                    'href' => route('job.application.edit.1', $jobPoster->id),
221
                    'title' => $jobLang['apply']['apply_link_title'],
222
                    'text' => $jobLang['apply']['apply_link_label'],
223
                ];
224
            }
225
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
226
            $applyButton = [
227
                'href' => route('job.application.edit.1', $jobPoster->id),
228
                'title' => $jobLang['apply']['login_link_title'],
229
                'text' => $jobLang['apply']['login_link_label'],
230
            ];
231
        } else {
232
            $applyButton = [
233
                'href' => null,
234
                'title' => null,
235
                'text' => $jobLang['apply']['job_closed_label'],
236
            ];
237
        }
238
239
        $jpb_release_date = strtotime('2019-08-21 16:18:17');
240
        $job_created_at = strtotime($jobPoster->created_at);
241
242
        $custom_breadcrumbs = [
243
            'home' => route('home'),
244
            'jobs' => route(WhichPortal::prefixRoute('jobs.index')),
245
            $jobPoster->title ?: 'job-title-missing' => route(WhichPortal::prefixRoute('jobs.summary'), $jobPoster),
246
            'preview' => '',
247
        ];
248
249
        // If the poster is part of the Strategic Talent Response dept, use the talent stream template.
250
        // Else, If the job poster is created after the release of the JPB.
251
        // Then, render with updated poster template.
252
        // Else, render with old poster template.
253
        if ($jobPoster->isInStrategicResponseDepartment()) {
254
            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

254
            return /** @scrutinizer ignore-call */ view(
Loading history...
255
                'applicant/strategic_response_job_post',
256
                [
257
                    'job_post' => $jobLang,
258
                    'frequencies' => Lang::get('common/lookup/frequency'),
259
                    'skill_template' => $skillsLang,
260
                    'job' => $jobPoster,
261
                    'manager' => $jobPoster->manager,
262
                    'criteria' => $criteria,
263
                    'apply_button' => $applyButton,
264
                    'custom_breadcrumbs' => $custom_breadcrumbs,
265
                ]
266
            );
267
        } elseif ($job_created_at > $jpb_release_date) {
268
            // Updated job poster (JPB).
269
            return view(
270
                'applicant/jpb_job_post',
271
                [
272
                    'job_post' => $jobLang,
273
                    'frequencies' => Lang::get('common/lookup/frequency'),
274
                    'skill_template' => $skillsLang,
275
                    'job' => $jobPoster,
276
                    'manager' => $jobPoster->manager,
277
                    'criteria' => $criteria,
278
                    'apply_button' => $applyButton,
279
                    'custom_breadcrumbs' => $custom_breadcrumbs,
280
                    'skills' => $skillsArray,
281
                    'goc' => Lang::get('common/goc'),
282
                ]
283
            );
284
        } else {
285
            // Old job poster.
286
            return view(
287
                'applicant/job_post',
288
                [
289
                    'job_post' => $jobLang,
290
                    'frequencies' => Lang::get('common/lookup/frequency'),
291
                    'manager' => $jobPoster->manager,
292
                    'manager_profile_photo_url' => '/images/user.png', // TODO get real photo.
293
                    'team_culture' => $jobPoster->manager->team_culture,
294
                    'work_environment' => $jobPoster->manager->work_environment,
295
                    'workplace_photos' => $workplacePhotos,
296
                    'job' => $jobPoster,
297
                    'criteria' => $criteria,
298
                    'apply_button' => $applyButton,
299
                    'skill_template' => Lang::get('common/skills'),
300
                    'custom_breadcrumbs' => $custom_breadcrumbs,
301
                ]
302
            );
303
        }
304
    }
305
306
    /**
307
     * Display the form for editing an existing Job Poster
308
     * Only allows editing fields that don't appear on the react-built Job Poster Builder.
309
     *
310
     * @param  \Illuminate\Http\Request $request   Incoming request object.
311
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
312
     * @return \Illuminate\Http\Response
313
     */
314
    public function edit(Request $request, JobPoster $jobPoster)
315
    {
316
        $manager = $jobPoster->manager;
317
318
        $defaultQuestionManager = new JobPosterDefaultQuestions();
319
        $defaultQuestionManager->initializeQuestionsIfEmpty($jobPoster);
320
321
        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

321
        return /** @scrutinizer ignore-call */ view(
Loading history...
322
            'manager/job_create',
323
            [
324
                // Localization Strings.
325
                'job_l10n' => Lang::get('manager/job_edit'),
326
                // Data.
327
                'manager' => $manager,
328
                'job' => $jobPoster,
329
                '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

329
                'form_action_url' => /** @scrutinizer ignore-call */ route('admin.jobs.update', $jobPoster),
Loading history...
330
            ]
331
        );
332
    }
333
334
    /**
335
     * Create a blank job poster for the specified manager
336
     *
337
     * @param  \App\Models\Manager $manager Incoming Manager object.
338
     * @return \Illuminate\Http\Response Job Create view
339
     */
340
    public function createAsManager(Manager $manager)
341
    {
342
        $jobPoster = new JobPoster();
343
        $jobPoster->manager_id = $manager->id;
344
345
        // Save manager-specific info to the job poster - equivalent to the intro step of the JPB.
346
        $divisionEn = $manager->getTranslation('division', 'en');
347
        $divisionFr = $manager->getTranslation('division', 'fr');
348
        $jobPoster->fill([
349
            'department_id' => $manager->user->department_id,
350
            'division' => ['en' => $divisionEn],
351
            'division' => ['fr' => $divisionFr],
352
        ]);
353
354
        $jobPoster->save();
355
356
        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

356
        return /** @scrutinizer ignore-call */ redirect()->route('manager.jobs.edit', $jobPoster->id);
Loading history...
357
    }
358
359
    /**
360
     * Update a resource in storage
361
     * NOTE: Only saves fields that are not on the react-built Job Poster Builder
362
     *
363
     * @param  \Illuminate\Http\Request $request   Incoming request object.
364
     * @param  \App\Models\JobPoster    $jobPoster Optional Job Poster object.
365
     * @return \Illuminate\Http\Response
366
     */
367
    public function store(Request $request, JobPoster $jobPoster)
368
    {
369
        // Don't allow edits for published Job Posters
370
        // Also check auth while we're at it.
371
        $this->authorize('update', $jobPoster);
372
        JobPosterValidator::validateUnpublished($jobPoster);
373
374
        $input = $request->input();
375
376
        if ($jobPoster->manager_id == null) {
377
            $jobPoster->manager_id = $request->user()->manager->id;
378
            $jobPoster->save();
379
        }
380
381
        if ($request->input('question')) {
382
            $validator = Validator::make($request->input('question'), [
383
                '*.question.*' => 'required|string',
384
            ], [
385
                'required' => Lang::get('validation.custom.job_poster_question.required'),
386
                'string' => Lang::get('validation.custom.job_poster_question.string')
387
            ]);
388
389
            if ($validator->fails()) {
390
                $request->session()->flash('errors', $validator->errors());
391
                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

391
                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

391
                return redirect(/** @scrutinizer ignore-call */ route('admin.jobs.edit', $jobPoster->id));
Loading history...
392
            }
393
        }
394
395
        $this->fillAndSaveJobPosterQuestions($input, $jobPoster, true);
396
397
        return redirect(route('manager.jobs.preview', $jobPoster->id));
398
    }
399
400
    /**
401
     * Fill Job Poster's questions and save
402
     *
403
     * @param  mixed[]               $input     Field values.
404
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
405
     * @param  boolean               $replace   Remove existing relationships.
406
     * @return void
407
     */
408
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void
409
    {
410
        if ($replace) {
411
            $jobPoster->job_poster_questions()->delete();
412
        }
413
414
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
415
            return;
416
        }
417
418
        foreach ($input['question'] as $question) {
419
            $jobQuestion = new JobPosterQuestion();
420
            $jobQuestion->job_poster_id = $jobPoster->id;
421
            $jobQuestion->fill(
422
                [
423
                    'question' => [
424
                        'en' => $question['question']['en'],
425
                        'fr' => $question['question']['fr']
426
427
                    ],
428
                    'description' => [
429
                        'en' => $question['description']['en'],
430
                        'fr' => $question['description']['fr']
431
                    ]
432
                ]
433
            );
434
            $jobPoster->save();
435
            $jobQuestion->save();
436
        }
437
    }
438
439
    /**
440
     * Downloads a CSV file with the applicants who have applied to the job poster.
441
     *
442
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
443
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
444
     */
445
    protected function downloadApplicants(JobPoster $jobPoster)
446
    {
447
        $tables = [];
448
        // The first row in the array represents the names of the columns in the spreadsheet.
449
        $tables[0] = ['Status', 'Applicant Name', 'Email', 'Language'];
450
451
        $application_status_id = ApplicationStatus::where('name', 'submitted')->first()->id;
452
        $applications = JobApplication::where('job_poster_id', $jobPoster->id)
453
            ->where('application_status_id', $application_status_id)
454
            ->get();
455
456
        $index = 1;
457
        foreach ($applications as $application) {
458
            $status = '';
459
            $username = $application->user_name;
460
            $user_email = $application->user_email;
461
            $language = strtoupper($application->preferred_language->name);
462
            // If the applicants veteran status name is NOT 'none' then set status to veteran.
463
            $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
464
            if ($application->veteran_status_id != $non_veteran) {
465
                $status = 'Veteran';
466
            } else {
467
                // Check if the applicant is a canadian citizen.
468
                $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
469
                if ($application->citizenship_declaration->id == $canadian_citizen) {
470
                    $status = 'Citizen';
471
                } else {
472
                    $status = 'Non-citizen';
473
                }
474
            }
475
            $tables[$index] = [$status, $username, $user_email, $language];
476
            $index++;
477
        }
478
479
        $filename = $jobPoster->id . '-' . 'applicants-data.csv';
480
481
        // Open file.
482
        $file = fopen($filename, 'w');
483
        // Iterate through tables and add each line to csv file.
484
        foreach ($tables as $line) {
485
            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

485
            fputcsv(/** @scrutinizer ignore-type */ $file, $line);
Loading history...
486
        }
487
        // Close open file.
488
        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

488
        fclose(/** @scrutinizer ignore-type */ $file);
Loading history...
489
490
        $headers = [
491
            'Content-Type' => 'text/csv',
492
            'Content-Disposition' => 'attachment; filename=' . $filename,
493
        ];
494
495
        return Response::download($filename, $filename, $headers);
496
    }
497
}
498