Completed
Push — feature/manager-form-validatio... ( ce5185...6a406e )
by Grant
13:31 queued 06:50
created

JobController::store()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 60
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.2742

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 60
ccs 14
cts 18
cp 0.7778
rs 8.9368
c 0
b 0
f 0
cc 5
nc 16
nop 2
crap 5.2742

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
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 6
    public function show(Request $request, JobPoster $jobPoster)
106
    {
107 6
        $user = Auth::user();
108
109
        //TODO: Improve workplace photos, and reference them in template direction from WorkEnvironment model
110 6
        $workplacePhotos = [];
111 6
        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 6
            'essential' => $jobPoster->criteria->filter(
122
                function ($value, $key) {
123 4
                    return $value->criteria_type->name == 'essential';
124 6
                }
125
            ),
126 6
            'asset' => $jobPoster->criteria->filter(
127
                function ($value, $key) {
128 4
                    return $value->criteria_type->name == 'asset';
129 6
                }
130
            ),
131
        ];
132
133 6
        $jobLang = Lang::get('applicant/job_post');
134
135 6
        $applyButton = [];
136
137 6
        if (isset($user)) {
138 4
            if (!$jobPoster->published && $this->authorize('update', $jobPoster)) {
139
                $applyButton = [
140 2
                    'href' => route('manager.jobs.edit', $jobPoster->id),
141 2
                    'title' => $jobLang['apply']['edit_link_title'],
142 2
                    '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 4
                    '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 6
        return view(
160 6
            'applicant/job_post',
161
            [
162 6
                'job_post' => $jobLang,
163 6
                'manager' => $jobPoster->manager,
164 6
                'manager_profile_photo_url' => '/images/user.png', //TODO get real photo
165 6
                'team_culture' => $jobPoster->manager->team_culture,
166 6
                'work_environment' => $jobPoster->manager->work_environment,
167 6
                'workplace_photos' => $workplacePhotos,
168 6
                'job' => $jobPoster,
169 6
                'criteria' => $criteria,
170 6
                'apply_button' => $applyButton,
171 6
                '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
        // Don't allow edits for published Job Posters
317
        // Also check auth while we're at it
318 2
        if (isset($jobPoster)) {
319
            $this->authorize('update', $jobPoster);
320
            JobPosterValidator::validateUnpublished($jobPoster);
321
        } else {
322 2
            $this->authorize('create', JobPoster::class);
323
        }
324
325 2
        $input = $request->input();
326
327 2
        $job = (isset($jobPoster) ? $jobPoster : new JobPoster());
328
329 2
        $job->manager_id = $request->user()->manager->id;
330 2
        $job->published = ($input['submit'] == 'publish');
331
332 2
        if ($job->published) {
333
            // Job Poster validation rules
334
            $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...
335
                'title.en' => 'required',
336
                'title.fr' => 'required',
337
                'salary_min' => 'required|integer|digits_between:4,6',
338
                'salary_max' => 'required|integer|digits_between:4,6',
339
                'classification' => 'required|alpha_dash|between:4,5',
340
                'noc' => 'required|integer|digits:4',
341
                'security_clearance' => 'required|integer|between:1,3',
342
                'language_requirement' => 'required|integer|between:1,4',
343
                'city' => 'required',
344
                'province' => 'required|integer|between:1,13',
345
                'open_date' => 'required|date|after:tomorrow',
346
                'open_time' => 'required',
347
                'close_date' => 'required|date|after:open_date',
348
                'close_time' => 'required',
349
                'start_date_time' => 'required|date|after:close_date',
350
                'term_qty' => 'required|digits_between:1,2',
351
                'department' => 'required|integer|between:1,10',
352
                'branch.en' => 'required',
353
                'branch.fr' => 'required',
354
                'divison.en' => 'required',
355
                'divison.fr' => 'required',
356
                'impact.en' => 'required',
357
                'impact.fr' => 'required',
358
                'education.en' => 'required',
359
                'education.fr' => 'required',
360
            ]);
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...
361
        }
362
363 2
        $this->fillAndSaveJobPoster($input, $job);
364
365 2
        $this->fillAndSaveJobPosterTasks($input, $job, isset($jobPoster));
366
367 2
        $this->fillAndSaveJobPosterQuestions($input, $job, isset($jobPoster));
368
369 2
        $this->fillAndSaveJobPosterCriteria($input, $job, isset($jobPoster));
370
371 2
        $route = $job->published ? route('manager.jobs.index') : route('manager.jobs.show', $job->id);
372
373 2
        return redirect($route);
374
    }
375
376
    /**
377
     * Fill Job Poster model's properties and save
378
     *
379
     * @param mixed[]               $input     Field values.
380
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
381
     *
382
     * @return void
383
     */
384 2
    protected function fillAndSaveJobPoster(array $input, JobPoster $jobPoster) : void
385
    {
386 2
        $jobPoster->fill(
387
            [
388 2
                'job_term_id' => JobTerm::where('name', 'month')->firstOrFail()->id,
389 2
                'term_qty' => $input['term_qty'],
390 2
                'open_date_time' => new Date($input['open_date'] . $input['open_time']),
391 2
                'close_date_time' => new Date($input['close_date'] . $input['close_time']),
392 2
                'start_date_time' => new Date($input['start_date_time']),
393 2
                'department_id' => $input['department'],
394 2
                'province_id' => $input['province'],
395 2
                'salary_min' => $input['salary_min'],
396 2
                'salary_max' => $input['salary_max'],
397 2
                'noc' => $input['noc'],
398 2
                'classification' => $input['classification'],
399 2
                'security_clearance_id' => $input['security_clearance'],
400 2
                'language_requirement_id' => $input['language_requirement'],
401 2
                'remote_work_allowed' => (isset($input['remote_work_allowed']) ? $input['remote_work_allowed'] : false),
402
                'en' => [
403 2
                    'city' => $input['city'],
404 2
                    'title' => $input['title']['en'],
405 2
                    'impact' => $input['impact']['en'],
406 2
                    'branch' => $input['branch']['en'],
407 2
                    'division' => $input['division']['en'],
408 2
                    'education' => $input['education']['en'],
409
                ],
410
                'fr' => [
411 2
                    'city' => $input['city'],
412 2
                    'title' => $input['title']['fr'],
413 2
                    'impact' => $input['impact']['fr'],
414 2
                    'branch' => $input['branch']['fr'],
415 2
                    'division' => $input['division']['fr'],
416 2
                    'education' => $input['education']['fr'],
417
                ],
418
            ]
419
        );
420 2
        $jobPoster->save();
421 2
    }
422
423
    /**
424
     * Fill Job Poster's tasks and save
425
     *
426
     * @param mixed[]               $input     Field values.
427
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
428
     * @param boolean               $replace   Remove existing relationships.
429
     *
430
     * @return void
431
     */
432 2
    protected function fillAndSaveJobPosterTasks(array $input, JobPoster $jobPoster, bool $replace) : void
433
    {
434 2
        if ($replace) {
435
            $jobPoster->job_poster_key_tasks()->delete();
436
        }
437
438 2
        if (!array_key_exists('task', $input) || !is_array($input['task'])) {
439 2
            return;
440
        }
441
442
        foreach ($input['task'] as $task) {
443
            $jobPosterTask = new JobPosterKeyTask();
444
            $jobPosterTask->job_poster_id = $jobPoster->id;
445
            $jobPosterTask->fill(
446
                [
447
                    'en' => [
448
                        'description' => $task['en']
449
                    ],
450
                    'fr' => [
451
                        'description' => $task['fr']
452
                    ]
453
                ]
454
            );
455
            $jobPosterTask->save();
456
        }
457
    }
458
459
    /**
460
     * Fill Job Poster's questions and save
461
     *
462
     * @param mixed[]               $input     Field values.
463
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
464
     * @param boolean               $replace   Remove existing relationships.
465
     *
466
     * @return void
467
     */
468 2
    protected function fillAndSaveJobPosterQuestions(array $input, JobPoster $jobPoster, bool $replace) : void
469
    {
470 2
        if ($replace) {
471
            $jobPoster->job_poster_questions()->delete();
472
        }
473
474 2
        if (!array_key_exists('question', $input) || !is_array($input['question'])) {
475 2
            return;
476
        }
477
478
        foreach ($input['question'] as $question) {
479
            $jobQuestion = new JobPosterQuestion();
480
            $jobQuestion->job_poster_id = $jobPoster->id;
481
            $jobQuestion->fill(
482
                [
483
                    'en' => [
484
                        'question' => $question['question']['en'],
485
                        'description' => $question['description']['en']
486
                    ],
487
                    'fr' => [
488
                        'question' => $question['question']['fr'],
489
                        'description' => $question['description']['fr']
490
                    ]
491
                ]
492
            );
493
            $jobQuestion->save();
494
        }
495
    }
496
497
    /**
498
     * Fill Job Poster's criteria and save
499
     *
500
     * @param mixed[]               $input     Field values.
501
     * @param \App\Models\JobPoster $jobPoster Job Poster object.
502
     * @param boolean               $replace   Remove existing relationships.
503
     *
504
     * @return void
505
     */
506 2
    protected function fillAndSaveJobPosterCriteria(array $input, JobPoster $jobPoster, bool $replace) : void
507
    {
508 2
        if ($replace) {
509
            $jobPoster->criteria()->delete();
510
        }
511
512 2
        if (!array_key_exists('criteria', $input) || !is_array($input['criteria'])) {
513 2
            return;
514
        }
515
516
        $criteria = $input['criteria'];
517
518
        $combinedCriteria = [];
519
        if (isset($criteria['old'])) {
520
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['old']);
521
        }
522
        if (isset($criteria['new'])) {
523
            $combinedCriteria = array_replace_recursive($combinedCriteria, $criteria['new']);
524
        }
525
526
        if (! empty($combinedCriteria)) {
527
            foreach ($combinedCriteria as $criteriaType => $criteriaTypeInput) {
528
                foreach ($criteriaTypeInput as $skillType => $skillTypeInput) {
529
                    foreach ($skillTypeInput as $criteriaInput) {
530
                        $criteria = new Criteria();
531
                        $criteria->job_poster_id = $jobPoster->id;
532
                        $criteria->fill(
533
                            [
534
                                'criteria_type_id' => CriteriaType::where('name', $criteriaType)->firstOrFail()->id,
535
                                'skill_id' => $criteriaInput['skill_id'],
536
                                'skill_level_id' => $criteriaInput['skill_level_id'],
537
                                'en' => [
538
                                    'description' => $criteriaInput['description']['en'],
539
                                ],
540
                                'fr' => [
541
                                    'description' => $criteriaInput['description']['fr'],
542
                                ],
543
                            ]
544
                        );
545
                        $criteria->save();
546
                    }
547
                }
548
            }
549
        }
550
    }
551
552
    /**
553
     * Get the localized default questions and add them to an array.
554
     *
555
     * @return mixed[]|void
556
     */
557 2
    protected function populateDefaultQuestions()
558
    {
559
        $defaultQuestions = [
560 2
            'en' => array_values(Lang::get('manager/job_create', [], 'en')['questions']),
561 2
            'fr' => array_values(Lang::get('manager/job_create', [], 'fr')['questions']),
562
        ];
563
564 2
        if (count($defaultQuestions['en']) !== count($defaultQuestions['fr'])) {
565
            Log::warning('There must be the same number of French and English default questions for a Job Poster.');
566
            return;
567
        }
568
569 2
        $jobQuestions = [];
570
571 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...
572 2
            $jobQuestion = new JobPosterQuestion();
573 2
            $jobQuestion->fill(
574
                [
575
                    'en' => [
576 2
                        'question' => $defaultQuestions['en'][$i],
577
                    ],
578
                    'fr' => [
579 2
                        'question' => $defaultQuestions['fr'][$i],
580
                    ]
581
                ]
582
            );
583
            // Workaround for Default Questions with empty descriptions
584
            // throwing an error during save.
585
            // The id isn't actually used during the fillAndSaveJobPosterQuestions
586
            // method call.
587 2
            $jobQuestion->id = $i + 1;
588 2
            $jobQuestions[] = $jobQuestion;
589
        }
590
591 2
        return $jobQuestions;
592
    }
593
}
594