Passed
Push — task/update-npm ( 00bdcc...33d0cb )
by Tristan
28:58 queued 13:36
created

JobController::submitForReview()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
ccs 6
cts 9
cp 0.6667
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.1481
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 2
     *
64
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
65 2
     */
66
    public function managerIndex()
67 2
    {
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 2
70
        $veteran_applications = [];
71 2
        $citizen_applications = [];
72 2
        $other_applications = [];
73
74
        foreach ($manager->job_posters as $job) {
75
            $job->submitted_applications->load(['veteran_status', 'citizenship_declaration']);
76 2
            $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
            });
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 2
            $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 2
89
        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 2
93 2
            /* Data */
94 2
            'jobs' => $manager->job_posters,
95
            'veteran_applications' => $veteran_applications,
96
            'citizen_applications' => $citizen_applications,
97
            '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 6
     *
107
     * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
108 6
     */
109
    public function submitForReview(Request $request, JobPoster $jobPoster)
110
    {
111 6
        // Update review request timestamp
112 6
        $jobPoster->review_requested_at = new Date();
113
        $jobPoster->save();
114
        $jobPoster->refresh();
115
116
        // Send email
117
        $reviewer_email = config('mail.reviewer_email');
118
        if (isset($reviewer_email)) {
119
            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

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

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