JobController::show()   C
last analyzed

Complexity

Conditions 14
Paths 42

Size

Total Lines 153
Code Lines 109

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 55
CRAP Score 15.126

Importance

Changes 0
Metric Value
eloc 109
c 0
b 0
f 0
dl 0
loc 153
rs 5.0133
ccs 55
cts 67
cp 0.8209
cc 14
nc 42
nop 2
crap 15.126

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\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 1
74
        $jobs = JobPoster::where('manager_id', $manager->id)
75 1
            ->with('classification')
76 1
            ->withCount('submitted_applications')
77 1
            ->get();
78 1
79
        foreach ($jobs as &$job) {
80 1
            // If the chosen language is null then set to english.
81
            $chosen_lang = $job->chosen_lang ?: 'en';
82 1
83
            // Show chosen lang title if current title is empty.
84 1
            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 1
        ]);
96
    }
97
98 1
    /**
99 1
     * Display a listing of a hr advisor's JobPosters.
100
     *
101
     * @param  \Illuminate\Http\Request $request Incoming request object.
102 1
     * @return \Illuminate\Http\Response
103
     */
104
    public function hrIndex(Request $request)
105 1
    {
106 1
        $hrAdvisor = $request->user()->hr_advisor;
107 1
        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 1
    }
113
114 1
    /**
115 1
     * 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 6
            'manager.team_culture',
139
            'manager.work_environment'
140 6
        ]);
141 6
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 6
            $workplacePhotos[] = [
148
                'description' => $photoCaption->description,
149
                'url' => '/images/user.png'
150 6
            ];
151 6
        }
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 6
            function ($value, $key) {
161
                return $value->criteria_type->name == 'asset';
162 4
            }
163 6
        )->sortBy('id');
164
        $criteria = [
165 6
            'essential' => $essential,
166
            'asset' => $asset,
167 4
        ];
168 6
169
        $jobLang = Lang::get('applicant/job_post');
170
171
        $skillsLang = Lang::get('common/skills');
172 6
173
        $applyButton = [];
174 6
        if (WhichPortal::isManagerPortal()) {
175 6
            $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 4
                'title' => $jobLang['apply']['edit_link_title'],
178 4
                'text' => $jobLang['apply']['edit_link_label'],
179 4
            ];
180
        } elseif (WhichPortal::isHrPortal()) {
181 2
            if ($jobPoster->hr_advisors->contains('user_id', $user->id)) {
182
                $applyButton = [
183 1
                    'href' => route('hr_advisor.jobs.summary', $jobPoster->id),
184 1
                    'title' => null,
185 1
                    'text' => Lang::get('hr_advisor/job_summary.summary_title'),
186
                ];
187 1
            } else {
188
                $applyButton = [
189 1
                    'href' => route('hr_advisor.jobs.index'),
190 1
                    'title' => null,
191 1
                    '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 6
                    'title' => $jobLang['apply']['view_link_title'],
202 6
                    'text' => $jobLang['apply']['view_link_label'],
203
                ];
204 6
            } else {
205 6
                $applyButton = [
206 6
                    'href' => route('jobs.apply', $jobPoster->id),
207 6
                    'title' => $jobLang['apply']['apply_link_title'],
208 6
                    'text' => $jobLang['apply']['apply_link_label'],
209 6
                ];
210 6
            }
211 6
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
212 6
            $applyButton = [
213 6
                '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 2
225
        $jpb_release_date = strtotime('2019-08-21 16:18:17');
226 2
        $job_created_at = strtotime($jobPoster->created_at);
227 2
228
        $custom_breadcrumbs = [
229 2
            'home' => route('home'),
230
            'jobs' => route(WhichPortal::prefixRoute('jobs.index')),
231 2
            $jobPoster->title ?: 'job-title-missing' => route(WhichPortal::prefixRoute('jobs.summary'), $jobPoster),
232 2
            'preview' => '',
233 2
        ];
234
235
        // If the poster is part of the Strategic Talent Response dept, use the talent stream template.
236 2
        // 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 1
                [
258
                    'job_post' => $jobLang,
259 1
                    '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 1
        } else {
270
            // Old job poster.
271 1
            return view(
272
                'applicant/job_post',
273
                [
274 1
                    'job_post' => $jobLang,
275
                    'frequencies' => Lang::get('common/lookup/frequency'),
276
                    'manager' => $jobPoster->manager,
277 1
                    'manager_profile_photo_url' => '/images/user.png', // TODO get real photo.
278 1
                    'team_culture' => $jobPoster->manager->team_culture,
279 1
                    'work_environment' => $jobPoster->manager->work_environment,
280 1
                    '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 1
    /**
292
     * Display the form for editing an existing Job Poster
293 1
     * Only allows editing fields that don't appear on the react-built Job Poster Builder.
294 1
     *
295
     * @param  \Illuminate\Http\Request $request   Incoming request object.
296 1
     * @param  \App\Models\JobPoster    $jobPoster Job Poster object.
297 1
     * @return \Illuminate\Http\Response
298 1
     */
299 1
    public function edit(Request $request, JobPoster $jobPoster)
300
    {
301
        $manager = $jobPoster->manager;
302 1
303
        $defaultQuestionManager = new JobPosterDefaultQuestions();
304 1
        $defaultQuestionManager->initializeQuestionsIfEmpty($jobPoster);
305
306 1
        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 1
            [
309 1
                // Localization Strings.
310
                'job_l10n' => Lang::get('manager/job_edit'),
311 1
                // Data.
312 1
                'manager' => $manager,
313 1
                'job' => $jobPoster,
314 1
                '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 1
    }
318
319 1
    /**
320
     * Create a blank job poster for the specified manager
321 1
     *
322
     * @param  \App\Models\Manager $manager Incoming Manager object.
323 1
     * @return \Illuminate\Http\Response Job Create view
324 1
     */
325
    public function createAsManager(Manager $manager)
326
    {
327
        $jobPoster = new JobPoster();
328 1
        $jobPoster->manager_id = $manager->id;
329 1
330
        // Save manager-specific info to the job poster - equivalent to the intro step of the JPB.
331
        $divisionEn = $manager->getTranslation('division', 'en');
332 1
        $divisionFr = $manager->getTranslation('division', 'fr');
333 1
        $jobPoster->fill([
334
            'department_id' => $manager->user->department_id,
335
            'division' => ['en' => $divisionEn],
336
            'division' => ['fr' => $divisionFr],
337 1
        ]);
338
339 1
        $jobPoster->save();
340
341 1
        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 1
344 1
    /**
345 1
     * Update a resource in storage
346
     * NOTE: Only saves fields that are not on the react-built Job Poster Builder
347 1
     *
348
     * @param  \Illuminate\Http\Request $request   Incoming request object.
349 1
     * @param  \App\Models\JobPoster    $jobPoster Optional Job Poster object.
350 1
     * @return \Illuminate\Http\Response
351 1
     */
352
    public function store(Request $request, JobPoster $jobPoster)
353 1
    {
354 1
        // Don't allow edits for published Job Posters
355
        // Also check auth while we're at it.
356
        $this->authorize('update', $jobPoster);
357 1
        JobPosterValidator::validateUnpublished($jobPoster);
358
359 1
        $input = $request->input();
360 1
361 1
        if ($jobPoster->manager_id == null) {
362 1
            $jobPoster->manager_id = $request->user()->manager->id;
363 1
            $jobPoster->save();
364 1
        }
365 1
366 1
        if ($request->input('question')) {
367 1
            $validator = Validator::make($request->input('question'), [
368 1
                '*.question.*' => 'required|string',
369 1
            ], [
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 6
382
        return redirect(route('manager.jobs.preview', $jobPoster->id));
383
    }
384
385 6
    /**
386 6
     * Fill Job Poster's questions and save
387 6
     *
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 6
     */
393
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace): void
394 6
    {
395
        if ($replace) {
396 6
            $jobPoster->job_poster_questions()->delete();
397
        }
398
399
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
400
            return;
401 6
        }
402
403 6
        foreach ($input['question'] as $question) {
404
            $jobQuestion = new JobPosterQuestion();
405 6
            $jobQuestion->job_poster_id = $jobPoster->id;
406
            $jobQuestion->fill(
407 6
                [
408
                    'question' => [
409 6
                        '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 6
            $jobPoster->save();
420
            $jobQuestion->save();
421 6
        }
422
    }
423 6
424 6
    /**
425 6
     * Downloads a CSV file with the applicants who have applied to the job poster.
426 6
     *
427 6
     * @param  \App\Models\JobPoster $jobPoster Job Poster object.
428 6
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
429 6
     */
430 6
    protected function downloadApplicants(JobPoster $jobPoster)
431 6
    {
432 6
        $tables = [];
433 6
        // The first row in the array represents the names of the columns in the spreadsheet.
434 6
        $tables[0] = ['Status', 'Applicant Name', 'Email', 'Language'];
435 6
436 6
        $application_status_id = ApplicationStatus::where('name', 'submitted')->first()->id;
437
        $applications = JobApplication::where('job_poster_id', $jobPoster->id)
438 6
            ->where('application_status_id', $application_status_id)
439 6
            ->get();
440 6
441 6
        $index = 1;
442 6
        foreach ($applications as $application) {
443 6
            $status = '';
444
            $username = $application->user_name;
445
            $user_email = $application->user_email;
446 6
            $language = strtoupper($application->preferred_language->name);
447 6
            // If the applicants veteran status name is NOT 'none' then set status to veteran.
448 6
            $non_veteran = VeteranStatus::where('name', 'none')->first()->id;
449 6
            if ($application->veteran_status_id != $non_veteran) {
450 6
                $status = 'Veteran';
451 6
            } 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 6
                    $status = 'Citizen';
456 6
                } 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 6
        // Open file.
467
        $file = fopen($filename, 'w');
468 6
        // Iterate through tables and add each line to csv file.
469 6
        foreach ($tables as $line) {
470
            fputcsv($file, $line);
471
        }
472 6
        // Close open file.
473 6
        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 6
            function ($value) {
502
                return $value->criteria_type->name == 'asset';
503 6
            }
504 6
        )->sortBy('id');
505
506
        $skillsArray = [];
507 6
        foreach ($essential as $criterion) {
508 6
            $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 6
            $lastTask = end($jobPoster['job_poster_key_tasks']);
538
            foreach ($jobPoster['job_poster_key_tasks'] as $task) {
539 6
                $tasksString .= $task['description'].' '.($task == $lastTask ? '' : ' | ');
540
            }
541 6
        }
542 2
543
        $securityClearanceString = $jobLang['structured_data']['security_clearance_requirement_level'].': '
544
        .$jobPoster['security_clearance']['value'].'. '
545 2
        .$jobLang['structured_data']['security_clearance_requirement_description'];
546 1
547 1
        $structuredData = [
548 1
            '@context' => 'https://schema.org',
549 1
            '@type' => 'JobPosting',
550 1
            '@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 2
                'name' => 'Canada'
557 1
            ],
558 1
            'applicationContact' => [
559 1
                '@type' => 'ContactPoint',
560 1
                'email' => '[email protected]'
561 1
            ],
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 6
            'employerOverview' => $jobLang['structured_data']['employer_overview'],
570 6
            'employmentType' => $jobLang['structured_data']['employment_type'],
571 6
            'estimatedSalary' => [
572
                '@type' => 'MonetaryAmount',
573 6
                '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 2
                    '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 2
            ],
587
            'industry' => $jobLang['structured_data']['industry'],
588
            'jobBenefits' => $benefitsString,
589 2
            'jobLocation' => [
590 2
                '@type' => 'Place',
591 2
                'address' => [
592 2
                    '@type' => 'PostalAddress',
593
                    'addressLocality' => $jobPoster['city'],
594 2
                    'addressRegion' => $jobPoster['province']['value'],
595 2
                    'addressCountry' => 'CA'
596 2
                ]
597
            ],
598 2
            'jobStartDate' => $jobPoster['start_date_time'],
599
            'qualifications' => $jobPoster['education'],
600
            'responsibilities' => $tasksString,
601 2
            'salaryCurrency' => 'CAD',
602
            'securityClearanceRequirement' => $securityClearanceString,
603
            'skills' => $skillsString,
604
            'specialCommitments' => $jobLang['structured_data']['special_commitments'],
605 2
            'title' => $jobPoster['title'],
606 1
            'totalJobOpenings' => '1',
607 1
            'validThrough' => $jobPoster['close_date_time'],
608 1
            'description' => $jobPoster['hire_impact'] ? nl2br($jobPoster['hire_impact']) : '',
609
        ];
610 1
611 1
        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 1
     */
624
    protected function apply(JobPoster $jobPoster)
625 1
    {
626 1
        $applicantId = Auth::user()->applicant->id;
627 1
        // Check if the current user already has an application.
628 1
        $application = JobApplication::where('applicant_id', $applicantId)
629 1
            ->where('job_poster_id', $jobPoster->id)->first();
630
        $version2id = JobApplicationVersion::where('version', 2)->firstOrFail()->id;
631 1
        if ($application === null) {
632 1
            // If the job is still open, create a new application and redirect there.
633 1
            if ($jobPoster->isOpen()) {
634
                $newApplication = new JobApplication();
635 1
                $newApplication->job_poster_id = $jobPoster->id;
636
                $newApplication->applicant_id = $applicantId;
637 1
                $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 1
648
        // If the application exists, is a draft, and job is still open, redirect to the Application UI.
649 1
        if ($application->isDraft() && $jobPoster->isOpen()) {
650 1
            if ($application->version_id === $version2id) {
651 1
                return redirect(route('applications.timeline', $application->id));
652 1
            }
653 1
            return redirect(route('job.application.edit.1', $jobPoster->id));
654 1
        }
655 1
656
        // If the application exists but has already been submitted, or the job is closed,
657 1
        // redirect to the Application Preview.
658
        return redirect(route('applications.show', $application->id));
659 1
    }
660
}
661