Passed
Push — feature/job-builder/step-0 ( 323da6...a76dc5 )
by Tristan
15:33 queued 09:48
created

ApplicationByJobController::updateExperience()   F

Complexity

Conditions 20
Paths 320

Size

Total Lines 167
Code Lines 114

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 420

Importance

Changes 0
Metric Value
eloc 114
dl 0
loc 167
ccs 0
cts 95
cp 0
rs 1.4665
c 0
b 0
f 0
cc 20
nc 320
nop 2
crap 420

How to fix   Long Method    Complexity   

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\Http\Request;
7
use App\Models\Lookup\ApplicationStatus;
8
use App\Models\Lookup\VeteranStatus;
9
use App\Models\Lookup\PreferredLanguage;
10
use App\Models\Lookup\CitizenshipDeclaration;
11
use App\Models\JobPoster;
12
use App\Models\JobApplication;
13
use App\Models\JobApplicationAnswer;
14
use App\Models\SkillDeclaration;
15
use App\Models\Skill;
16
use App\Models\Lookup\SkillStatus;
17
use App\Models\Degree;
18
use App\Models\Criteria;
19
use App\Models\Course;
20
use App\Models\WorkExperience;
21
use App\Services\Validation\ApplicationValidator;
22
use Illuminate\Support\Facades\Auth;
23
use Illuminate\Support\Facades\Log;
24
use App\Models\Lookup\ReviewStatus;
25
26
class ApplicationByJobController extends Controller
27
{
28
    /**
29
     * Display a listing of the applications for given jobPoster.
30
     *
31
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
32
     * @return \Illuminate\Http\Response
33
     */
34
    public function index(JobPoster $jobPoster)
35
    {
36
        $applications = $jobPoster->submitted_applications()
37
            ->with([
38
                'veteran_status',
39
                'citizenship_declaration',
40
                'application_review',
41
                'applicant.user',
42
                'job_poster.criteria',
43
            ])
44
            ->get();
45
        return view('manager/review_applications', [
46
            // Localization Strings.
47
            'jobs_l10n' => Lang::get('manager/job_index'),
48
            // Data.
49
            'job' => $jobPoster->toApiArray(),
50
            'applications' => $applications,
51
            'review_statuses' => ReviewStatus::all()
52
        ]);
53
    }
54
55
    /**
56
     * Return the current applicant's application for a given Job Poster.
57
     *
58
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
59
     * @return mixed|\App\Models\JobApplication
60
     */
61
    protected function getApplicationFromJob(JobPoster $jobPoster)
62
    {
63
        $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
64
            ->where('job_poster_id', $jobPoster->id)->first();
65
        if ($application == null) {
66
            $application = new JobApplication();
67
            $application->job_poster_id = $jobPoster->id;
68
            $application->applicant_id = Auth::user()->applicant->id;
69
            $application->application_status_id = ApplicationStatus::where('name', 'draft')->firstOrFail()->id;
70
            $application->save();
71
        }
72
        return $application;
73
    }
74
75
    /**
76
     * Show the form for editing Application basics for the specified job.
77
     *
78
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
79
     * @return \Illuminate\Http\Response
80
     */
81
    public function editBasics(JobPoster $jobPoster)
82
    {
83
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
84
        $application = $this->getApplicationFromJob($jobPoster);
85
86
        // Ensure user has permissions to view and update application.
87
        $this->authorize('view', $application);
88
        $this->authorize('update', $application);
89
90
        return view(
91
            'applicant/application_post_01',
92
            [
93
                // Application Template Data.
94
                'application_step' => 1,
95
                'application_template' => Lang::get('applicant/application_template'),
96
                'language_options' => PreferredLanguage::all(),
97
                'citizenship_options' => CitizenshipDeclaration::all(),
98
                'veteran_options' => VeteranStatus::all(),
99
                'preferred_language_template' => Lang::get('common/preferred_language'),
100
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
101
                'veteran_status_template' => Lang::get('common/veteran_status'),
102
                // Job Data.
103
                'job' => $jobPoster,
104
                // Applicant Data.
105
                'applicant' => $applicant,
106
                'job_application' => $application,
107
                // Submission.
108
                'form_submit_action' => route('job.application.update.1', $jobPoster)
109
            ]
110
        );
111
    }
112
113
    /**
114
     * Show the form for editing Application Experience for the specified job.
115
     *
116
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
117
     * @return \Illuminate\Http\Response
118
     */
119
    public function editExperience(JobPoster $jobPoster)
120
    {
121
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
122
        $application = $this->getApplicationFromJob($jobPoster);
123
124
        // Ensure user has permissions to view and update application.
125
        $this->authorize('view', $application);
126
        $this->authorize('update', $application);
127
128
        return view(
129
            'applicant/application_post_02',
130
            [
131
                // Application Template Data.
132
                'application_step' => 2,
133
                'application_template' => Lang::get('applicant/application_template'),
134
                // Job Data.
135
                'job' => $jobPoster,
136
                // Applicant Data.
137
                'applicant' => $applicant,
138
                'job_application' => $application,
139
                // Submission.
140
                'form_submit_action' => route('job.application.update.2', $jobPoster)
141
            ]
142
        );
143
    }
144
145
    /**
146
     * Show the form for editing Application Essential Skills for the specified job.
147
     *
148
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
149
     * @return \Illuminate\Http\Response
150
     */
151
    public function editEssentialSkills(JobPoster $jobPoster)
152
    {
153
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
154
        $application = $this->getApplicationFromJob($jobPoster);
155
156
        // Ensure user has permissions to view and update application.
157
        $this->authorize('view', $application);
158
        $this->authorize('update', $application);
159
160
        $criteria = [
161
            'essential' => $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

161
            'essential' => $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
162
                return $value->criteria_type->name == 'essential';
163
            }),
164
            'asset' => $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

164
            'asset' => $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
                return $value->criteria_type->name == 'asset';
166
            }),
167
        ];
168
169
        return view(
170
            'applicant/application_post_03',
171
            [
172
                // Application Template Data.
173
                'application_step' => 3,
174
                'application_template' => Lang::get('applicant/application_template'),
175
                // Job Data.
176
                'job' => $jobPoster,
177
                // Skills Data.
178
                'skills' => Skill::all(),
179
                'skill_template' => Lang::get('common/skills'),
180
                'criteria' => $criteria,
181
                // Applicant Data.
182
                'applicant' => $applicant,
183
                'job_application' => $application,
184
                // Submission.
185
                'form_submit_action' => route('job.application.update.3', $jobPoster)
186
            ]
187
        );
188
    }
189
190
    /**
191
     * Show the form for editing Application Asset Skills for the specified job.
192
     *
193
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
194
     * @return \Illuminate\Http\Response
195
     */
196
    public function editAssetSkills(JobPoster $jobPoster)
197
    {
198
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
199
        $application = $this->getApplicationFromJob($jobPoster);
200
201
        // Ensure user has permissions to view and update application.
202
        $this->authorize('view', $application);
203
        $this->authorize('update', $application);
204
205
        $criteria = [
206
            'essential' => $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

206
            'essential' => $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
                return $value->criteria_type->name == 'essential';
208
            }),
209
            'asset' => $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

209
            'asset' => $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
                return $value->criteria_type->name == 'asset';
211
            }),
212
        ];
213
214
        return view(
215
            'applicant/application_post_04',
216
            [
217
                // Application Template Data.
218
                'application_step' => 4,
219
                'application_template' => Lang::get('applicant/application_template'),
220
                // Job Data.
221
                'job' => $jobPoster,
222
                // Skills Data.
223
                'skills' => Skill::all(),
224
                'skill_template' => Lang::get('common/skills'),
225
                'criteria' => $criteria,
226
                // Applicant Data.
227
                'applicant' => $applicant,
228
                'job_application' => $application,
229
                // Submission.
230
                'form_submit_action' => route('job.application.update.4', $jobPoster)
231
            ]
232
        );
233
    }
234
235
    /**
236
     * Show the Application Preview for the application for the specified job.
237
     *
238
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
239
     * @return \Illuminate\Http\Response
240
     */
241
    public function preview(JobPoster $jobPoster)
242
    {
243
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
244
        $application = $this->getApplicationFromJob($jobPoster);
245
246
        $this->authorize('view', $application);
247
        $criteria = [
248
            'essential' => $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

248
            'essential' => $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
249
                return $value->criteria_type->name == 'essential';
250
            }),
251
            'asset' => $jobPoster->criteria->filter(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

251
            'asset' => $jobPoster->criteria->filter(function ($value, /** @scrutinizer ignore-unused */ $key) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
252
                return $value->criteria_type->name == 'asset';
253
            }),
254
        ];
255
256
        return view(
257
            'applicant/application_post_05',
258
            [
259
                // Application Template Data.
260
                'application_step' => 5,
261
                'application_template' => Lang::get('applicant/application_template'),
262
                'preferred_language_template' => Lang::get('common/preferred_language'),
263
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
264
                'veteran_status_template' => Lang::get('common/veteran_status'),
265
                // Job Data.
266
                'job' => $jobPoster,
267
                // Skills Data.
268
                'skills' => Skill::all(),
269
                'skill_template' => Lang::get('common/skills'),
270
                'criteria' => $criteria,
271
                // Applicant Data.
272
                'applicant' => $applicant,
273
                'job_application' => $application,
274
            ]
275
        );
276
    }
277
278
    /**
279
     * Show the Confirm Submit page for the application for the specified job.
280
     *
281
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
282
     * @return \Illuminate\Http\Response
283
     */
284
    public function confirm(JobPoster $jobPoster)
285
    {
286
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
287
        $application = $this->getApplicationFromJob($jobPoster);
288
289
        $this->authorize('update', $application);
290
291
        return view(
292
            'applicant/application_post_06',
293
            [
294
                // Application Template Data.
295
                'application_step' => 6,
296
                'application_template' => Lang::get('applicant/application_template'),
297
                // Used by tracker partial.
298
                'job' => $jobPoster,
299
                'job_application' => $application,
300
                // Submission.
301
                'form_submit_action' => route('job.application.submit', $jobPoster)
302
            ]
303
        );
304
    }
305
306
    /**
307
     * Show the application submission information.
308
     *
309
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
310
     * @return \Illuminate\Http\Response
311
     */
312
    public function complete(JobPoster $jobPoster)
313
    {
314
        // Include Applicant Data.
315
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
316
        // Include Application Data.
317
        $application = $this->getApplicationFromJob($jobPoster);
318
319
        // Ensure user has permissions to view application.
320
        $this->authorize('view', $application);
321
322
        // Return the Completion View.
323
        return view(
324
            'applicant/application_post_complete',
325
            [
326
                // Application Template Data.
327
                'application_template' => Lang::get('applicant/application_template'),
328
                // Job Data.
329
                'job' => $jobPoster,
330
                // Applicant Data.
331
                'applicant' => $applicant,
332
                'job_application' => $application
333
            ]
334
        );
335
    }
336
337
    /**
338
     * Update the Application Basics in storage for the specified job.
339
     *
340
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
341
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
342
     * @return \Illuminate\Http\Response
343
     */
344
    public function updateBasics(Request $request, JobPoster $jobPoster)
345
    {
346
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
347
        $application = $this->getApplicationFromJob($jobPoster);
348
349
        // Ensure user has permissions to update this application.
350
        $this->authorize('update', $application);
351
352
        $application->fill([
353
            'citizenship_declaration_id' => $request->input('citizenship_declaration_id'),
354
            'veteran_status_id' => $request->input('veteran_status_id'),
355
            'preferred_language_id' => $request->input('preferred_language_id'),
356
            'language_requirement_confirmed' => $request->input('language_requirement_confirmed')
357
        ]);
358
        $application->save();
359
360
        $questions = $jobPoster->job_poster_questions;
361
        $questionsInput = $request->input('questions');
362
        foreach ($questions as $question) {
363
            $answer = null;
364
            if (isset($questionsInput[$question->id])) {
365
                $answer = $questionsInput[$question->id];
366
            }
367
            $answerObj = $application->job_application_answers
368
            ->firstWhere('job_poster_question_id', $question->id);
369
            if ($answerObj == null) {
370
                $answerObj = new JobApplicationAnswer();
371
                $answerObj->job_poster_question_id = $question->id;
372
                $answerObj->job_application_id = $application->id;
373
            }
374
            $answerObj->answer = $answer;
375
            $answerObj->save();
376
        }
377
378
        // Redirect to correct page.
379
        switch ($request->input('submit')) {
380
            case 'save_and_quit':
381
            case 'previous':
382
                return redirect()->route('applications.index');
383
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
384
            case 'save_and_continue':
385
            case 'next':
386
                return redirect()->route('job.application.edit.2', $jobPoster);
387
                break;
388
            default:
389
                return redirect()->back()->withInput();
390
        }
391
    }
392
393
    /**
394
     * Update the Application Basics in storage for the specified job.
395
     *
396
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
397
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
398
     * @return \Illuminate\Http\Response
399
     */
400
    public function updateExperience(Request $request, JobPoster $jobPoster)
401
    {
402
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
403
        $application = $this->getApplicationFromJob($jobPoster);
404
405
        // Ensure user has permissions to update this application.
406
        $this->authorize('update', $application);
407
408
        // Record that the user has saved their experience for this application.
409
        $application->experience_saved = true;
410
        $application->save();
411
412
        $degrees = $request->input('degrees');
413
414
        $request->validate([
415
            'degrees.new.*.degree_type_id' => 'required',
416
            'degrees.new.*.area_of_study'  => 'required',
417
            'degrees.new.*.institution'    => 'required',
418
            'degrees.new.*.thesis'         => 'nullable',
419
            'degrees.new.*.start_date'     => 'required|date',
420
            'degrees.new.*.end_date'       => 'required|date',
421
        ]);
422
423
        // Save new degrees.
424
        if (isset($degrees['new'])) {
425
            foreach ($degrees['new'] as $degreeInput) {
426
                $degree = new Degree();
427
                $degree->applicant_id = $applicant->id;
428
                $degree->fill([
429
                    'degree_type_id' => $degreeInput['degree_type_id'],
430
                    'area_of_study' => $degreeInput['area_of_study'],
431
                    'institution' => $degreeInput['institution'],
432
                    'thesis' => $degreeInput['thesis'],
433
                    'start_date' => $degreeInput['start_date'],
434
                    'end_date' => $degreeInput['end_date']
435
                ]);
436
                $degree->save();
437
            }
438
        }
439
440
        // Update old degrees.
441
        if (isset($degrees['old'])) {
442
            foreach ($degrees['old'] as $id => $degreeInput) {
443
                // Ensure this degree belongs to this applicant.
444
                $degree = $applicant->degrees->firstWhere('id', $id);
445
                if ($degree != null) {
446
                    $degree->fill([
447
                        'degree_type_id' => $degreeInput['degree_type_id'],
448
                        'area_of_study' => $degreeInput['area_of_study'],
449
                        'institution' => $degreeInput['institution'],
450
                        'thesis' => $degreeInput['thesis'],
451
                        'start_date' => $degreeInput['start_date'],
452
                        'end_date' => $degreeInput['end_date']
453
                    ]);
454
                    $degree->save();
455
                } else {
456
                    Log::warning("Applicant $applicant->id attempted to update degree with invalid id: $id");
457
                }
458
            }
459
        }
460
461
        $courses = $request->input('courses');
462
463
        $request->validate([
464
            'courses.new.*.name'             => 'required',
465
            'courses.new.*.institution'      => 'required',
466
            'courses.new.*.course_status_id' => 'required',
467
            'courses.new.*.start_date'       => 'required|date',
468
            'courses.new.*.end_date'         => 'required|date',
469
        ]);
470
471
        // Save new courses.
472
        if (isset($courses['new'])) {
473
            foreach ($courses['new'] as $courseInput) {
474
                $course = new Course();
475
                $course->applicant_id = $applicant->id;
476
                $course->fill([
477
                    'name' => $courseInput['name'],
478
                    'institution' => $courseInput['institution'],
479
                    'course_status_id' => $courseInput['course_status_id'],
480
                    'start_date' => $courseInput['start_date'],
481
                    'end_date' => $courseInput['end_date']
482
                ]);
483
                $course->save();
484
            }
485
        }
486
487
        // Update old courses.
488
        if (isset($courses['old'])) {
489
            foreach ($courses['old'] as $id => $courseInput) {
490
                // Ensure this course belongs to this applicant.
491
                $course = $applicant->courses->firstWhere('id', $id);
492
                if ($course != null) {
493
                    $course->fill([
494
                        'name' => $courseInput['name'],
495
                        'institution' => $courseInput['institution'],
496
                        'course_status_id' => $courseInput['course_status_id'],
497
                        'start_date' => $courseInput['start_date'],
498
                        'end_date' => $courseInput['end_date']
499
                    ]);
500
                    $course->save();
501
                } else {
502
                    Log::warning("Applicant $applicant->id attempted to update course with invalid id: $id");
503
                }
504
            }
505
        }
506
507
        $work_experiences = $request->input('work_experiences');
508
509
        $request->validate([
510
            'work_experiences.new.*.role'        => 'required',
511
            'work_experiences.new.*.company'     => 'required',
512
            'work_experiences.new.*.description' => 'required',
513
            'work_experiences.new.*.start_date'  => 'required|date',
514
            'work_experiences.new.*.end_date'    => 'required|date',
515
        ]);
516
517
        // Save new work_experiences.
518
        if (isset($work_experiences['new'])) {
519
            foreach ($work_experiences['new'] as $workExperienceInput) {
520
                $workExperience = new WorkExperience();
521
                $workExperience->applicant_id = $applicant->id;
522
                $workExperience->fill([
523
                    'role' => $workExperienceInput['role'],
524
                    'company' => $workExperienceInput['company'],
525
                    'description' => $workExperienceInput['description'],
526
                    'start_date' => $workExperienceInput['start_date'],
527
                    'end_date' => $workExperienceInput['end_date']
528
                ]);
529
                $workExperience->save();
530
            }
531
        }
532
533
        // Update old work_experiences.
534
        if (isset($work_experiences['old'])) {
535
            foreach ($work_experiences['old'] as $id => $workExperienceInput) {
536
                // Ensure this work_experience belongs to this applicant.
537
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
538
                if ($workExperience != null) {
539
                    $workExperience->fill([
540
                        'role' => $workExperienceInput['role'],
541
                        'company' => $workExperienceInput['company'],
542
                        'description' => $workExperienceInput['description'],
543
                        'start_date' => $workExperienceInput['start_date'],
544
                        'end_date' => $workExperienceInput['end_date']
545
                    ]);
546
                    $workExperience->save();
547
                } else {
548
                    Log::warning("Applicant $applicant->id attempted to update work_experience with invalid id: $id");
549
                }
550
            }
551
        }
552
553
        // Redirect to correct page.
554
        switch ($request->input('submit')) {
555
            case 'save_and_quit':
556
                return redirect()->route('applications.index');
557
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
558
            case 'save_and_continue':
559
            case 'next':
560
                return redirect()->route('job.application.edit.3', $jobPoster);
561
                break;
562
            case 'previous':
563
                return redirect()->route('job.application.edit.1', $jobPoster);
564
                break;
565
            default:
566
                return redirect()->back()->withInput();
567
        }
568
    }
569
570
    /**
571
     * Update the Application Essential Skills in storage for the specified job.
572
     *
573
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
574
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
575
     * @return \Illuminate\Http\Response
576
     */
577
    public function updateEssentialSkills(Request $request, JobPoster $jobPoster)
578
    {
579
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
580
        $application = $this->getApplicationFromJob($jobPoster);
581
582
        // Ensure user has permissions to update this application.
583
        $this->authorize('update', $application);
584
585
        $skillDeclarations = $request->input('skill_declarations');
586
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
587
588
        // Save new skill declarartions.
589
        if (isset($skillDeclarations['new'])) {
590
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
591
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
592
                    $skillDeclaration = new SkillDeclaration();
593
                    $skillDeclaration->applicant_id = $applicant->id;
594
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
595
                    $skillDeclaration->skill_status_id = $claimedStatusId;
596
                    $skillDeclaration->fill([
597
                        'description' => $skillDeclarationInput['description'],
598
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
599
                    ]);
600
                    $skillDeclaration->save();
601
602
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
603
                    $skillDeclaration->references()->sync($referenceIds);
604
605
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
606
                    $skillDeclaration->work_samples()->sync($sampleIds);
607
                }
608
            }
609
        }
610
611
        // Update old declarations.
612
        if (isset($skillDeclarations['old'])) {
613
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
614
                foreach ($typeInput as $id => $skillDeclarationInput) {
615
                    // Ensure this declaration belongs to this applicant.
616
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
617
                    if ($skillDeclaration != null) {
618
                        // skill_id and skill_status cannot be changed.
1 ignored issue
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
619
                        $skillDeclaration->fill([
620
                            'description' => $skillDeclarationInput['description'],
621
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
622
                        ]);
623
                        $skillDeclaration->save();
624
625
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
626
                        $skillDeclaration->references()->sync($referenceIds);
627
628
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
629
                        $skillDeclaration->work_samples()->sync($sampleIds);
630
                    } else {
631
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
632
                    }
633
                }
634
            }
635
        }
636
637
        // Redirect to correct page.
638
        switch ($request->input('submit')) {
639
            case 'save_and_quit':
640
                return redirect()->route('applications.index');
641
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
642
            case 'save_and_continue':
643
            case 'next':
644
                return redirect()->route('job.application.edit.4', $jobPoster);
645
                break;
646
            case 'previous':
647
                return redirect()->route('job.application.edit.2', $jobPoster);
648
                break;
649
            default:
650
                return redirect()->back()->withInput();
651
        }
652
    }
653
654
    /**
655
     * Update the Application Asset Skills in storage for the specified job.
656
     *
657
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
658
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
659
     * @return \Illuminate\Http\Response
660
     */
661
    public function updateAssetSkills(Request $request, JobPoster $jobPoster)
662
    {
663
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
664
        $application = $this->getApplicationFromJob($jobPoster);
665
666
        // Ensure user has permissions to update this application.
667
        $this->authorize('update', $application);
668
669
        $skillDeclarations = $request->input('skill_declarations');
670
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
671
672
        // Save new skill declarartions.
673
        if (isset($skillDeclarations['new'])) {
674
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
675
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
676
                    $skillDeclaration = new SkillDeclaration();
677
                    $skillDeclaration->applicant_id = $applicant->id;
678
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
679
                    $skillDeclaration->skill_status_id = $claimedStatusId;
680
                    $skillDeclaration->fill([
681
                        'description' => $skillDeclarationInput['description'],
682
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
683
                    ]);
684
                    $skillDeclaration->save();
685
686
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
687
                    $skillDeclaration->references()->sync($referenceIds);
688
689
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
690
                    $skillDeclaration->work_samples()->sync($sampleIds);
691
                }
692
            }
693
        }
694
695
        // Update old declarations.
696
        if (isset($skillDeclarations['old'])) {
697
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
698
                foreach ($typeInput as $id => $skillDeclarationInput) {
699
                    // Ensure this declaration belongs to this applicant.
700
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
701
                    if ($skillDeclaration != null) {
702
                        // skill_id and skill_status cannot be changed.
1 ignored issue
show
Coding Style Documentation introduced by
Inline comments must start with a capital letter
Loading history...
703
                        $skillDeclaration->fill([
704
                            'description' => $skillDeclarationInput['description'],
705
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
706
                        ]);
707
                        $skillDeclaration->save();
708
709
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
710
                        $skillDeclaration->references()->sync($referenceIds);
711
712
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
713
                        $skillDeclaration->work_samples()->sync($sampleIds);
714
                    } else {
715
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
716
                    }
717
                }
718
            }
719
        }
720
721
        // Redirect to correct page.
722
        switch ($request->input('submit')) {
723
            case 'save_and_quit':
724
                return redirect()->route('applications.index');
725
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
726
            case 'save_and_continue':
727
            case 'next':
728
                return redirect()->route('job.application.edit.5', $jobPoster);
729
                break;
730
            case 'previous':
731
                return redirect()->route('job.application.edit.3', $jobPoster);
732
                break;
733
            default:
734
                return redirect()->back()->withInput();
735
        }
736
    }
737
738
    /**
739
     * Submit the Application for the specified job.
740
     *
741
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
742
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
743
     * @return \Illuminate\Http\Response
744
     */
745
    public function submit(Request $request, JobPoster $jobPoster)
746
    {
747
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
Bug introduced by
Accessing applicant on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
748
        $application = $this->getApplicationFromJob($jobPoster);
749
750
        // Ensure user has permissions to update this application.
751
        $this->authorize('update', $application);
752
753
        // Only complete submission if submit button was pressed.
754
        if ($request->input('submit') == 'submit') {
755
            $request->validate([
756
                'submission_signature' => [
757
                    'required',
758
                    'string',
759
                    'max:191',
760
                ],
761
                'submission_date' => [
762
                    'required',
763
                    'string',
764
                    'max:191',
765
                ]
766
            ]);
767
768
            // Save any final info.
769
            $application->fill([
770
                'submission_signature' => $request->input('submission_signature'),
771
                'submission_date' => $request->input('submission_date'),
772
            ]);
773
774
            $validator = new ApplicationValidator();
775
            $validator->validate($application);
776
777
            // Change status to 'submitted'.
778
            $application->application_status_id = ApplicationStatus::where('name', 'submitted')->firstOrFail()->id;
779
        }
780
781
        $application->save();
782
783
        // Redirect to correct page.
784
        switch ($request->input('submit')) {
785
            case 'save_and_quit':
786
                return redirect()->route('applications.index');
787
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
788
            case 'submit':
789
                return redirect()->route('job.application.complete', $jobPoster);
790
                break;
791
            case 'previous':
792
                return redirect()->route('job.application.edit.4', $jobPoster);
793
                break;
794
            default:
795
                return redirect()->back()->withInput();
796
        }
797
    }
798
}
799