Passed
Push — feature/manager_poster_index ( b676df...f1e2b3 )
by Chris
05:48
created

JobController::submitForReview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 9
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Auth;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\Mail;
9
use Illuminate\Http\RedirectResponse;
10
use Illuminate\Http\Request;
11
use Illuminate\Http\Response;
12
use Illuminate\View\View;
13
use App\Http\Controllers\Controller;
14
15
use Carbon\Carbon;
16
17
use App\Mail\JobPosterReviewRequested;
18
19
use App\Models\JobPoster;
20
use App\Models\JobPosterQuestion;
21
use App\Models\Lookup\JobTerm;
22
use App\Models\Lookup\Province;
23
use App\Models\Lookup\SecurityClearance;
24
use App\Models\Lookup\LanguageRequirement;
25
use App\Models\Lookup\CitizenshipDeclaration;
26
use App\Models\Lookup\Department;
27
use App\Models\Lookup\SkillLevel;
28
use App\Models\Lookup\CriteriaType;
29
use App\Models\Lookup\VeteranStatus;
30
use App\Models\JobApplication;
31
use App\Models\Criteria;
32
use App\Models\Skill;
33
use App\Models\JobPosterKeyTask;
34
35
use App\Services\Validation\JobPosterValidator;
36
use Jenssegers\Date\Date;
37
38
class JobController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JobController
Loading history...
39
{
40
    /**
41
     * Display a listing of JobPosters.
42
     *
43
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
44
     */
45
    public function index()
46
    {
47
        $now = Carbon::now();
48
49
        //Find published jobs that are currently open for applications
50
        $jobs = JobPoster::where('open_date_time', '<=', $now)
51
            ->where('close_date_time', '>=', $now)
52
            ->where('published', true)
53
            ->get();
54
        $jobs->load('manager.work_environment');
55
        return view('applicant/job_index', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
56
            'job_index' => Lang::get('applicant/job_index'),
57
            'jobs' => $jobs
58
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
59
    }
60
61
    /**
62
     * Display a listing of a manager's JobPosters.
63
     *
64
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
65
     */
66 2
    public function managerIndex()
67
    {
68 2
        $manager = Auth::user()->manager;
0 ignored issues
show
Bug introduced by
Accessing manager on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
69
70 2
        $veteran_applications = [];
71 2
        $citizen_applications = [];
72 2
        $other_applications = [];
73
74 2
        foreach ($manager->job_posters as $job) {
75 2
            $job->submitted_applications->load(['veteran_status', 'citizenship_declaration']);
76
            $veteran_applications[$job->id] = $job->submitted_applications->filter(function ($application) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
77
                return $application->veteran_status->name !== "none" &&
78
                    $application->citizenship_declaration->name === "citizen";
79 2
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
80
            $citizen_applications[$job->id] = $job->submitted_applications->filter(function ($application) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
81
                return $application->veteran_status->name === "none" &&
82
                    $application->citizenship_declaration->name === "citizen";
83 2
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
84
            $other_applications[$job->id] = $job->submitted_applications->filter(function ($application) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
85
                return $application->citizenship_declaration->name !== "citizen";
86 2
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
87
        }
88
89 2
        return view('manager/job_index', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
90
            /*Localization Strings*/
91 2
            'jobs_l10n' => Lang::get('manager/job_index'),
92
93
            /* Data */
94 2
            'jobs' => $manager->job_posters,
95 2
            'veteran_applications' => $veteran_applications,
96 2
            'citizen_applications' => $citizen_applications,
97 2
            'other_applications' => $other_applications,
98
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
99
    }
100
101
    /**
102
     * Submit the Job Poster for review.
103
     *
104
     * @param \Illuminate\Http\Request $request   Incoming request object.
105
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
106
     *
107
     * @return \Illuminate\View\View
108
     */
109
    public function submitForReview(Request $request, JobPoster $jobPoster)
110
    {
111
        // Update review request timestamp
112
        $jobPoster->review_requested_at = new Date();
113
        $jobPoster->save();
114
115
        // Send email
116
        // Mail::to(config('mail.reviewer_email'))->send(new JobPosterReviewRequested($jobPoster, Auth::user()));
117
        return (new JobPosterReviewRequested($jobPoster, Auth::user()))->render();
0 ignored issues
show
Bug introduced by
It seems like Illuminate\Support\Facades\Auth::user() can also be of type null; however, parameter $manager of App\Mail\JobPosterReviewRequested::__construct() does only seem to accept App\Models\User, 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

117
        return (new JobPosterReviewRequested($jobPoster, /** @scrutinizer ignore-type */ Auth::user()))->render();
Loading history...
118
    }
119
120
    /**
121
     * Display the specified job poster.
122
     *
123
     * @param \Illuminate\Http\Request $request   Incoming request object.
124
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
125
     *
126
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
127
     */
128 6
    public function show(Request $request, JobPoster $jobPoster)
129
    {
130 6
        $user = Auth::user();
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
131
132
        //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model
133 6
        $workplacePhotos = [];
134 6
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
135
            $workplacePhotos[] = [
136
                'description' => $photoCaption->description,
137
                'url' => '/images/user.png'
138
            ];
139
        }
140
141
        //TODO: replace route('manager.show',manager.id) in templates with link using slug
142
143
        $criteria = [
144 6
            'essential' => $jobPoster->criteria->filter(
145
                function ($value, $key) {
146 4
                    return $value->criteria_type->name == 'essential';
147 6
                }
148
            ),
149 6
            'asset' => $jobPoster->criteria->filter(
150
                function ($value, $key) {
151 4
                    return $value->criteria_type->name == 'asset';
152 6
                }
153
            ),
154
        ];
155
156 6
        $jobLang = Lang::get('applicant/job_post');
157
158 6
        $applyButton = [];
159 6
        if (!$jobPoster->published && $this->authorize('update', $jobPoster)) {
160
            $applyButton = [
161 2
                'href' => route('manager.jobs.edit', $jobPoster->id),
162 2
                'title' => $jobLang['apply']['edit_link_title'],
163 2
                'text' => $jobLang['apply']['edit_link_label'],
164
            ];
165 4
        } elseif (Auth::check() && $jobPoster->isOpen()) {
166
            $applyButton = [
167 2
                'href' => route('job.application.edit.1', $jobPoster->id),
168 2
                'title' => $jobLang['apply']['apply_link_title'],
169 2
                'text' => $jobLang['apply']['apply_link_label'],
170
            ];
171 2
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
172
            $applyButton = [
173 2
                'href' => route('job.application.edit.1', $jobPoster->id),
174 2
                'title' => $jobLang['apply']['login_link_title'],
175 2
                'text' => $jobLang['apply']['login_link_label'],
176
            ];
177
        } else {
178
            $applyButton = [
179
                'href' => null,
180
                'title' => null,
181
                'text' => $jobLang['apply']['job_closed_label'],
182
            ];
183
        }
184
185 6
        return view(
186 6
            'applicant/job_post',
187
            [
188 6
                'job_post' => $jobLang,
189 6
                'manager' => $jobPoster->manager,
190 6
                'manager_profile_photo_url' => '/images/user.png', //TODO get real photo
191 6
                'team_culture' => $jobPoster->manager->team_culture,
192 6
                'work_environment' => $jobPoster->manager->work_environment,
193 6
                'workplace_photos' => $workplacePhotos,
194 6
                'job' => $jobPoster,
195 6
                'criteria' => $criteria,
196 6
                'apply_button' => $applyButton,
197 6
                'skill_template' => Lang::get('common/skills'),
198
            ]
199
        );
200
    }
201
202
    /**
203
     * Display the form for creating a new Job Poster
204
     *
205
     * @param \Illuminate\Http\Request $request Incoming request object.
206
     *
207
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
208
     */
209 2
    public function create(Request $request)
210
    {
211 2
        return $this->populateCreateView($request);
212
    }
213
214
    /**
215
     * Display the form for editing an existing Job Poster
216
     *
217
     * @param \Illuminate\Http\Request $request   Incoming request object.
218
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
219
     *
220
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
221
     */
222 2
    public function edit(Request $request, JobPoster $jobPoster)
223
    {
224 2
        return $this->populateCreateView($request, $jobPoster);
225
    }
226
227
    /**
228
     * Get the manager from the request object and check if creating or editing
229
     *
230
     * @param \Illuminate\Http\Request $request   Incoming request object.
231
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object.
232
     *
233
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
234
     */
235 4
    public function populateCreateView(Request $request, JobPoster $jobPoster = null)
236
    {
237 4
        $manager = $request->user() ? $request->user()->manager : null;
238 4
        if (isset($jobPoster)) {
239 2
            $job = $jobPoster;
240 2
            $route = ['manager.jobs.store', $jobPoster];
241 2
            $jobHeading = 'manager/job_edit';
242
        } else {
243 2
            $job = [];
244 2
            $defaultQuestions = $this->populateDefaultQuestions();
245 2
            if (!empty($defaultQuestions)) {
246 2
                $job['job_poster_questions'] = $defaultQuestions;
247
            }
248 2
            $route = ['manager.jobs.store'];
249 2
            $jobHeading = 'manager/job_create';
250
        }
251
252 4
        $skillLangs = Lang::get('common/skills');
253
254 4
        $softSkills = Skill::whereHas(
255 4
            'skill_type',
256
            function ($query) : void {
257 4
                $query->where('name', '=', 'soft');
258 4
            }
259 4
        )->get()
260 4
            ->mapWithKeys(
261
                function ($skill) use ($skillLangs) {
262
                    return [
263 4
                        $skill->id => $skillLangs['skills'][$skill->name]['name']
264
                    ];
265 4
                }
266
            )
267 4
            ->all();
268
269 4
        $hardSkills = Skill::whereHas(
270 4
            'skill_type',
271
            function ($query) : void {
272 4
                $query->where('name', '=', 'hard');
273 4
            }
274 4
        )->get()
275 4
            ->mapWithKeys(
276
                function ($skill) use ($skillLangs) {
277
                    return [
278 4
                        $skill->id => $skillLangs['skills'][$skill->name]['name']
279
                    ];
280 4
                }
281
            )
282 4
            ->all();
283
284 4
        asort($softSkills, SORT_LOCALE_STRING);
285 4
        asort($hardSkills, SORT_LOCALE_STRING);
286
287
        $skills = [
288
            'essential' => [
289 4
                'hard' => $hardSkills,
290 4
                'soft' => $softSkills
291
            ],
292
            'asset' => [
293 4
                'hard' => $hardSkills,
294 4
                'soft' => $softSkills
295
            ]
296
        ];
297
298 4
        $skillLevelCollection = SkillLevel::all();
299
300 4
        $skillLevels = array();
301
302 4
        $skillLevels['hard'] = $skillLevelCollection->mapWithKeys(
303
            function ($skillLevel) use ($skillLangs) {
304 4
                return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]];
305 4
            }
306 4
        )->all();
307
308 4
        $skillLevels['soft'] = $skillLevelCollection->mapWithKeys(
309
            function ($skillLevel) use ($skillLangs) {
310 4
                return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]];
311 4
            }
312 4
        )->all();
313
314 4
        return view(
315 4
            'manager/job_create',
316
            [
317
                /*Localization Strings*/
318 4
                'job_l10n' => Lang::get('manager/job_create'),
319
320
                /* Data */
321 4
                'job' => Lang::get($jobHeading),
322 4
                'manager' => $manager,
323 4
                'provinces' => Province::all(),
324 4
                'departments' => Department::all(),
325 4
                'language_requirments' => LanguageRequirement::all(),
326 4
                'security_clearances' => SecurityClearance::all(),
327 4
                'job' => $job,
328 4
                'form_action_url' => route(/** @scrutinizer ignore-type */ ...$route), // phpcs:ignore
0 ignored issues
show
Bug introduced by
$route is expanded, but the parameter $name of route() does not expect variable arguments. ( Ignorable by Annotation )

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

328
                'form_action_url' => route(/** @scrutinizer ignore-type */ /** @scrutinizer ignore-type */ ...$route), // phpcs:ignore
Loading history...
329 4
                'skills' => $skills,
330 4
                'skill_levels' => $skillLevels,
331 4
                'skill_template' => $skillLangs,
332
            ]
333
        );
334
    }
335
336
    /**
337
     * Create a new resource in storage
338
     *
339
     * @param \Illuminate\Http\Request $request   Incoming request object.
340
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object.
341
     *
342
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index
343
     */
344 2
    public function store(Request $request, JobPoster $jobPoster = null)
345
    {
346
        // Don't allow edits for published Job Posters
347
        // Also check auth while we're at it
348 2
        if (isset($jobPoster)) {
349
            $this->authorize('update', $jobPoster);
350
            JobPosterValidator::validateUnpublished($jobPoster);
351
        } else {
352 2
            $this->authorize('create', JobPoster::class);
353
        }
354
355 2
        $input = $request->input();
356
357 2
        $job = (isset($jobPoster) ? $jobPoster : new JobPoster());
358
359 2
        $job->manager_id = $request->user()->manager->id;
360 2
        $job->published = ($input['submit'] == 'publish');
361
362 2
        $this->fillAndSaveJobPoster($input, $job);
363
364 2
        $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster));
365
366 2
        $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster));
367
368 2
        $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster));
369
370 2
        $route = $job->published ? route('manager.jobs.index') : route('manager.jobs.show', $job->id);
371
372 2
        return redirect($route);
373
    }
374
375
    /**
376
     * Fill Job Poster model's properties and save
377
     *
378
     * @param mixed[]               $input     Field values.
379
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
380
     *
381
     * @return void
382
     */
383 2
    protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void
384
    {
385 2
        $jobPoster->fill(
386
            [
387 2
                'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id,
388 2
                'term_qty' => $input['term_qty'],
389 2
                'open_date_time' => new Date($input['open_date'] . $input['open_time']),
390 2
                'close_date_time' => new Date($input['close_date'] . $input['close_time']),
391 2
                'start_date_time' => new Date($input['start_date_time']),
392 2
                'department_id' => $input['department'],
393 2
                'province_id' => $input['province'],
394 2
                'salary_min' => $input['salary_min'],
395 2
                'salary_max' => $input['salary_max'],
396 2
                'noc' => $input['noc'],
397 2
                'classification' => $input['classification'],
398 2
                'security_clearance_id' => $input['security_clearance'],
399 2
                'language_requirement_id' => $input['language_requirement'],
400 2
                'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false),
401
                'en' => [
402 2
                    'city' => $input['city'],
403 2
                    'title' => $input['title']['en'],
404 2
                    'impact' => $input['impact']['en'],
405 2
                    'branch' => $input['branch']['en'],
406 2
                    'division' => $input['division']['en'],
407 2
                    'education' => $input['education']['en'],
408
                ],
409
                'fr' => [
410 2
                    'city' => $input['city'],
411 2
                    'title' => $input['title']['fr'],
412 2
                    'impact' => $input['impact']['fr'],
413 2
                    'branch' => $input['branch']['fr'],
414 2
                    'division' => $input['division']['fr'],
415 2
                    'education' => $input['education']['fr'],
416
                ],
417
            ]
418
        );
419 2
        $jobPoster->save();
420 2
    }
421
422
    /**
423
     * Fill Job Poster's tasks and save
424
     *
425
     * @param mixed[]               $input     Field values.
426
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
427
     * @param boolean               $replace   Remove existing relationships.
428
     *
429
     * @return void
430
     */
431 2
    protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void
432
    {
433 2
        if ($replace) {
434
            $jobPoster->job_poster_key_tasks()->delete();
435
        }
436
437 2
        if (!array_key_exists('task', $input) || !is_array($input['task'])) {
438 2
            return;
439
        }
440
441
        foreach ($input['task'] as $task) {
442
            $jobPosterTask = new JobPosterKeyTask();
443
            $jobPosterTask->job_poster_id = $jobPoster->id;
444
            $jobPosterTask->fill(
445
                [
446
                    'en' => [
447
                        'description' => $task['en']
448
                    ],
449
                    'fr' => [
450
                        'description' => $task['fr']
451
                    ]
452
                ]
453
            );
454
            $jobPosterTask->save();
455
        }
456
    }
457
458
    /**
459
     * Fill Job Poster's questions and save
460
     *
461
     * @param mixed[]               $input     Field values.
462
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
463
     * @param boolean               $replace   Remove existing relationships.
464
     *
465
     * @return void
466
     */
467 2
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void
468
    {
469 2
        if ($replace) {
470
            $jobPoster->job_poster_questions()->delete();
471
        }
472
473 2
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
474 2
            return;
475
        }
476
477
        foreach ($input['question'] as $question) {
478
            $jobQuestion = new JobPosterQuestion();
479
            $jobQuestion->job_poster_id = $jobPoster->id;
480
            $jobQuestion->fill(
481
                [
482
                    'en' => [
483
                        'question' => $question['question']['en'],
484
                        'description' => $question['description']['en']
485
                    ],
486
                    'fr' => [
487
                        'question' => $question['question']['fr'],
488
                        'description' => $question['description']['fr']
489
                    ]
490
                ]
491
            );
492
            $jobQuestion->save();
493
        }
494
    }
495
496
    /**
497
     * Fill Job Poster's criteria and save
498
     *
499
     * @param mixed[]               $input     Field values.
500
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
501
     * @param boolean               $replace   Remove existing relationships.
502
     *
503
     * @return void
504
     */
505 2
    protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void
506
    {
507 2
        if ($replace) {
508
            $jobPoster->criteria()->delete();
509
        }
510
511 2
        if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) {
512 2
            return;
513
        }
514
515
        $criteria = $input['criteria'];
516
517
        $combinedCriteria = [];
518
        if (isset($criteria['old'])) {
519
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']);
520
        }
521
        if (isset($criteria['new'])) {
522
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']);
523
        }
524
525
        if (! empty($combinedCriteria)) {
526
            foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) {
527
                foreach ($criteriaTypeInput as $skillType => $skillTypeInput) {
528
                    foreach ($skillTypeInput as $criteriaInput) {
529
                        $criteria = new Criteria();
530
                        $criteria->job_poster_id = $jobPoster->id;
531
                        $criteria->fill(
532
                            [
533
                                'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id,
534
                                'skill_id' => $criteriaInput['skill_id'],
535
                                'skill_level_id' => $criteriaInput['skill_level_id'],
536
                                'en' => [
537
                                    'description' => $criteriaInput['description']['en'],
538
                                ],
539
                                'fr' => [
540
                                    'description' => $criteriaInput['description']['fr'],
541
                                ],
542
                            ]
543
                        );
544
                        $criteria->save();
545
                    }
546
                }
547
            }
548
        }
549
    }
550
551
    /**
552
     * Get the localized default questions and add them to an array.
553
     *
554
     * @return mixed[]|void
555
     */
556 2
    protected function populateDefaultQuestions()
557
    {
558
        $defaultQuestions = [
559 2
            'en' => array_values(Lang::get('manager/job_create', [], 'en')['questions']),
560 2
            'fr' => array_values(Lang::get('manager/job_create', [], 'fr')['questions']),
561
        ];
562
563 2
        if (count($defaultQuestions['en']) !== count($defaultQuestions['fr'])) {
564
            Log::warning('There must be the same number of French and English default questions for a Job Poster.');
565
            return;
566
        }
567
568 2
        $jobQuestions = [];
569
570 2
        for ($i = 0; $i < count($defaultQuestions['en']); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
571 2
            $jobQuestion = new JobPosterQuestion();
572 2
            $jobQuestion->fill(
573
                [
574
                    'en' => [
575 2
                        'question' => $defaultQuestions['en'][$i],
576
                    ],
577
                    'fr' => [
578 2
                        'question' => $defaultQuestions['fr'][$i],
579
                    ]
580
                ]
581
            );
582
            // Workaround for Default Questions with empty descriptions
583
            // throwing an error during save.
584
            // The id isn't actually used during the fillAndSaveJobPosterQuestions
585
            // method call.
586 2
            $jobQuestion->id = $i + 1;
587 2
            $jobQuestions[] = $jobQuestion;
588
        }
589
590 2
        return $jobQuestions;
591
    }
592
}
593