Passed
Push — task/applicant-profile-perform... ( 267d12...585f2b )
by Chris
13:18 queued 07:42
created

ExperienceController::editAuthenticated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Support\Facades\Lang;
6
use Illuminate\Support\Facades\Log;
7
use Illuminate\Http\Request;
8
use App\Models\Degree;
9
use App\Models\Applicant;
10
use App\Models\Course;
11
use App\Models\WorkExperience;
12
use App\Models\Lookup\DegreeType;
13
use App\Models\Lookup\CourseStatus;
14
use App\Http\Controllers\Controller;
15
16
class ExperienceController extends Controller
17
{
18
    /**
19
     * Show the form for editing the logged-in applicant's experience
20
     *
21
     * @param  Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
22
     * @return \Illuminate\Http\RedirectResponse
23
     */
24
    public function editAuthenticated(Request $request): \Illuminate\Http\RedirectResponse
25
    {
26
        $applicant = $request->user()->applicant;
27
        return redirect(route('profile.experience.edit', $applicant));
28
    }
29
30
    /**
31
    * Show the form for editing the applicant's experience
32
    *
33
    * @param \Illuminate\Http\Request $request   Incoming request object.
34
    * @param \App\Models\Applicant    $applicant Incoming applicant object.
35
    *
36
    * @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
37
    */
38
    public function edit(Request $request, Applicant $applicant)
39
    {
40
        return view('applicant/profile_02_experience', [
41
            'applicant' => $applicant,
42
            'profile' => Lang::get('applicant/profile_experience'),
43
            'work_template' => Lang::get('common/work_experience'),
44
            'form_submit_action' => route('profile.experience.update', $applicant)
45
        ]);
46
    }
47
48
    /**
49
     * Update the applicant's profile in storage.
50
     *
51
     * @param  \Illuminate\Http\Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
52
     * @param  \App\Models\Applicant    $applicant
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
53
     * @return \Illuminate\Http\Response
54
     */
55
    public function update(Request $request, Applicant $applicant)
0 ignored issues
show
introduced by
Method \App\Http\Controllers\ExperienceController::update() does not have return type hint for its return value but it should be possible to add it based on @return annotation "\Illuminate\Http\Response".
Loading history...
56
    {
57
        $input = $request->input();
58
59
        $degrees = $input['degrees'];
60
61
        $validatedDegreeData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedDegreeData is dead and can be removed.
Loading history...
62
            'degrees.new.*.degree_type_id' => 'required',
63
            'degrees.new.*.area_of_study'  => 'required',
64
            'degrees.new.*.institution'    => 'required',
65
            'degrees.new.*.thesis'         => 'nullable',
66
            'degrees.new.*.start_date'     => 'required|date',
67
            'degrees.new.*.end_date'       => 'required|date',
68
        ]);
69
70
        //Delete old degrees that weren't resubmitted
71
        //Note: this must be done before adding new degrees, so we don't delete
72
        // them right after adding them
73
        foreach ($applicant->degrees as $oldDegree) {
74
            //Check if no degrees were resubmitted, or if this specific one wasn't
75
            if (!isset($degrees['old']) ||
76
            !isset($degrees['old'][$oldDegree->id])) {
77
                $oldDegree->delete();
78
            }
79
        }
80
81
        //Save new degrees
82
        if (isset($degrees['new'])) {
83
            foreach ($degrees['new'] as $degreeInput) {
84
                $degree = new Degree();
85
                $degree->applicant_id = $applicant->id;
86
                $degree->fill([
87
                    'degree_type_id' => $degreeInput['degree_type_id'],
88
                    'area_of_study' => $degreeInput['area_of_study'],
89
                    'institution' => $degreeInput['institution'],
90
                    'thesis' => $degreeInput['thesis'],
91
                    'start_date' => $degreeInput['start_date'],
92
                    'end_date' => $degreeInput['end_date']
93
                ]);
94
                $degree->save();
95
            }
96
        }
97
98
        //Update old degrees
99
        if (isset($degrees['old'])) {
100
            foreach ($degrees['old'] as $id => $degreeInput) {
101
                //Ensure this degree belongs to this applicant
102
                $degree = $applicant->degrees->firstWhere('id', $id);
103
                if ($degree != null) {
104
                    $degree->fill([
105
                        'degree_type_id' => $degreeInput['degree_type_id'],
106
                        'area_of_study' => $degreeInput['area_of_study'],
107
                        'institution' => $degreeInput['institution'],
108
                        'thesis' => $degreeInput['thesis'],
109
                        'start_date' => $degreeInput['start_date'],
110
                        'end_date' => $degreeInput['end_date']
111
                    ]);
112
                    $degree->save();
113
                } else {
114
                    Log::warning('Applicant '.$applicant->id.' attempted to update degree with invalid id '.$id);
115
                }
116
            }
117
        }
118
119
        $courses = $input['courses'];
120
121
        $validatedCourseData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedCourseData is dead and can be removed.
Loading history...
122
            'courses.new.*.name'             => 'required',
123
            'courses.new.*.institution'      => 'required',
124
            'courses.new.*.course_status_id' => 'required',
125
            'courses.new.*.start_date'       => 'required|date',
126
            'courses.new.*.end_date'         => 'required|date',
127
        ]);
128
129
        //Delete old courses that weren't resubmitted
130
        //Note: this must be done before adding new ones, so we don't delete
131
        // them right after adding them
132
        foreach ($applicant->courses as $oldCourse) {
133
            //Check if no courses were resubmitted, or if this specific one wasn't
134
            if (!isset($courses['old']) ||
135
            !isset($courses['old'][$oldCourse->id])) {
136
                $oldCourse->delete();
137
            }
138
        }
139
140
        //Save new courses
141
        if (isset($courses['new'])) {
142
            foreach ($courses['new'] as $courseInput) {
143
                $course = new Course();
144
                $course->applicant_id = $applicant->id;
145
                $course->fill([
146
                    'name' => $courseInput['name'],
147
                    'institution' => $courseInput['institution'],
148
                    'course_status_id' => $courseInput['course_status_id'],
149
                    'start_date' => $courseInput['start_date'],
150
                    'end_date' => $courseInput['end_date']
151
                ]);
152
                $course->save();
153
            }
154
        }
155
156
        //Update old courses
157
        if (isset($courses['old'])) {
158
            foreach ($courses['old'] as $id => $courseInput) {
159
                //Ensure this course belongs to this applicant
160
                $course = $applicant->courses->firstWhere('id', $id);
161
                if ($course != null) {
162
                    $course->fill([
163
                        'name' => $courseInput['name'],
164
                        'institution' => $courseInput['institution'],
165
                        'course_status_id' => $courseInput['course_status_id'],
166
                        'start_date' => $courseInput['start_date'],
167
                        'end_date' => $courseInput['end_date']
168
                    ]);
169
                    $course->save();
170
                } else {
171
                    Log::warning('Applicant '.$applicant->id.' attempted to update course with invalid id '.$id);
172
                }
173
            }
174
        }
175
176
        $work_experiences = $input['work_experiences'];
177
178
        $validatedWorkData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedWorkData is dead and can be removed.
Loading history...
179
            'work_experiences.new.*.role'        => 'required',
180
            'work_experiences.new.*.company'     => 'required',
181
            'work_experiences.new.*.description' => 'required',
182
            'work_experiences.new.*.start_date'  => 'required|date',
183
            'work_experiences.new.*.end_date'    => 'required|date',
184
        ]);
185
186
        //Delete old work_experiences that weren't resubmitted
187
        //Note: this must be done before adding new ones, so we don't delete
188
        // them right after adding them
189
        foreach ($applicant->work_experiences as $oldWorkExperience) {
190
            //Check if no work_experiences were resubmitted, or if this specific one wasn't
191
            if (!isset($work_experiences['old']) ||
192
            !isset($work_experiences['old'][$oldWorkExperience->id])) {
193
                $oldWorkExperience->delete();
194
            }
195
        }
196
197
        //Save new work_experiences
198
        if (isset($work_experiences['new'])) {
199
            foreach ($work_experiences['new'] as $workExperienceInput) {
200
                $workExperience = new WorkExperience();
201
                $workExperience->applicant_id = $applicant->id;
202
                $workExperience->fill([
203
                    'role' => $workExperienceInput['role'],
204
                    'company' => $workExperienceInput['company'],
205
                    'description' => $workExperienceInput['description'],
206
                    'start_date' => $workExperienceInput['start_date'],
207
                    'end_date' => $workExperienceInput['end_date']
208
                ]);
209
                $workExperience->save();
210
            }
211
        }
212
213
        //Update old work_experiences
214
        if (isset($work_experiences['old'])) {
215
            foreach ($work_experiences['old'] as $id => $workExperienceInput) {
216
                //Ensure this work_experience belongs to this applicant
217
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
218
                if ($workExperience != null) {
219
                    $workExperience->fill([
220
                        'role' => $workExperienceInput['role'],
221
                        'company' => $workExperienceInput['company'],
222
                        'description' => $workExperienceInput['description'],
223
                        'start_date' => $workExperienceInput['start_date'],
224
                        'end_date' => $workExperienceInput['end_date']
225
                    ]);
226
                    $workExperience->save();
227
                } else {
228
                    Log::warning('Applicant '.$applicant->id.' attempted to update work_experience with invalid id '.$id);
229
                }
230
            }
231
        }
232
233
        return redirect(route('profile.experience.edit', $applicant));
1 ignored issue
show
Bug Best Practice introduced by
The expression return redirect(route('p...nce.edit', $applicant)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
234
    }
235
}
236