Passed
Push — bugfix/admin-edit-job-changes-... ( df03d6 )
by Tristan
12:10 queued 03:28
created

JobController::populateCreateView()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 102
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 56
CRAP Score 6.0493

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 102
ccs 56
cts 63
cp 0.8889
rs 8.0541
c 0
b 0
f 0
cc 6
nc 9
nop 2
crap 6.0493

How to fix   Long Method   

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 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\Criteria;
20
use App\Models\JobPoster;
21
use App\Models\JobPosterKeyTask;
22
use App\Models\JobPosterQuestion;
23
use App\Models\Lookup\JobTerm;
24
use App\Models\Lookup\Province;
25
use App\Models\Lookup\SecurityClearance;
26
use App\Models\Lookup\LanguageRequirement;
27
use App\Models\Lookup\Department;
28
use App\Models\Lookup\SkillLevel;
29
use App\Models\Lookup\CriteriaType;
30
use App\Models\Skill;
31
use App\Models\Manager;
32
33
use App\Services\Validation\JobPosterValidator;
34
use Jenssegers\Date\Date;
35
36
class JobController extends Controller
37
{
38
    /**
39
     * Display a listing of JobPosters.
40
     *
41
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
42
     */
43
    public function index()
44
    {
45
        $now = Carbon::now();
46
47
        // Find published jobs that are currently open for applications.
48
        // Eager load required relationships: Department, Province, JobTerm.
49
        // Eager load the count of submitted applications, to prevent the relationship
50
        // from being actually loaded and firing off events.
51
        $jobs = JobPoster::where('open_date_time', '<=', $now)
52
            ->where('close_date_time', '>=', $now)
53
            ->where('published', true)
54
            ->with([
55
                'department',
56
                'province',
57
                'job_term',
58
            ])
59
            ->withCount([
60
                'submitted_applications',
61
            ])
62
            ->get();
63
        return view('applicant/job_index', [
64
            'job_index' => Lang::get('applicant/job_index'),
65
            'jobs' => $jobs
66
        ]);
67
    }
68
69
    /**
70
     * Display a listing of a manager's JobPosters.
71
     *
72
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
73
     */
74 1
    public function managerIndex()
75
    {
76 1
        $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...
77 1
        $jobs = JobPoster::where('manager_id', $manager->id)
78 1
            ->withCount('submitted_applications')
79 1
            ->get();
80
81 1
        return view('manager/job_index', [
82
            /*Localization Strings*/
83 1
            'jobs_l10n' => Lang::get('manager/job_index'),
84
85
            /* Data */
86 1
            'jobs' => $jobs,
87
        ]);
88
    }
89
90
    /**
91
     * Submit the Job Poster for review.
92
     *
93
     * @param \Illuminate\Http\Request $request   Incoming request object.
94
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
95
     *
96
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
97
     */
98 1
    public function submitForReview(Request $request, JobPoster $jobPoster)
99
    {
100
        // Update review request timestamp
101 1
        $jobPoster->review_requested_at = new Date();
102 1
        $jobPoster->save();
103
104
        // Refresh model instance with updated DB values.
105 1
        $jobPoster = JobPoster::withCount('submitted_applications')->where('id', $jobPoster->id)->first();
106
107
        // Send email
108 1
        $reviewer_email = config('mail.reviewer_email');
109 1
        if (isset($reviewer_email)) {
110 1
            Mail::to($reviewer_email)->send(new JobPosterReviewRequested($jobPoster, Auth::user()));
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

110
            Mail::to($reviewer_email)->send(new JobPosterReviewRequested($jobPoster, /** @scrutinizer ignore-type */ Auth::user()));
Loading history...
111
        } else {
112
            Log::error('The reviewer email environment variable is not set.');
113
        }
114
115 1
        return view('manager/job_index/job', [
116
            /*Localization Strings*/
117 1
            'jobs_l10n' => Lang::get('manager/job_index'),
118 1
            'job' => $jobPoster
119
        ]);
120
    }
121
122
    /**
123
     * Delete a draft Job Poster.
124
     *
125
     * @param \Illuminate\Http\Request $request   Incoming request object.
126
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
127
     *
128
     * @return void
129
     */
130
    public function destroy(Request $request, JobPoster $jobPoster) : void
131
    {
132
        $jobPoster->delete();
133
    }
134
135
    /**
136
     * Display the specified job poster.
137
     *
138
     * @param \Illuminate\Http\Request $request   Incoming request object.
139
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
140
     *
141
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
142
     */
143 4
    public function show(Request $request, JobPoster $jobPoster)
144
    {
145 4
        $jobPoster->load([
146 4
            'department',
147
            'criteria.skill.skill_type',
148
            'manager.team_culture',
149
            'manager.work_environment'
150
        ]);
151
152 4
        $user = Auth::user();
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
153
154
        //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model
155 4
        $workplacePhotos = [];
156 4
        foreach ($jobPoster->manager->work_environment->workplace_photo_captions as $photoCaption) {
157
            $workplacePhotos[] = [
158
                'description' => $photoCaption->description,
159
                'url' => '/images/user.png'
160
            ];
161
        }
162
163
        //TODO: replace route('manager.show',manager.id) in templates with link using slug
164
165
        $criteria = [
166 4
            'essential' => $jobPoster->criteria->filter(
167
                function ($value, $key) {
168 2
                    return $value->criteria_type->name == 'essential';
169 4
                }
170
            ),
171 4
            'asset' => $jobPoster->criteria->filter(
172
                function ($value, $key) {
173 2
                    return $value->criteria_type->name == 'asset';
174 4
                }
175
            ),
176
        ];
177
178 4
        $jobLang = Lang::get('applicant/job_post');
179
180 4
        $applyButton = [];
181 4
        if (!$jobPoster->published && $this->authorize('update', $jobPoster)) {
182
            $applyButton = [
183 2
                'href' => route('manager.jobs.edit', $jobPoster->id),
184 2
                'title' => $jobLang['apply']['edit_link_title'],
185 2
                'text' => $jobLang['apply']['edit_link_label'],
186
            ];
187 2
        } elseif (Auth::check() && $jobPoster->isOpen()) {
188
            $applyButton = [
189 1
                'href' => route('job.application.edit.1', $jobPoster->id),
190 1
                'title' => $jobLang['apply']['apply_link_title'],
191 1
                'text' => $jobLang['apply']['apply_link_label'],
192
            ];
193 1
        } elseif (Auth::guest() && $jobPoster->isOpen()) {
194
            $applyButton = [
195 1
                'href' => route('job.application.edit.1', $jobPoster->id),
196 1
                'title' => $jobLang['apply']['login_link_title'],
197 1
                'text' => $jobLang['apply']['login_link_label'],
198
            ];
199
        } else {
200
            $applyButton = [
201
                'href' => null,
202
                'title' => null,
203
                'text' => $jobLang['apply']['job_closed_label'],
204
            ];
205
        }
206
207 4
        return view(
208 4
            'applicant/job_post',
209
            [
210 4
                'job_post' => $jobLang,
211 4
                'manager' => $jobPoster->manager,
212 4
                'manager_profile_photo_url' => '/images/user.png', //TODO get real photo
213 4
                'team_culture' => $jobPoster->manager->team_culture,
214 4
                'work_environment' => $jobPoster->manager->work_environment,
215 4
                'workplace_photos' => $workplacePhotos,
216 4
                'job' => $jobPoster,
217 4
                'criteria' => $criteria,
218 4
                'apply_button' => $applyButton,
219 4
                'skill_template' => Lang::get('common/skills'),
220
            ]
221
        );
222
    }
223
224
    /**
225
     * Create a blank job poster for the specified manager
226
     *
227
     * @param Manager $manager
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
228
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
229
     */
230 1
    public function createAsManager(Manager $manager)
231
    {
232 1
        $jobPoster = new JobPoster();
233 1
        $jobPoster->manager_id = $manager->id;
234 1
        $managerEn = $manager->translate('en');
235 1
        $managerFr = $manager->translate('fr');
236 1
        $jobPoster->fill([
237 1
            'department_id' => $manager->department_id,
238
            'en' => [
239 1
                'branch' => $managerEn->branch,
240 1
                'division' => $managerEn->division,
241
            ],
242
            'fr' => [
243 1
                'branch' => $managerFr->branch,
244 1
                'division' => $managerFr->division,
245
            ]
246
        ]);
247 1
        $jobPoster->save();
248 1
        return redirect()->route('manager.jobs.edit', $jobPoster->id);
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect()->route....edit', $jobPoster->id) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Contracts\Vie...ry|Illuminate\View\View.
Loading history...
249
    }
250
251
    /**
252
     * Display the form for creating a new Job Poster
253
     *
254
     * @param \Illuminate\Http\Request $request Incoming request object.
255
     *
256
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
257
     */
258
    public function create(Request $request)
259
    {
260
        return $this->populateCreateView($request);
261
    }
262
263
    /**
264
     * Display the form for editing an existing Job Poster
265
     *
266
     * @param \Illuminate\Http\Request $request   Incoming request object.
267
     * @param \App\Models\JobPoster    $jobPoster Job Poster object.
268
     *
269
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
270
     */
271 1
    public function edit(Request $request, JobPoster $jobPoster)
272
    {
273 1
        return $this->populateCreateView($request, $jobPoster);
274
    }
275
276
    /**
277
     * Get the manager from the request object and check if creating or editing
278
     *
279
     * @param \Illuminate\Http\Request $request   Incoming request object.
280
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object.
281
     *
282
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory Job Create view
283
     */
284 1
    public function populateCreateView(Request $request, JobPoster $jobPoster = null)
285
    {
286 1
        if ($jobPoster == null || $jobPoster->manager == null) {
287
            $manager = $request->user() ? $request->user()->manager : null;
288
        } else {
289 1
            $manager = $jobPoster->manager;
290
        }
291
292 1
        if (isset($jobPoster)) {
293 1
            $job = $jobPoster;
294 1
            $route = ['manager.jobs.update', $jobPoster];
295 1
            $jobHeading = 'manager/job_edit';
296
        } else {
297
            $job = [];
298
            $defaultQuestions = $this->populateDefaultQuestions();
299
            if (!empty($defaultQuestions)) {
300
                $job['job_poster_questions'] = $defaultQuestions;
301
            }
302
            $route = ['manager.jobs.store'];
303
            $jobHeading = 'manager/job_create';
304
        }
305
306 1
        $skillLangs = Lang::get('common/skills');
307
308 1
        $softSkills = Skill::whereHas(
309 1
            'skill_type',
310
            function ($query) : void {
311 1
                $query->where('name', '=', 'soft');
312 1
            }
313 1
        )->get()
314 1
            ->mapWithKeys(
315
                function ($skill) {
316
                    return [
317 1
                        $skill->id => $skill->name
318
                    ];
319 1
                }
320
            )
321 1
            ->all();
322
323 1
        $hardSkills = Skill::whereHas(
324 1
            'skill_type',
325
            function ($query) : void {
326 1
                $query->where('name', '=', 'hard');
327 1
            }
328 1
        )->get()
329 1
            ->mapWithKeys(
330
                function ($skill) {
331
                    return [
332 1
                        $skill->id => $skill->name
333
                    ];
334 1
                }
335
            )
336 1
            ->all();
337
338 1
        asort($softSkills, SORT_LOCALE_STRING);
339 1
        asort($hardSkills, SORT_LOCALE_STRING);
340
341
        $skills = [
342
            'essential' => [
343 1
                'hard' => $hardSkills,
344 1
                'soft' => $softSkills
345
            ],
346
            'asset' => [
347 1
                'hard' => $hardSkills,
348 1
                'soft' => $softSkills
349
            ]
350
        ];
351
352 1
        $skillLevelCollection = SkillLevel::all();
353
354 1
        $skillLevels = array();
355
356 1
        $skillLevels['hard'] = $skillLevelCollection->mapWithKeys(
357
            function ($skillLevel) use ($skillLangs) {
358 1
                return [$skillLevel->id => $skillLangs['skill_levels']['hard'][$skillLevel->name]];
359 1
            }
360 1
        )->all();
361
362 1
        $skillLevels['soft'] = $skillLevelCollection->mapWithKeys(
363
            function ($skillLevel) use ($skillLangs) {
364 1
                return [$skillLevel->id => $skillLangs['skill_levels']['soft'][$skillLevel->name]];
365 1
            }
366 1
        )->all();
367
368 1
        return view(
369 1
            'manager/job_create',
370
            [
371
                /*Localization Strings*/
372 1
                'job_l10n' => Lang::get('manager/job_create'),
373
374
                /* Data */
375 1
                'job' => Lang::get($jobHeading),
376 1
                'manager' => $manager,
377 1
                'provinces' => Province::all(),
378 1
                'departments' => Department::all(),
379 1
                'language_requirments' => LanguageRequirement::all(),
380 1
                'security_clearances' => SecurityClearance::all(),
381 1
                'job' => $job,
382 1
                '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

382
                'form_action_url' => route(/** @scrutinizer ignore-type */ /** @scrutinizer ignore-type */ ...$route), // phpcs:ignore
Loading history...
383 1
                'skills' => $skills,
384 1
                'skill_levels' => $skillLevels,
385 1
                'skill_template' => $skillLangs,
386
            ]
387
        );
388
    }
389
390
    /**
391
     * Create a new resource in storage
392
     *
393
     * @param \Illuminate\Http\Request $request   Incoming request object.
394
     * @param \App\Models\JobPoster    $jobPoster Optional Job Poster object.
395
     *
396
     * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse A redirect to the Job Index
397
     */
398 3
    public function store(Request $request, JobPoster $jobPoster = null)
399
    {
400
        // Don't allow edits for published Job Posters
401
        // Also check auth while we're at it
402 3
        if (isset($jobPoster)) {
403 3
            $this->authorize('update', $jobPoster);
404 3
            JobPosterValidator::validateUnpublished($jobPoster);
405
        } else {
406
            $this->authorize('create', JobPoster::class);
407
        }
408
409 3
        $input = $request->input();
410
411 3
        $job = (isset($jobPoster) ? $jobPoster : new JobPoster());
412
413 3
        if ($job->manager_id == null) {
414
            $job->manager_id = $request->user()->manager->id;
415
            $job->save();
416
        }
417
418 3
        $this->fillAndSaveJobPoster($input, $job);
419
420 3
        $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster));
421
422 3
        $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster));
423
424 3
        $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster));
425
426 3
        return redirect(route('manager.jobs.show', $job->id));
427
    }
428
429
    /**
430
     * Fill Job Poster model's properties and save
431
     *
432
     * @param mixed[]               $input     Field values.
433
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
434
     *
435
     * @return void
436
     */
437 3
    protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void
438
    {
439 3
        $closeDate = new Date($input['close_date']);
440 3
        $openDate = new Date($input['open_date']);
441 3
        $startDate = new Date($input['start_date']);
442
443 3
        $jobPoster->fill(
444
            [
445 3
                'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id,
446 3
                'term_qty' => $input['term_qty'],
447 3
                'open_date_time' => pstDayStartToUtcTime($openDate->year, $openDate->month, $openDate->day),
448 3
                'close_date_time' => pstDayEndToUtcTime($closeDate->year, $closeDate->month, $closeDate->day),
449 3
                'start_date_time' => pstDayStartToUtcTime($startDate->year, $startDate->month, $startDate->day),
450 3
                'department_id' => $input['department'],
451 3
                'province_id' => $input['province'],
452 3
                'salary_min' => $input['salary_min'],
453 3
                'salary_max' => $input['salary_max'],
454 3
                'noc' => $input['noc'],
455 3
                'classification' => $input['classification'],
456 3
                'security_clearance_id' => $input['security_clearance'],
457 3
                'language_requirement_id' => $input['language_requirement'],
458 3
                'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false),
459
                'en' => [
460 3
                    'city' => $input['city'],
461 3
                    'title' => $input['title']['en'],
462 3
                    'impact' => $input['impact']['en'],
463 3
                    'branch' => $input['branch']['en'],
464 3
                    'division' => $input['division']['en'],
465 3
                    'education' => $input['education']['en'],
466
                ],
467
                'fr' => [
468 3
                    'city' => $input['city'],
469 3
                    'title' => $input['title']['fr'],
470 3
                    'impact' => $input['impact']['fr'],
471 3
                    'branch' => $input['branch']['fr'],
472 3
                    'division' => $input['division']['fr'],
473 3
                    'education' => $input['education']['fr'],
474
                ],
475
            ]
476
        );
477 3
        $jobPoster->save();
478 3
    }
479
480
    /**
481
     * Fill Job Poster's tasks and save
482
     *
483
     * @param mixed[]               $input     Field values.
484
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
485
     * @param boolean               $replace   Remove existing relationships.
486
     *
487
     * @return void
488
     */
489 3
    protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void
490
    {
491 3
        if ($replace) {
492 3
            $jobPoster->job_poster_key_tasks()->delete();
493
        }
494
495 3
        if (!array_key_exists('task', $input) || !is_array($input['task'])) {
496 3
            return;
497
        }
498
499
        foreach ($input['task'] as $task) {
500
            $jobPosterTask = new JobPosterKeyTask();
501
            $jobPosterTask->job_poster_id = $jobPoster->id;
502
            $jobPosterTask->fill(
503
                [
504
                    'en' => [
505
                        'description' => $task['en']
506
                    ],
507
                    'fr' => [
508
                        'description' => $task['fr']
509
                    ]
510
                ]
511
            );
512
            $jobPosterTask->save();
513
        }
514
    }
515
516
    /**
517
     * Fill Job Poster's questions and save
518
     *
519
     * @param mixed[]               $input     Field values.
520
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
521
     * @param boolean               $replace   Remove existing relationships.
522
     *
523
     * @return void
524
     */
525 3
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void
526
    {
527 3
        if ($replace) {
528 3
            $jobPoster->job_poster_questions()->delete();
529
        }
530
531 3
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
532 3
            return;
533
        }
534
535
        foreach ($input['question'] as $question) {
536
            $jobQuestion = new JobPosterQuestion();
537
            $jobQuestion->job_poster_id = $jobPoster->id;
538
            $jobQuestion->fill(
539
                [
540
                    'en' => [
541
                        'question' => $question['question']['en'],
542
                        'description' => $question['description']['en']
543
                    ],
544
                    'fr' => [
545
                        'question' => $question['question']['fr'],
546
                        'description' => $question['description']['fr']
547
                    ]
548
                ]
549
            );
550
            $jobQuestion->save();
551
        }
552
    }
553
554
    /**
555
     * Fill Job Poster's criteria and save
556
     *
557
     * @param mixed[]               $input     Field values.
558
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
559
     * @param boolean               $replace   Remove existing relationships.
560
     *
561
     * @return void
562
     */
563 3
    protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void
564
    {
565 3
        if ($replace) {
566 3
            $jobPoster->criteria()->delete();
567
        }
568
569 3
        if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) {
570 3
            return;
571
        }
572
573
        $criteria = $input['criteria'];
574
575
        $combinedCriteria = [];
576
        if (isset($criteria['old'])) {
577
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']);
578
        }
579
        if (isset($criteria['new'])) {
580
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']);
581
        }
582
583
        if (! empty($combinedCriteria)) {
584
            foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) {
585
                foreach ($criteriaTypeInput as $skillType => $skillTypeInput) {
586
                    foreach ($skillTypeInput as $criteriaInput) {
587
                        $criteria = new Criteria();
588
                        $criteria->job_poster_id = $jobPoster->id;
589
                        $criteria->fill(
590
                            [
591
                                'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id,
592
                                'skill_id' => $criteriaInput['skill_id'],
593
                                'skill_level_id' => $criteriaInput['skill_level_id'],
594
                                'en' => [
595
                                    'description' => $criteriaInput['description']['en'],
596
                                ],
597
                                'fr' => [
598
                                    'description' => $criteriaInput['description']['fr'],
599
                                ],
600
                            ]
601
                        );
602
                        $criteria->save();
603
                    }
604
                }
605
            }
606
        }
607
    }
608
609
    /**
610
     * Get the localized default questions and add them to an array.
611
     *
612
     * @return mixed[]|void
613
     */
614
    protected function populateDefaultQuestions()
615
    {
616
        $defaultQuestions = [
617
            'en' => array_values(Lang::get('manager/job_create', [], 'en')['questions']),
618
            'fr' => array_values(Lang::get('manager/job_create', [], 'fr')['questions']),
619
        ];
620
621
        if (count($defaultQuestions['en']) !== count($defaultQuestions['fr'])) {
622
            Log::warning('There must be the same number of French and English default questions for a Job Poster.');
623
            return;
624
        }
625
626
        $jobQuestions = [];
627
628
        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...
629
            $jobQuestion = new JobPosterQuestion();
630
            $jobQuestion->fill(
631
                [
632
                    'en' => [
633
                        'question' => $defaultQuestions['en'][$i],
634
                    ],
635
                    'fr' => [
636
                        'question' => $defaultQuestions['fr'][$i],
637
                    ]
638
                ]
639
            );
640
            // Workaround for Default Questions with empty descriptions
641
            // throwing an error during save.
642
            // The id isn't actually used during the fillAndSaveJobPosterQuestions
643
            // method call.
644
            $jobQuestion->id = $i + 1;
645
            $jobQuestions[] = $jobQuestion;
646
        }
647
648
        return $jobQuestions;
649
    }
650
}
651