Passed
Push — task/extract-localization-to-f... ( 65b263...600275 )
by Tristan
05:41 queued 10s
created

ApplicationByJobController::preview()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 51
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 51
rs 8.9848
cc 5
nc 16
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Course;
6
use App\Models\Criteria;
7
use App\Models\Degree;
8
use App\Models\JobApplication;
9
use App\Models\JobApplicationAnswer;
10
use App\Models\JobPoster;
11
use App\Models\Lookup\ApplicationStatus;
12
use App\Models\Lookup\CitizenshipDeclaration;
13
use App\Models\Lookup\PreferredLanguage;
14
use App\Models\Lookup\ReviewStatus;
15
use App\Models\Lookup\SkillStatus;
16
use App\Models\Lookup\VeteranStatus;
17
use App\Models\Skill;
18
use App\Models\SkillDeclaration;
19
use App\Models\WorkExperience;
20
use App\Services\Validation\ApplicationValidator;
21
use Facades\App\Services\WhichPortal;
22
use Illuminate\Http\Request;
23
use Illuminate\Http\Resources\Json\JsonResource;
24
use Illuminate\Support\Facades\Auth;
25
use Illuminate\Support\Facades\Lang;
26
use Illuminate\Support\Facades\Log;
27
28
class ApplicationByJobController extends Controller
29
{
30
    /**
31
     * Display a listing of the applications for given jobPoster.
32
     *
33
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function index(JobPoster $jobPoster)
37
    {
38
        $applications = $jobPoster->submitted_applications()
39
            ->with([
40
                'veteran_status',
41
                'citizenship_declaration',
42
                'application_review',
43
                'applicant.user',
44
                'job_poster.criteria',
45
            ])
46
            ->get();
47
        return view('manager/review_applications', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

47
        return /** @scrutinizer ignore-call */ view('manager/review_applications', [
Loading history...
48
            // Localization Strings.
49
            'jobs_l10n' => Lang::get('manager/job_index'),
50
            // Data.
51
            'job' => new JsonResource($jobPoster),
52
            'is_hr_portal' => WhichPortal::isHrPortal(),
53
            'applications' => $applications,
54
            'review_statuses' => ReviewStatus::all()
55
        ]);
56
    }
57
58
    /**
59
     * Return the current applicant's application for a given Job Poster.
60
     *
61
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
62
     * @return mixed|\App\Models\JobApplication
63
     */
64
    protected function getApplicationFromJob(JobPoster $jobPoster)
65
    {
66
        $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
67
            ->where('job_poster_id', $jobPoster->id)->first();
68
        if ($application == null) {
69
            $application = new JobApplication();
70
            $application->job_poster_id = $jobPoster->id;
71
            $application->applicant_id = Auth::user()->applicant->id;
72
            $application->application_status_id = ApplicationStatus::where('name', 'draft')->firstOrFail()->id;
73
            $application->save();
74
        }
75
        return $application;
76
    }
77
78
    /**
79
     * Show the form for editing Application basics for the specified job.
80
     *
81
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
82
     * @return \Illuminate\Http\Response
83
     */
84
    public function editBasics(JobPoster $jobPoster)
85
    {
86
        $applicant = Auth::user()->applicant;
87
        $application = $this->getApplicationFromJob($jobPoster);
88
89
        // Ensure user has permissions to view and update application.
90
        $this->authorize('view', $application);
91
        $this->authorize('update', $application);
92
93
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

93
        return /** @scrutinizer ignore-call */ view(
Loading history...
94
            'applicant/application_post_01',
95
            [
96
                // Application Template Data.
97
                'application_step' => 1,
98
                'application_template' => Lang::get('applicant/application_template'),
99
                'language_options' => PreferredLanguage::all(),
100
                'citizenship_options' => CitizenshipDeclaration::all(),
101
                'veteran_options' => VeteranStatus::all(),
102
                'preferred_language_template' => Lang::get('common/preferred_language'),
103
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
104
                'veteran_status_template' => Lang::get('common/veteran_status'),
105
                // Job Data.
106
                'job' => $jobPoster,
107
                // Applicant Data.
108
                'applicant' => $applicant,
109
                'job_application' => $application,
110
                // Submission.
111
                'form_submit_action' => route('job.application.update.1', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

111
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.1', $jobPoster)
Loading history...
112
            ]
113
        );
114
    }
115
116
    /**
117
     * Show the form for editing Application Experience for the specified job.
118
     *
119
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
120
     * @return \Illuminate\Http\Response
121
     */
122
    public function editExperience(JobPoster $jobPoster)
123
    {
124
        $applicant = Auth::user()->applicant;
125
        $application = $this->getApplicationFromJob($jobPoster);
126
127
        // Ensure user has permissions to view and update application.
128
        $this->authorize('view', $application);
129
        $this->authorize('update', $application);
130
131
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

131
        return /** @scrutinizer ignore-call */ view(
Loading history...
132
            'applicant/application_post_02',
133
            [
134
                // Application Template Data.
135
                'application_step' => 2,
136
                'application_template' => Lang::get('applicant/application_template'),
137
                // Job Data.
138
                'job' => $jobPoster,
139
                // Applicant Data.
140
                'applicant' => $applicant,
141
                'job_application' => $application,
142
                // Submission.
143
                'form_submit_action' => route('job.application.update.2', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

143
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.2', $jobPoster)
Loading history...
144
            ]
145
        );
146
    }
147
148
    /**
149
     * Show the form for editing Application Essential Skills for the specified job.
150
     *
151
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
152
     * @return \Illuminate\Http\Response
153
     */
154
    public function editEssentialSkills(JobPoster $jobPoster)
155
    {
156
        $applicant = Auth::user()->applicant;
157
        $application = $this->getApplicationFromJob($jobPoster);
158
159
        // Ensure user has permissions to view and update application.
160
        $this->authorize('view', $application);
161
        $this->authorize('update', $application);
162
163
        $criteria = [
164
            '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

164
            '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...
165
                return $value->criteria_type->name == 'essential';
166
            }),
167
            '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

167
            '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...
168
                return $value->criteria_type->name == 'asset';
169
            }),
170
        ];
171
172
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

172
        return /** @scrutinizer ignore-call */ view(
Loading history...
173
            'applicant/application_post_03',
174
            [
175
                // Application Template Data.
176
                'application_step' => 3,
177
                'application_template' => Lang::get('applicant/application_template'),
178
                // Job Data.
179
                'job' => $jobPoster,
180
                // Skills Data.
181
                'skills' => Skill::all(),
182
                'skill_template' => Lang::get('common/skills'),
183
                'criteria' => $criteria,
184
                // Applicant Data.
185
                'applicant' => $applicant,
186
                'job_application' => $application,
187
                // Submission.
188
                'form_submit_action' => route('job.application.update.3', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

188
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.3', $jobPoster)
Loading history...
189
            ]
190
        );
191
    }
192
193
    /**
194
     * Show the form for editing Application Asset Skills for the specified job.
195
     *
196
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
197
     * @return \Illuminate\Http\Response
198
     */
199
    public function editAssetSkills(JobPoster $jobPoster)
200
    {
201
        $applicant = Auth::user()->applicant;
202
        $application = $this->getApplicationFromJob($jobPoster);
203
204
        // Ensure user has permissions to view and update application.
205
        $this->authorize('view', $application);
206
        $this->authorize('update', $application);
207
208
        $criteria = [
209
            '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

209
            '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...
210
                return $value->criteria_type->name == 'essential';
211
            }),
212
            '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

212
            '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...
213
                return $value->criteria_type->name == 'asset';
214
            }),
215
        ];
216
217
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

217
        return /** @scrutinizer ignore-call */ view(
Loading history...
218
            'applicant/application_post_04',
219
            [
220
                // Application Template Data.
221
                'application_step' => 4,
222
                'application_template' => Lang::get('applicant/application_template'),
223
                // Job Data.
224
                'job' => $jobPoster,
225
                // Skills Data.
226
                'skills' => Skill::all(),
227
                'skill_template' => Lang::get('common/skills'),
228
                'criteria' => $criteria,
229
                // Applicant Data.
230
                'applicant' => $applicant,
231
                'job_application' => $application,
232
                // Submission.
233
                'form_submit_action' => route('job.application.update.4', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

233
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.4', $jobPoster)
Loading history...
234
            ]
235
        );
236
    }
237
238
    /**
239
     * Show the Application Preview for the application for the specified job.
240
     *
241
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
242
     * @return \Illuminate\Http\Response
243
     */
244
    public function preview(JobPoster $jobPoster)
245
    {
246
        $applicant = Auth::user()->applicant;
247
        $application = $this->getApplicationFromJob($jobPoster);
248
249
        $this->authorize('view', $application);
250
        $criteria = [
251
            '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

251
            '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...
252
                return $value->criteria_type->name == 'essential';
253
            }),
254
            '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

254
            '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...
255
                return $value->criteria_type->name == 'asset';
256
            }),
257
        ];
258
        $skillDeclarations = $application->isDraft()
259
            ? $applicant->skill_declarations
260
            : $application->skill_declarations;
261
        $degrees = $application->isDraft()
262
            ? $applicant->degrees
263
            : $application->degrees;
264
        $courses = $application->isDraft()
265
            ? $applicant->courses
266
            : $application->courses;
267
        $work_experiences = $application->isDraft()
268
            ? $applicant->work_experiences
269
            : $application->work_experiences;
270
271
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

271
        return /** @scrutinizer ignore-call */ view(
Loading history...
272
            'applicant/application_post_05',
273
            [
274
                // Application Template Data.
275
                'application_step' => 5,
276
                'application_template' => Lang::get('applicant/application_template'),
277
                'preferred_language_template' => Lang::get('common/preferred_language'),
278
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
279
                'veteran_status_template' => Lang::get('common/veteran_status'),
280
                // Job Data.
281
                'job' => $jobPoster,
282
                // Skills Data.
283
                'skills' => Skill::all(),
284
                'skill_template' => Lang::get('common/skills'),
285
                'criteria' => $criteria,
286
                // Applicant Data.
287
                'applicant' => $applicant,
288
                'job_application' => $application,
289
                'skill_declarations' => $skillDeclarations,
290
                'degrees' => $degrees,
291
                'courses' => $courses,
292
                'work_experiences' => $work_experiences,
293
                'is_manager_view' => WhichPortal::isManagerPortal(),
294
                'is_draft' => $application->application_status->name == 'draft',
295
            ]
296
        );
297
    }
298
299
    /**
300
     * Show the Confirm Submit page for the application for the specified job.
301
     *
302
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
303
     * @return \Illuminate\Http\Response
304
     */
305
    public function confirm(JobPoster $jobPoster)
306
    {
307
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
308
        $application = $this->getApplicationFromJob($jobPoster);
309
310
        $this->authorize('update', $application);
311
312
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

312
        return /** @scrutinizer ignore-call */ view(
Loading history...
313
            'applicant/application_post_06',
314
            [
315
                // Application Template Data.
316
                'application_step' => 6,
317
                'application_template' => Lang::get('applicant/application_template'),
318
                // Used by tracker partial.
319
                'job' => $jobPoster,
320
                'job_application' => $application,
321
                // Submission.
322
                'form_submit_action' => route('job.application.submit', $jobPoster)
0 ignored issues
show
Bug introduced by
The function route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

322
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.submit', $jobPoster)
Loading history...
323
            ]
324
        );
325
    }
326
327
    /**
328
     * Show the application submission information.
329
     *
330
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
331
     * @return \Illuminate\Http\Response
332
     */
333
    public function complete(JobPoster $jobPoster)
334
    {
335
        // Include Applicant Data.
336
        $applicant = Auth::user()->applicant;
337
        // Include Application Data.
338
        $application = $this->getApplicationFromJob($jobPoster);
339
340
        // Ensure user has permissions to view application.
341
        $this->authorize('view', $application);
342
343
        // Return the Completion View.
344
        return view(
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

344
        return /** @scrutinizer ignore-call */ view(
Loading history...
345
            'applicant/application_post_complete',
346
            [
347
                // Application Template Data.
348
                'application_template' => Lang::get('applicant/application_template'),
349
                // Job Data.
350
                'job' => $jobPoster,
351
                // Applicant Data.
352
                'applicant' => $applicant,
353
                'job_application' => $application
354
            ]
355
        );
356
    }
357
358
    /**
359
     * Update the Application Basics in storage for the specified job.
360
     *
361
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
362
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
363
     * @return \Illuminate\Http\Response
364
     */
365
    public function updateBasics(Request $request, JobPoster $jobPoster)
366
    {
367
        $applicant = Auth::user()->applicant;
0 ignored issues
show
Unused Code introduced by
The assignment to $applicant is dead and can be removed.
Loading history...
368
        $application = $this->getApplicationFromJob($jobPoster);
369
370
        // Ensure user has permissions to update this application.
371
        $this->authorize('update', $application);
372
373
        $application->fill([
374
            'citizenship_declaration_id' => $request->input('citizenship_declaration_id'),
375
            'veteran_status_id' => $request->input('veteran_status_id'),
376
            'preferred_language_id' => $request->input('preferred_language_id'),
377
            'language_requirement_confirmed' => $request->input('language_requirement_confirmed')
378
        ]);
379
        $application->save();
380
381
        $questions = $jobPoster->job_poster_questions;
382
        $questionsInput = $request->input('questions');
383
        foreach ($questions as $question) {
384
            $answer = null;
385
            if (isset($questionsInput[$question->id])) {
386
                $answer = $questionsInput[$question->id];
387
            }
388
            $answerObj = $application->job_application_answers
389
            ->firstWhere('job_poster_question_id', $question->id);
390
            if ($answerObj == null) {
391
                $answerObj = new JobApplicationAnswer();
392
                $answerObj->job_poster_question_id = $question->id;
393
                $answerObj->job_application_id = $application->id;
394
            }
395
            $answerObj->answer = $answer;
396
            $answerObj->save();
397
        }
398
399
        // Redirect to correct page.
400
        switch ($request->input('submit')) {
401
            case 'save_and_quit':
402
            case 'previous':
403
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

403
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
404
                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...
405
            case 'save_and_continue':
406
            case 'next':
407
                return redirect()->route('job.application.edit.2', $jobPoster);
408
                break;
409
            default:
410
                return redirect()->back()->withInput();
411
        }
412
    }
413
414
    /**
415
     * Update the Application Basics in storage for the specified job.
416
     *
417
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
418
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
419
     * @return \Illuminate\Http\Response
420
     */
421
    public function updateExperience(Request $request, JobPoster $jobPoster)
422
    {
423
        $applicant = Auth::user()->applicant;
424
        $application = $this->getApplicationFromJob($jobPoster);
425
426
        // Ensure user has permissions to update this application.
427
        $this->authorize('update', $application);
428
429
        // Record that the user has saved their experience for this application.
430
        $application->experience_saved = true;
431
        $application->save();
432
433
        $degrees = $request->input('degrees');
434
435
        $request->validate([
436
            'degrees.new.*.degree_type_id' => 'required',
437
            'degrees.new.*.area_of_study'  => 'required',
438
            'degrees.new.*.institution'    => 'required',
439
            'degrees.new.*.thesis'         => 'nullable',
440
            'degrees.new.*.start_date'     => 'required|date',
441
            'degrees.new.*.end_date'       => 'required|date',
442
            'degrees.new.*.blockcert_url'  => 'nullable|string',
443
        ]);
444
445
        // Save new degrees.
446
        if (isset($degrees['new'])) {
447
            foreach ($degrees['new'] as $degreeInput) {
448
                $degree = new Degree();
449
                $degree->fill([
450
                    'degree_type_id' => $degreeInput['degree_type_id'],
451
                    'area_of_study' => $degreeInput['area_of_study'],
452
                    'institution' => $degreeInput['institution'],
453
                    'thesis' => $degreeInput['thesis'],
454
                    'start_date' => $degreeInput['start_date'],
455
                    'end_date' => $degreeInput['end_date'],
456
                    'blockcert_url' => $degreeInput['blockcert_url'],
457
                ]);
458
                $applicant->degrees()->save($degree);
459
            }
460
        }
461
462
        // Update old degrees.
463
        if (isset($degrees['old'])) {
464
            foreach ($degrees['old'] as $id => $degreeInput) {
465
                // Ensure this degree belongs to this applicant.
466
                $degree = $applicant->degrees->firstWhere('id', $id);
467
                if ($degree != null) {
468
                    $degree->fill([
469
                        'degree_type_id' => $degreeInput['degree_type_id'],
470
                        'area_of_study' => $degreeInput['area_of_study'],
471
                        'institution' => $degreeInput['institution'],
472
                        'thesis' => $degreeInput['thesis'],
473
                        'start_date' => $degreeInput['start_date'],
474
                        'end_date' => $degreeInput['end_date'],
475
                        'blockcert_url' => $degreeInput['blockcert_url'],
476
                    ]);
477
                    $degree->save();
478
                } else {
479
                    Log::warning("Applicant $applicant->id attempted to update degree with invalid id: $id");
480
                }
481
            }
482
        }
483
484
        $courses = $request->input('courses');
485
486
        $request->validate([
487
            'courses.new.*.name'             => 'required',
488
            'courses.new.*.institution'      => 'required',
489
            'courses.new.*.course_status_id' => 'required',
490
            'courses.new.*.start_date'       => 'required|date',
491
            'courses.new.*.end_date'         => 'required|date',
492
        ]);
493
494
        // Save new courses.
495
        if (isset($courses['new'])) {
496
            foreach ($courses['new'] as $courseInput) {
497
                $course = new Course();
498
                $course->fill([
499
                    'name' => $courseInput['name'],
500
                    'institution' => $courseInput['institution'],
501
                    'course_status_id' => $courseInput['course_status_id'],
502
                    'start_date' => $courseInput['start_date'],
503
                    'end_date' => $courseInput['end_date']
504
                ]);
505
                $applicant->courses()->save($course);
506
            }
507
        }
508
509
        // Update old courses.
510
        if (isset($courses['old'])) {
511
            foreach ($courses['old'] as $id => $courseInput) {
512
                // Ensure this course belongs to this applicant.
513
                $course = $applicant->courses->firstWhere('id', $id);
514
                if ($course != null) {
515
                    $course->fill([
516
                        'name' => $courseInput['name'],
517
                        'institution' => $courseInput['institution'],
518
                        'course_status_id' => $courseInput['course_status_id'],
519
                        'start_date' => $courseInput['start_date'],
520
                        'end_date' => $courseInput['end_date']
521
                    ]);
522
                    $course->save();
523
                } else {
524
                    Log::warning("Applicant $applicant->id attempted to update course with invalid id: $id");
525
                }
526
            }
527
        }
528
529
        $work_experiences = $request->input('work_experiences');
530
531
        $request->validate([
532
            'work_experiences.new.*.role'        => 'required',
533
            'work_experiences.new.*.company'     => 'required',
534
            'work_experiences.new.*.description' => 'required',
535
            'work_experiences.new.*.start_date'  => 'required|date',
536
            'work_experiences.new.*.end_date'    => 'required|date',
537
        ]);
538
539
        // Save new work_experiences.
540
        if (isset($work_experiences['new'])) {
541
            foreach ($work_experiences['new'] as $workExperienceInput) {
542
                $workExperience = new WorkExperience();
543
                $workExperience->fill([
544
                    'role' => $workExperienceInput['role'],
545
                    'company' => $workExperienceInput['company'],
546
                    'description' => $workExperienceInput['description'],
547
                    'start_date' => $workExperienceInput['start_date'],
548
                    'end_date' => $workExperienceInput['end_date']
549
                ]);
550
                $applicant->work_experiences()->save($workExperience);
551
            }
552
        }
553
554
        // Update old work_experiences.
555
        if (isset($work_experiences['old'])) {
556
            foreach ($work_experiences['old'] as $id => $workExperienceInput) {
557
                // Ensure this work_experience belongs to this applicant.
558
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
559
                if ($workExperience != null) {
560
                    $workExperience->fill([
561
                        'role' => $workExperienceInput['role'],
562
                        'company' => $workExperienceInput['company'],
563
                        'description' => $workExperienceInput['description'],
564
                        'start_date' => $workExperienceInput['start_date'],
565
                        'end_date' => $workExperienceInput['end_date']
566
                    ]);
567
                    $workExperience->save();
568
                } else {
569
                    Log::warning("Applicant $applicant->id attempted to update work_experience with invalid id: $id");
570
                }
571
            }
572
        }
573
574
        // Redirect to correct page.
575
        switch ($request->input('submit')) {
576
            case 'save_and_quit':
577
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

577
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
578
                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...
579
            case 'save_and_continue':
580
            case 'next':
581
                return redirect()->route('job.application.edit.3', $jobPoster);
582
                break;
583
            case 'previous':
584
                return redirect()->route('job.application.edit.1', $jobPoster);
585
                break;
586
            default:
587
                return redirect()->back()->withInput();
588
        }
589
    }
590
591
    /**
592
     * Update the Application Essential Skills in storage for the specified job.
593
     *
594
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
595
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
596
     * @return \Illuminate\Http\Response
597
     */
598
    public function updateEssentialSkills(Request $request, JobPoster $jobPoster)
599
    {
600
        $applicant = Auth::user()->applicant;
601
        $application = $this->getApplicationFromJob($jobPoster);
602
603
        // Ensure user has permissions to update this application.
604
        $this->authorize('update', $application);
605
606
        $skillDeclarations = $request->input('skill_declarations');
607
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
608
609
        // Save new skill declarations.
610
        if (isset($skillDeclarations['new'])) {
611
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
612
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
613
                    $skillDeclaration = new SkillDeclaration();
614
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
615
                    $skillDeclaration->skill_status_id = $claimedStatusId;
616
                    $skillDeclaration->fill([
617
                        'description' => $skillDeclarationInput['description'],
618
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
619
                    ]);
620
                    $applicant->skill_declarations()->save($skillDeclaration);
621
622
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
623
                    $skillDeclaration->references()->sync($referenceIds);
624
625
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
626
                    $skillDeclaration->work_samples()->sync($sampleIds);
627
                }
628
            }
629
        }
630
631
        // Update old declarations.
632
        if (isset($skillDeclarations['old'])) {
633
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
634
                foreach ($typeInput as $id => $skillDeclarationInput) {
635
                    // Ensure this declaration belongs to this applicant.
636
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
637
                    if ($skillDeclaration != null) {
638
                        // skill_id and skill_status cannot be changed.
639
                        $skillDeclaration->fill([
640
                            'description' => $skillDeclarationInput['description'],
641
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
642
                        ]);
643
                        $skillDeclaration->save();
644
645
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
646
                        $skillDeclaration->references()->sync($referenceIds);
647
648
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
649
                        $skillDeclaration->work_samples()->sync($sampleIds);
650
                    } else {
651
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
652
                    }
653
                }
654
            }
655
        }
656
657
        // Redirect to correct page.
658
        switch ($request->input('submit')) {
659
            case 'save_and_quit':
660
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

660
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
661
                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...
662
            case 'save_and_continue':
663
            case 'next':
664
                return redirect()->route('job.application.edit.4', $jobPoster);
665
                break;
666
            case 'previous':
667
                return redirect()->route('job.application.edit.2', $jobPoster);
668
                break;
669
            default:
670
                return redirect()->back()->withInput();
671
        }
672
    }
673
674
    /**
675
     * Update the Application Asset Skills in storage for the specified job.
676
     *
677
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
678
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
679
     * @return \Illuminate\Http\Response
680
     */
681
    public function updateAssetSkills(Request $request, JobPoster $jobPoster)
682
    {
683
        $applicant = Auth::user()->applicant;
684
        $application = $this->getApplicationFromJob($jobPoster);
685
686
        // Ensure user has permissions to update this application.
687
        $this->authorize('update', $application);
688
689
        $skillDeclarations = $request->input('skill_declarations');
690
        $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id;
691
692
        // Save new skill declarations.
693
        if (isset($skillDeclarations['new'])) {
694
            foreach ($skillDeclarations['new'] as $skillType => $typeInput) {
695
                foreach ($typeInput as $criterion_id => $skillDeclarationInput) {
696
                    $skillDeclaration = new SkillDeclaration();
697
                    $skillDeclaration->skill_id = Criteria::find($criterion_id)->skill->id;
698
                    $skillDeclaration->skill_status_id = $claimedStatusId;
699
                    $skillDeclaration->fill([
700
                        'description' => $skillDeclarationInput['description'],
701
                        'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
702
                    ]);
703
                    $applicant->skill_declarations()->save($skillDeclaration);
704
705
                    $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
706
                    $skillDeclaration->references()->sync($referenceIds);
707
708
                    $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
709
                    $skillDeclaration->work_samples()->sync($sampleIds);
710
                }
711
            }
712
        }
713
714
        // Update old declarations.
715
        if (isset($skillDeclarations['old'])) {
716
            foreach ($skillDeclarations['old'] as $skillType => $typeInput) {
717
                foreach ($typeInput as $id => $skillDeclarationInput) {
718
                    // Ensure this declaration belongs to this applicant.
719
                    $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id);
720
                    if ($skillDeclaration != null) {
721
                        // skill_id and skill_status cannot be changed.
722
                        $skillDeclaration->fill([
723
                            'description' => $skillDeclarationInput['description'],
724
                            'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null,
725
                        ]);
726
                        $skillDeclaration->save();
727
728
                        $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references');
729
                        $skillDeclaration->references()->sync($referenceIds);
730
731
                        $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples');
732
                        $skillDeclaration->work_samples()->sync($sampleIds);
733
                    } else {
734
                        Log::warning("Applicant $applicant->id attempted to update skill declaration with invalid id: $id");
735
                    }
736
                }
737
            }
738
        }
739
740
        // Redirect to correct page.
741
        switch ($request->input('submit')) {
742
            case 'save_and_quit':
743
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

743
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
744
                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...
745
            case 'save_and_continue':
746
            case 'next':
747
                return redirect()->route('job.application.edit.5', $jobPoster);
748
                break;
749
            case 'previous':
750
                return redirect()->route('job.application.edit.3', $jobPoster);
751
                break;
752
            default:
753
                return redirect()->back()->withInput();
754
        }
755
    }
756
757
    /**
758
     * Submit the Application for the specified job.
759
     *
760
     * @param  \Illuminate\Http\Request $request   Incoming Request object.
761
     * @param  \App\Models\JobPoster    $jobPoster Incoming Job Poster object.
762
     * @return \Illuminate\Http\Response
763
     */
764
    public function submit(Request $request, JobPoster $jobPoster)
765
    {
766
        $application = $this->getApplicationFromJob($jobPoster);
767
768
        // Only complete submission if submit button was pressed.
769
        if ($request->input('submit') == 'submit' && $application->application_status->name == 'draft') {
770
            // Ensure user has permissions to update this application.
771
            $this->authorize('update', $application);
772
            $request->validate([
773
                'submission_signature' => [
774
                    'required',
775
                    'string',
776
                    'max:191',
777
                ],
778
                'submission_date' => [
779
                    'required',
780
                    'string',
781
                    'max:191',
782
                ]
783
            ]);
784
785
            // Save any final info.
786
            $application->fill([
787
                'submission_signature' => $request->input('submission_signature'),
788
                'submission_date' => $request->input('submission_date'),
789
            ]);
790
791
            // Error out of this process now if application is not complete.
792
            $validator = new ApplicationValidator();
793
            $validator->validate($application);
794
795
            // Change status to 'submitted'.
796
            $application->application_status_id = ApplicationStatus::where('name', 'submitted')->firstOrFail()->id;
797
            $application->saveProfileSnapshot();
798
        }
799
800
        $application->save();
801
802
        // Redirect to correct page.
803
        switch ($request->input('submit')) {
804
            case 'save_and_quit':
805
                return redirect()->route('applications.index');
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

805
                return /** @scrutinizer ignore-call */ redirect()->route('applications.index');
Loading history...
806
                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...
807
            case 'submit':
808
                return redirect()->route('job.application.complete', $jobPoster);
809
                break;
810
            case 'previous':
811
                return redirect()->route('job.application.edit.4', $jobPoster);
812
                break;
813
            default:
814
                return redirect()->back()->withInput();
815
        }
816
    }
817
}
818