Passed
Push — task/activity-feed-applicants-... ( 526f6c...c3441a )
by Yonathan
05:02 queued 33s
created

ApplicationByJobController   F

Complexity

Total Complexity 76

Size/Duplication

Total Lines 788
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 76
eloc 427
c 2
b 0
f 0
dl 0
loc 788
rs 2.32

14 Methods

Rating   Name   Duplication   Size   Complexity  
A complete() 0 21 1
A editExperience() 0 22 1
A editAssetSkills() 0 35 1
C updateEssentialSkills() 0 73 14
B submit() 0 51 6
A confirm() 0 18 1
B updateBasics() 0 46 8
A index() 0 20 1
C updateAssetSkills() 0 73 14
A editBasics() 0 28 1
F updateExperience() 0 167 20
B preview() 0 51 5
A editEssentialSkills() 0 35 1
A getApplicationFromJob() 0 12 2

How to fix   Complexity   

Complex Class

Complex classes like ApplicationByJobController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ApplicationByJobController, and based on these observations, apply Extract Interface, too.

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
            'isHrAdvisor' => Auth::user()->isHrAdvisor(),
56
        ]);
57
    }
58
59
    /**
60
     * Return the current applicant's application for a given Job Poster.
61
     *
62
     * @param  \App\Models\JobPoster $jobPoster Incoming JobPoster object.
63
     * @return mixed|\App\Models\JobApplication
64
     */
65
    protected function getApplicationFromJob(JobPoster $jobPoster)
66
    {
67
        $application = JobApplication::where('applicant_id', Auth::user()->applicant->id)
68
            ->where('job_poster_id', $jobPoster->id)->first();
69
        if ($application == null) {
70
            $application = new JobApplication();
71
            $application->job_poster_id = $jobPoster->id;
72
            $application->applicant_id = Auth::user()->applicant->id;
73
            $application->application_status_id = ApplicationStatus::where('name', 'draft')->firstOrFail()->id;
74
            $application->save();
75
        }
76
        return $application;
77
    }
78
79
    /**
80
     * Show the form for editing Application basics for the specified job.
81
     *
82
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function editBasics(JobPoster $jobPoster)
86
    {
87
        $applicant = Auth::user()->applicant;
88
        $application = $this->getApplicationFromJob($jobPoster);
89
90
        // Ensure user has permissions to view and update application.
91
        $this->authorize('view', $application);
92
        $this->authorize('update', $application);
93
94
        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

94
        return /** @scrutinizer ignore-call */ view(
Loading history...
95
            'applicant/application_post_01',
96
            [
97
                // Application Template Data.
98
                'application_step' => 1,
99
                'application_template' => Lang::get('applicant/application_template'),
100
                'language_options' => PreferredLanguage::all(),
101
                'citizenship_options' => CitizenshipDeclaration::all(),
102
                'veteran_options' => VeteranStatus::all(),
103
                'preferred_language_template' => Lang::get('common/preferred_language'),
104
                'citizenship_declaration_template' => Lang::get('common/citizenship_declaration'),
105
                'veteran_status_template' => Lang::get('common/veteran_status'),
106
                // Job Data.
107
                'job' => $jobPoster,
108
                // Applicant Data.
109
                'applicant' => $applicant,
110
                'job_application' => $application,
111
                // Submission.
112
                '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

112
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.update.1', $jobPoster)
Loading history...
113
            ]
114
        );
115
    }
116
117
    /**
118
     * Show the form for editing Application Experience for the specified job.
119
     *
120
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
121
     * @return \Illuminate\Http\Response
122
     */
123
    public function editExperience(JobPoster $jobPoster)
124
    {
125
        $applicant = Auth::user()->applicant;
126
        $application = $this->getApplicationFromJob($jobPoster);
127
128
        // Ensure user has permissions to view and update application.
129
        $this->authorize('view', $application);
130
        $this->authorize('update', $application);
131
132
        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

132
        return /** @scrutinizer ignore-call */ view(
Loading history...
133
            'applicant/application_post_02',
134
            [
135
                // Application Template Data.
136
                'application_step' => 2,
137
                'application_template' => Lang::get('applicant/application_template'),
138
                // Job Data.
139
                'job' => $jobPoster,
140
                // Applicant Data.
141
                'applicant' => $applicant,
142
                'job_application' => $application,
143
                // Submission.
144
                '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

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

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

168
            '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...
169
                return $value->criteria_type->name == 'asset';
170
            }),
171
        ];
172
173
        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

173
        return /** @scrutinizer ignore-call */ view(
Loading history...
174
            'applicant/application_post_03',
175
            [
176
                // Application Template Data.
177
                'application_step' => 3,
178
                'application_template' => Lang::get('applicant/application_template'),
179
                // Job Data.
180
                'job' => $jobPoster,
181
                // Skills Data.
182
                'skills' => Skill::all(),
183
                'skill_template' => Lang::get('common/skills'),
184
                'criteria' => $criteria,
185
                // Applicant Data.
186
                'applicant' => $applicant,
187
                'job_application' => $application,
188
                // Submission.
189
                '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

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

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

213
            '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...
214
                return $value->criteria_type->name == 'asset';
215
            }),
216
        ];
217
218
        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

218
        return /** @scrutinizer ignore-call */ view(
Loading history...
219
            'applicant/application_post_04',
220
            [
221
                // Application Template Data.
222
                'application_step' => 4,
223
                'application_template' => Lang::get('applicant/application_template'),
224
                // Job Data.
225
                'job' => $jobPoster,
226
                // Skills Data.
227
                'skills' => Skill::all(),
228
                'skill_template' => Lang::get('common/skills'),
229
                'criteria' => $criteria,
230
                // Applicant Data.
231
                'applicant' => $applicant,
232
                'job_application' => $application,
233
                // Submission.
234
                '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

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

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

255
            '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...
256
                return $value->criteria_type->name == 'asset';
257
            }),
258
        ];
259
        $skillDeclarations = $application->isDraft()
260
            ? $applicant->skill_declarations
261
            : $application->skill_declarations;
262
        $degrees = $application->isDraft()
263
            ? $applicant->degrees
264
            : $application->degrees;
265
        $courses = $application->isDraft()
266
            ? $applicant->courses
267
            : $application->courses;
268
        $work_experiences = $application->isDraft()
269
            ? $applicant->work_experiences
270
            : $application->work_experiences;
271
272
        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

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

313
        return /** @scrutinizer ignore-call */ view(
Loading history...
314
            'applicant/application_post_06',
315
            [
316
                // Application Template Data.
317
                'application_step' => 6,
318
                'application_template' => Lang::get('applicant/application_template'),
319
                // Used by tracker partial.
320
                'job' => $jobPoster,
321
                'job_application' => $application,
322
                // Submission.
323
                '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

323
                'form_submit_action' => /** @scrutinizer ignore-call */ route('job.application.submit', $jobPoster)
Loading history...
324
            ]
325
        );
326
    }
327
328
    /**
329
     * Show the application submission information.
330
     *
331
     * @param  \App\Models\JobPoster $jobPoster Incoming Job Poster object.
332
     * @return \Illuminate\Http\Response
333
     */
334
    public function complete(JobPoster $jobPoster)
335
    {
336
        // Include Applicant Data.
337
        $applicant = Auth::user()->applicant;
338
        // Include Application Data.
339
        $application = $this->getApplicationFromJob($jobPoster);
340
341
        // Ensure user has permissions to view application.
342
        $this->authorize('view', $application);
343
344
        // Return the Completion View.
345
        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

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

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

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

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

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

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