Passed
Push — feature/manager-form-validatio... ( 817475 )
by Grant
18:10 queued 07:45
created

JobController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 10
cp 0
cc 1
nc 1
nop 0
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\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\Response;
11
use Illuminate\View\View;
12
use App\Http\Controllers\Controller;
13
14
use Carbon\Carbon;
15
16
use App\Models\JobPoster;
17
use App\Models\JobPosterQuestion;
18
use App\Models\Lookup\JobTerm;
19
use App\Models\Lookup\Province;
20
use App\Models\Lookup\SecurityClearance;
21
use App\Models\Lookup\LanguageRequirement;
22
use App\Models\Lookup\CitizenshipDeclaration;
23
use App\Models\Lookup\Department;
24
use App\Models\Lookup\SkillLevel;
25
use App\Models\Lookup\CriteriaType;
26
use App\Models\Lookup\VeteranStatus;
27
use App\Models\JobApplication;
28
use App\Models\Criteria;
29
use App\Models\Skill;
30
use App\Models\JobPosterKeyTask;
31
32
use App\Services\Validation\JobPosterValidator;
33
use Jenssegers\Date\Date;
34
35
class JobController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class JobController
Loading history...
36
{
37
    /**
38
     * Display a listing of JobPosters.
39
     *
40
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
41
     */
42
    public function index()
43
    {
44
        $now = Carbon::now();
45
46
        //Find published jobs that are currently open for applications
47
        $jobs = JobPoster::where('open_date_time', '<=', $now)
48
            ->where('close_date_time', '>=', $now)
49
            ->where('published', true)
50
            ->get();
51
        $jobs->load('manager.work_environment');
52
        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...
53
            'job_index' => Lang::get('applicant/job_index'),
54
            'jobs' => $jobs
55
        ]);
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...
56
    }
57
58
    /**
59
     * Display a listing of a manager's JobPosters.
60
     *
61
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
62
     */
63 2
    public function managerIndex()
64
    {
65 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...
66
67 2
        $veteran_applications = [];
68 2
        $citizen_applications = [];
69 2
        $other_applications = [];
70
71 2
        foreach ($manager->job_posters as $job) {
72 2
            $job->submitted_applications->load(['veteran_status', 'citizenship_declaration']);
73
            $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...
74
                return $application->veteran_status->name !== "none" &&
75
                    $application->citizenship_declaration->name === "citizen";
76 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...
77
            $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...
78
                return $application->veteran_status->name === "none" &&
79
                    $application->citizenship_declaration->name === "citizen";
80 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...
81
            $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...
82
                return $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
        }
85
86 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...
87 2
            "manager_job_index" => [
88
                "title" => "My Job Posts"
89
            ],
90 2
            'jobs' => $manager->job_posters,
91 2
            'veteran_applications' => $veteran_applications,
92 2
            'citizen_applications' => $citizen_applications,
93 2
            'other_applications' => $other_applications,
94
        ]);
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...
95
    }
96
97
    /**
98
     * Display the specified job poster.
99
     *
100
     * @param \Illuminate\Http\Request $request   Incoming request object.
101
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
102
     *
103
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
104
     */
105 4
    public function show(Request $request, JobPoster $jobPoster)
106
    {
107 4
        $user = Auth::user();
108
109
        //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model
110 4
        $workplacePhotos = [];
111 4
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
112
            $workplacePhotos[] = [
113
                'description' => $photoCaption->description,
114
                'url' => '/images/user.png'
115
            ];
116
        }
117
118
        //TODO: replace route('manager.show',manager.id) in templates with link using slug
119
120
        $criteria = [
121 4
            'essential' => $jobPoster->criteria->filter(
122
                function ($value, $key) {
123 4
                    return $value->criteria_type->name == 'essential';
124 4
                }
125
            ),
126 4
            'asset' => $jobPoster->criteria->filter(
127
                function ($value, $key) {
128 4
                    return $value->criteria_type->name == 'asset';
129 4
                }
130
            ),
131
        ];
132
133 4
        $jobLang = Lang::get('applicant/job_post');
134
135 4
        $applyButton = [];
136
137 4
        if (isset($user)) {
138 2
            if (!$jobPoster->published && $this->authorize('update', $jobPoster)) {
139
                $applyButton = [
140
                    'href' => route('manager.jobs.edit', $jobPoster->id),
141
                    'title' => $jobLang['apply']['edit_link_title'],
142
                    'text' => $jobLang['apply']['edit_link_label'],
143
                ];
144
            } else {
145
                $applyButton = [
146 2
                    'href' => route('job.application.edit.1', $jobPoster->id),
147 2
                    'title' => $jobLang['apply']['apply_link_title'],
148 2
                    'text' => $jobLang['apply']['apply_link_label'],
149
                ];
150
            }
151
        } else {
152
            $applyButton = [
153 2
                'href' => route('job.application.edit.1', $jobPoster->id),
154 2
                'title' => $jobLang['apply']['login_link_title'],
155 2
                'text' => $jobLang['apply']['login_link_label'],
156
            ];
157
        }
158
159 4
        return view(
160 4
            'applicant/job_post',
161
            [
162 4
                'job_post' => $jobLang,
163 4
                'manager' => $jobPoster->manager,
164 4
                'manager_profile_photo_url' => '/images/user.png', //TODO get real photo
165 4
                'team_culture' => $jobPoster->manager->team_culture,
166 4
                'work_environment' => $jobPoster->manager->work_environment,
167 4
                'workplace_photos' => $workplacePhotos,
168 4
                'job' => $jobPoster,
169 4
                'criteria' => $criteria,
170 4
                'apply_button' => $applyButton,
171 4
                'skill_template' => Lang::get('common/skills'),
172
            ]
173
        );
174
    }
175
176
    /**
177
     * Display the form for creating a new Job Poster
178
     *
179
     * @param \Illuminate\Http\Request $request Incoming request object.
180
     *
181
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
182
     */
183 2
    public function create(Request $request)
184
    {
185 2
        return $this->populateCreateView($request);
186
    }
187
188
    /**
189
     * Display the form for editing an existing Job Poster
190
     *
191
     * @param \Illuminate\Http\Request $request   Incoming request object.
192
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
193
     *
194
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
195
     */
196 2
    public function edit(Request $request, JobPoster $jobPoster)
197
    {
198 2
        return $this->populateCreateView($request, $jobPoster);
199
    }
200
201
    /**
202
     * Get the manager from the request object and check if creating or editing
203
     *
204
     * @param \Illuminate\Http\Request $request   Incoming request object.
205
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object.
206
     *
207
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
208
     */
209 4
    public function populateCreateView(Request $request, JobPoster $jobPoster = null)
210
    {
211 4
        $manager = $request->user() ? $request->user()->manager : null;
212 4
        if (isset($jobPoster)) {
213 2
            $job = $jobPoster;
214 2
            $route = ['manager.jobs.store', $jobPoster];
215 2
            $jobHeading = 'manager/job_edit';
216
        } else {
217 2
            $job = [];
218 2
            $defaultQuestions = $this->populateDefaultQuestions();
219 2
            if (!empty($defaultQuestions)) {
220 2
                $job['job_poster_questions'] = $defaultQuestions;
221
            }
222 2
            $route = ['manager.jobs.store'];
223 2
            $jobHeading = 'manager/job_create';
224
        }
225
226 4
        $skillLangs = Lang::get('common/skills');
227
228 4
        $softSkills = Skill::whereHas(
229 4
            'skill_type',
230
            function ($query) : void {
231 4
                $query->where('name', '=', 'soft');
232 4
            }
233 4
        )->get()
234 4
            ->mapWithKeys(
235
                function ($skill) use ($skillLangs) {
236
                    return [
237 4
                        $skill->id => $skillLangs['skills'][$skill->name]['name']
238
                    ];
239 4
                }
240
            )
241 4
            ->all();
242
243 4
        $hardSkills = Skill::whereHas(
244 4
            'skill_type',
245
            function ($query) : void {
246 4
                $query->where('name', '=', 'hard');
247 4
            }
248 4
        )->get()
249 4
            ->mapWithKeys(
250
                function ($skill) use ($skillLangs) {
251
                    return [
252 4
                        $skill->id => $skillLangs['skills'][$skill->name]['name']
253
                    ];
254 4
                }
255
            )
256 4
            ->all();
257
258 4
        asort($softSkills, SORT_LOCALE_STRING);
259 4
        asort($hardSkills, SORT_LOCALE_STRING);
260
261
        $skills = [
262
            'essential' => [
263 4
                'hard' => $hardSkills,
264 4
                'soft' => $softSkills
265
            ],
266
            'asset' => [
267 4
                'hard' => $hardSkills,
268 4
                'soft' => $softSkills
269
            ]
270
        ];
271
272 4
        $skillLevelCollection = SkillLevel::all();
273
274 4
        $skillLevels = array();
275
276 4
        $skillLevels['hard'] = $skillLevelCollection->mapWithKeys(
277
            function ($skillLevel) use ($skillLangs) {
278 4
                return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]];
279 4
            }
280 4
        )->all();
281
282 4
        $skillLevels['soft'] = $skillLevelCollection->mapWithKeys(
283
            function ($skillLevel) use ($skillLangs) {
284 4
                return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]];
285 4
            }
286 4
        )->all();
287
288 4
        return view(
289 4
            'manager/job_create',
290
            [
291 4
                'job_heading' => Lang::get($jobHeading),
292 4
                'manager' => $manager,
293 4
                'provinces' => Province::all(),
294 4
                'departments' => Department::all(),
295 4
                'language_requirments' => LanguageRequirement::all(),
296 4
                'security_clearances' => SecurityClearance::all(),
297 4
                'job' => $job,
298 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

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