Passed
Push — feature/ie-alert ( c39468...dc7a98 )
by
unknown
07:35
created

JobController   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 633
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 57
eloc 343
c 0
b 0
f 0
dl 0
loc 633
rs 5.04

12 Methods

Rating   Name   Duplication   Size   Complexity  
A destroy() 0 3 1
A index() 0 29 2
A managerIndex() 0 25 4
B apply() 0 35 6
A createAsManager() 0 17 1
A edit() 0 16 1
A fillAndSaveJobPosterQuestions() 0 28 5
A downloadApplicants() 0 51 5
C buildStructuredData() 0 127 13
C show() 0 153 14
A store() 0 31 4
A hrIndex() 0 7 1

How to fix   Complexity   

Complex Class

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
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
6
use App\Models\JobApplication;
7
use App\Models\JobApplicationVersion;
8
use App\Models\JobPoster;
9
use App\Models\JobPosterQuestion;
10
use App\Models\Lookup\ApplicationStatus;
11
use App\Models\Lookup\CitizenshipDeclaration;
12
use App\Models\Lookup\JobPosterStatus;
13
use App\Models\Lookup\VeteranStatus;
14
use App\Models\Manager;
15
use App\Services\JobPosterDefaultQuestions;
16
use App\Services\Validation\JobPosterValidator;
17
use Carbon\Carbon;
18
use Facades\App\Services\WhichPortal;
19
use Illuminate\Http\Request;
20
use Illuminate\Support\Facades\Auth;
21
use Illuminate\Support\Facades\Lang;
22
use Illuminate\Support\Facades\Response;
23
use Illuminate\Support\Facades\Validator;
24
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
25
26
class JobController extends Controller
27
{
28
    /**
29
     * Display a listing of JobPosters.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function index()
34
    {
35
        // If true, show the Paused due to COVID-19 message.
36
        $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

36
        $emergency_response = /** @scrutinizer ignore-call */ config('seasonal.is_covid_emergency');
Loading history...
37
38
        // Find published jobs that are currently open for applications.
39
        // Eager load required relationships: Department, Province, JobTerm.
40
        // Eager load the count of submitted applications, to prevent the relationship
41
        // from being actually loaded and firing off events.
42
        $jobs = JobPoster::where('internal_only', false)
43
            ->where('department_id', '!=', config('app.strategic_response_department_id'))
44
            ->where('job_poster_status_id', JobPosterStatus::where('key', 'live')->first()->id)
45
            ->with([
46
                'department',
47
                'province',
48
                'job_term',
49
            ])
50
            ->withCount([
51
                'submitted_applications',
52
            ])
53
            ->get();
54
55
        $null_alert = $emergency_response
56
            ? Lang::get('applicant/job_index.index.covid_null_alert')
57
            : Lang::get('applicant/job_index.index.null_alert');
58
        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

58
        return /** @scrutinizer ignore-call */ view('applicant/job_index', [
Loading history...
59
            'job_index' => Lang::get('applicant/job_index'),
60
            'null_alert' => $null_alert,
61
            'jobs' => $jobs
62
        ]);
63
    }
64
65
    /**
66
     * Display a listing of a manager's JobPosters.
67
     *
68
     * @return \Illuminate\Http\Response
69
     */
70
    public function managerIndex()
71
    {
72
        $manager = Auth::user()->manager;
73
74
        $jobs = JobPoster::where('manager_id', $manager->id)
75
            ->with('classification')
76
            ->withCount('submitted_applications')
77
            ->get();
78
79
        foreach ($jobs as &$job) {
80
            // If the chosen language is null then set to english.
81
            $chosen_lang = $job->chosen_lang ?: 'en';
82
83
            // Show chosen lang title if current title is empty.
84
            if (empty($job->title)) {
85
                $job->title = $job->getTranslation('title', $chosen_lang);
86
                $job->trans_required = true;
87
            }
88
        }
89
90
        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

90
        return /** @scrutinizer ignore-call */ view('manager/job_index', [
Loading history...
91
            // Localization Strings.
92
            'jobs_l10n' => Lang::get('manager/job_index'),
93
            // Data.
94
            'jobs' => $jobs,
95
        ]);
96
    }
97
98
    /**
99
     * Display a listing of a hr advisor's JobPosters.
100
     *
101
     * @param  \Illuminate\Http\Request $request Incoming request object.
102
     * @return \Illuminate\Http\Response
103
     */
104
    public function hrIndex(Request $request)
105
    {
106
        $hrAdvisor = $request->user()->hr_advisor;
107
        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

107
        return /** @scrutinizer ignore-call */ view('hr_advisor/job_index', [
Loading history...
108
            'jobs_l10n' => Lang::get('hr_advisor/job_index'),
109
            'hr_advisor_id' => $hrAdvisor->id,
110
            'disable_clone_js' => true,
111
        ]);
112
    }
113
114
    /**
115
     * Delete a draft Job Poster.
116
     *
117
     * @param  \Illuminate\Http\Request $request   Incoming request object.
118
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
119
     * @return \Illuminate\Http\Response
120
     */
121
    public function destroy(Request $request, JobPoster $jobPoster)
122
    {
123
        $jobPoster->delete();
124
    }
125
126
    /**
127
     * Display the specified job poster.
128
     *
129
     * @param  \Illuminate\Http\Request $request   Incoming request object.
130
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
131
     * @return \Illuminate\Http\Response
132
     */
133
    public function show(Request $request, JobPoster $jobPoster)
134
    {
135
        $jobPoster->load([
136
            'department',
137
            'criteria.skill.skill_type',
138
            'manager.team_culture',
139
            'manager.work_environment'
140
        ]);
141
142
        $user = Auth::user();
143
144
        // TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model.
145
        $workplacePhotos = [];
146
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
147
            $workplacePhotos[] = [
148
                'description' => $photoCaption->description,
149
                'url' => '/images/user.png'
150
            ];
151
        }
152
153
        // TODO: replace route('manager.show',manager.id) in templates with link using slug.
154
        $essential = $jobPoster->criteria->filter(
155
            function ($value, $key) {
156
                return $value->criteria_type->name == 'essential';
157
            }
158
        )->sortBy('id');
159
        $asset = $jobPoster->criteria->filter(
160
            function ($value, $key) {
161
                return $value->criteria_type->name == 'asset';
162
            }
163
        )->sortBy('id');
164
        $criteria = [
165
            'essential' => $essential,
166
            'asset' => $asset,
167
        ];
168
169
        $jobLang = Lang::get('applicant/job_post');
170
171
        $skillsLang = Lang::get('common/skills');
172
173
        $applyButton = [];
174
        if (WhichPortal::isManagerPortal()) {
175
            $applyButton = [
176
                '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

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

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

306
        return /** @scrutinizer ignore-call */ view(
Loading history...
307
            'manager/job_create',
308
            [
309
                // Localization Strings.
310
                'job_l10n' => Lang::get('manager/job_edit'),
311
                // Data.
312
                'manager' => $manager,
313
                'job' => $jobPoster,
314
                '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

314
                'form_action_url' => /** @scrutinizer ignore-call */ route('admin.jobs.update', $jobPoster),
Loading history...
315
            ]
316
        );
317
    }
318
319
    /**
320
     * Create a blank job poster for the specified manager
321
     *
322
     * @param  \App\Models\Manager $manager Incoming Manager object.
323
     * @return \Illuminate\Http\Response Job Create view
324
     */
325
    public function createAsManager(Manager $manager)
326
    {
327
        $jobPoster = new JobPoster();
328
        $jobPoster->manager_id = $manager->id;
329
330
        // Save manager-specific info to the job poster - equivalent to the intro step of the JPB.
331
        $divisionEn = $manager->getTranslation('division', 'en');
332
        $divisionFr = $manager->getTranslation('division', 'fr');
333
        $jobPoster->fill([
334
            'department_id' => $manager->user->department_id,
335
            'division' => ['en' => $divisionEn],
336
            'division' => ['fr' => $divisionFr],
337
        ]);
338
339
        $jobPoster->save();
340
341
        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

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

376
                return redirect(/** @scrutinizer ignore-call */ route('admin.jobs.edit', $jobPoster->id));
Loading history...
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

376
                return /** @scrutinizer ignore-call */ redirect(route('admin.jobs.edit', $jobPoster->id));
Loading history...
377
            }
378
        }
379
380
        $this->fillAndSaveJobPosterQuestions($input, $jobPoster, true);
381
382
        return redirect(route('manager.jobs.preview', $jobPoster->id));
383
    }
384
385
    /**
386
     * Fill Job Poster's questions and save
387
     *
388
     * @param  mixed[]               $input     Field values.
389
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
390
     * @param  boolean               $replace   Remove existing relationships.
391
     * @return void
392
     */
393
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void
394
    {
395
        if ($replace) {
396
            $jobPoster->job_poster_questions()->delete();
397
        }
398
399
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
400
            return;
401
        }
402
403
        foreach ($input['question'] as $question) {
404
            $jobQuestion = new JobPosterQuestion();
405
            $jobQuestion->job_poster_id = $jobPoster->id;
406
            $jobQuestion->fill(
407
                [
408
                    'question' => [
409
                        'en' => $question['question']['en'],
410
                        'fr' => $question['question']['fr']
411
412
                    ],
413
                    'description' => [
414
                        'en' => $question['description']['en'],
415
                        'fr' => $question['description']['fr']
416
                    ]
417
                ]
418
            );
419
            $jobPoster->save();
420
            $jobQuestion->save();
421
        }
422
    }
423
424
    /**
425
     * Downloads a CSV file with the applicants who have applied to the job poster.
426
     *
427
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
428
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
429
     */
430
    protected function downloadApplicants(JobPoster $jobPoster)
431
    {
432
        $tables = [];
433
        // The first row in the array represents the names of the columns in the spreadsheet.
434
        $tables[0] = ['Status', 'Applicant Name', 'Email', 'Language'];
435
436
        $application_status_id = ApplicationStatus::where('name', 'submitted')->first()->id;
437
        $applications = JobApplication::where('job_poster_id', $jobPoster->id)
438
            ->where('application_status_id', $application_status_id)
439
            ->get();
440
441
        $index = 1;
442
        foreach ($applications as $application) {
443
            $status = '';
444
            $username = $application->user_name;
445
            $user_email = $application->user_email;
446
            $language = strtoupper($application->preferred_language->name);
447
            // If the applicants veteran status name is NOT 'none' then set status to veteran.
448
            $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
449
            if ($application->veteran_status_id != $non_veteran) {
450
                $status = 'Veteran';
451
            } else {
452
                // Check if the applicant is a canadian citizen.
453
                $canadian_citizen = CitizenshipDeclaration::where('name', 'citizen')->first()->id;
454
                if ($application->citizenship_declaration->id == $canadian_citizen) {
455
                    $status = 'Citizen';
456
                } else {
457
                    $status = 'Non-citizen';
458
                }
459
            }
460
            $tables[$index] = [$status, $username, $user_email, $language];
461
            $index++;
462
        }
463
464
        $filename = $jobPoster->id . '-' . 'applicants-data.csv';
465
466
        // Open file.
467
        $file = fopen($filename, 'w');
468
        // Iterate through tables and add each line to csv file.
469
        foreach ($tables as $line) {
470
            fputcsv($file, $line);
471
        }
472
        // Close open file.
473
        fclose($file);
474
475
        $headers = [
476
            'Content-Type' => 'text/csv',
477
            'Content-Disposition' => 'attachment; filename=' . $filename,
478
        ];
479
480
        return Response::download($filename, $filename, $headers);
481
    }
482
483
    /**
484
     * Build Job Poster's structured data
485
     *
486
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
487
     * @return array
488
     */
489
    protected function buildStructuredData(JobPoster $jobPoster)
490
    {
491
        $gocLang = Lang::get('common/goc');
492
493
        $jobLang = Lang::get('applicant/job_post');
494
495
        $essential = $jobPoster->criteria->filter(
496
            function ($value) {
497
                return $value->criteria_type->name == 'essential';
498
            }
499
        )->sortBy('id');
500
        $asset = $jobPoster->criteria->filter(
501
            function ($value) {
502
                return $value->criteria_type->name == 'asset';
503
            }
504
        )->sortBy('id');
505
506
        $skillsArray = [];
507
        foreach ($essential as $criterion) {
508
            $skillsArray[] = [
509
                'skill' => $criterion['skill']['name'],
510
                'level' => $criterion->level_name,
511
            ];
512
        }
513
        foreach ($asset as $criterion) {
514
            $skillsArray[] = [
515
                'skill' => $criterion['skill']['name'],
516
                'level' => $criterion->level_name,
517
            ];
518
        }
519
520
        $skillsString = '';
521
        if (!empty($skillsArray)) {
522
            $lastSkill = end($skillsArray);
523
            foreach ($skillsArray as $skillItem) {
524
                $skillsString .= $skillItem['skill'].' - '.$skillItem['level'].($skillItem == $lastSkill ? '.' : ', ');
525
            }
526
        }
527
528
        $benefitsString = '';
529
        if (is_array($jobLang['structured_data']['benefits'])) {
530
            foreach ($jobLang['structured_data']['benefits'] as $benefit) {
531
                $benefitsString .= $benefit.' ';
532
            }
533
        }
534
535
        $tasksString = '';
536
        if (is_array($jobPoster['job_poster_key_tasks'])) {
537
            $lastTask = end($jobPoster['job_poster_key_tasks']);
538
            foreach ($jobPoster['job_poster_key_tasks'] as $task) {
539
                $tasksString .= $task['description'].' '.($task == $lastTask ? '' : ' | ');
540
            }
541
        }
542
543
        $securityClearanceString = $jobLang['structured_data']['security_clearance_requirement_level'].': '
544
        .$jobPoster['security_clearance']['value'].'. '
545
        .$jobLang['structured_data']['security_clearance_requirement_description'];
546
547
        $structuredData = [
548
            '@context' => 'https://schema.org',
549
            '@type' => 'JobPosting',
550
            '@id' => url()->current(),
0 ignored issues
show
Bug introduced by
The function url 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

550
            '@id' => /** @scrutinizer ignore-call */ url()->current(),
Loading history...
551
            'url' => url('/'),
552
            'inLanguage' => app()->getLocale(),
0 ignored issues
show
Bug introduced by
The function app 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

552
            'inLanguage' => /** @scrutinizer ignore-call */ app()->getLocale(),
Loading history...
553
            'applicantLocationRequirements' => [
554
                '@type' => 'Country',
555
                'sameAs' => 'https://www.wikidata.org/wiki/Q16',
556
                'name' => 'Canada'
557
            ],
558
            'applicationContact' => [
559
                '@type' => 'ContactPoint',
560
                'email' => '[email protected]'
561
            ],
562
            'baseSalary' => [
563
                '@type' => 'MonetaryAmount',
564
                'currency' => 'CAD',
565
                'minValue' => $jobPoster['salary_min'],
566
                'maxValue' => $jobPoster['salary_max'],
567
            ],
568
            'datePosted' => $jobPoster['open_date_time'],
569
            'employerOverview' => $jobLang['structured_data']['employer_overview'],
570
            'employmentType' => $jobLang['structured_data']['employment_type'],
571
            'estimatedSalary' => [
572
                '@type' => 'MonetaryAmount',
573
                'currency' => 'CAD',
574
                'minValue' => $jobPoster['salary_min'],
575
                'maxValue' => $jobPoster['salary_max']
576
            ],
577
            'hiringOrganization' => [
578
                '@type' => 'GovernmentOrganization',
579
                'name' => $jobPoster['manager']['user']['department']['name'],
580
                'parentOrganization' => [
581
                    '@type' => 'GovernmentOrganization',
582
                    'name' => $jobLang['structured_data']['parent_organization'],
583
                    'url' => $gocLang['logo_link'],
584
                    'logo' => asset('/images/logo_canada_colour.png'),
0 ignored issues
show
Bug introduced by
The function asset 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

584
                    'logo' => /** @scrutinizer ignore-call */ asset('/images/logo_canada_colour.png'),
Loading history...
585
                ]
586
            ],
587
            'industry' => $jobLang['structured_data']['industry'],
588
            'jobBenefits' => $benefitsString,
589
            'jobLocation' => [
590
                '@type' => 'Place',
591
                'address' => [
592
                    '@type' => 'PostalAddress',
593
                    'addressLocality' => $jobPoster['city'],
594
                    'addressRegion' => $jobPoster['province']['value'],
595
                    'addressCountry' => 'CA'
596
                ]
597
            ],
598
            'jobStartDate' => $jobPoster['start_date_time'],
599
            'qualifications' => $jobPoster['education'],
600
            'responsibilities' => $tasksString,
601
            'salaryCurrency' => 'CAD',
602
            'securityClearanceRequirement' => $securityClearanceString,
603
            'skills' => $skillsString,
604
            'specialCommitments' => $jobLang['structured_data']['special_commitments'],
605
            'title' => $jobPoster['title'],
606
            'totalJobOpenings' => '1',
607
            'validThrough' => $jobPoster['close_date_time'],
608
            'description' => $jobPoster['hire_impact'] ? nl2br($jobPoster['hire_impact']) : '',
609
        ];
610
611
        if ($jobPoster['remote_work_allowed'] == true) {
612
            $structuredData['jobLocationType'] = 'TELECOMMUTE';
613
        };
614
615
        return $structuredData;
616
    }
617
618
    /**
619
     * Redirects an applicant to the proper page to start or continue their application.
620
     *
621
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
622
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
623
     */
624
    protected function apply(JobPoster $jobPoster)
625
    {
626
        $applicantId = Auth::user()->applicant->id;
627
        // Check if the current user already has an application.
628
        $application = JobApplication::where('applicant_id', $applicantId)
629
            ->where('job_poster_id', $jobPoster->id)->first();
630
        $version2id = JobApplicationVersion::where('version', 2)->firstOrFail()->id;
631
        if ($application === null) {
632
            // If the job is still open, create a new application and redirect there.
633
            if ($jobPoster->isOpen()) {
634
                $newApplication = new JobApplication();
635
                $newApplication->job_poster_id = $jobPoster->id;
636
                $newApplication->applicant_id = $applicantId;
637
                $newApplication->application_status_id = ApplicationStatus::where('name', 'draft')->firstOrFail()->id;
638
                $newApplication->version_id = $version2id; // All applications created now should be version 2.
639
                $newApplication->save();
640
                $newApplication->attachSteps();
641
642
                return redirect(route('applications.timeline', $newApplication->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

642
                return /** @scrutinizer ignore-call */ redirect(route('applications.timeline', $newApplication->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

642
                return redirect(/** @scrutinizer ignore-call */ route('applications.timeline', $newApplication->id));
Loading history...
643
            }
644
            // If the job is closed, redirect to Job Poster.
645
            return redirect(route('jobs.summary', $jobPoster->id));
646
        }
647
648
        // If the application exists, is a draft, and job is still open, redirect to the Application UI.
649
        if ($application->isDraft() && $jobPoster->isOpen()) {
650
            if ($application->version_id === $version2id) {
651
                return redirect(route('applications.timeline', $application->id));
652
            }
653
            return redirect(route('job.application.edit.1', $jobPoster->id));
654
        }
655
656
        // If the application exists but has already been submitted, or the job is closed,
657
        // redirect to the Application Preview.
658
        return redirect(route('applications.show', $application->id));
659
    }
660
}
661