Passed
Push — task/talent-dot-test ( 0ef1e8...074f28 )
by Grant
08:57 queued 03:14
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
            'degree_types' => DegreeType::all(),
44
            'course_status' => CourseStatus::all(),
45
            'degree_template' => Lang::get('common/degree'),
46
            'course_template' => Lang::get('common/course'),
47
            'work_template' => Lang::get('common/work_experience'),
48
            'form_submit_action' => route('profile.experience.update', $applicant)
49
        ]);
50
    }
51
52
    /**
53
     * Update the applicant's profile in storage.
54
     *
55
     * @param  \Illuminate\Http\Request $request
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
56
     * @param  \App\Models\Applicant    $applicant
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter comment
Loading history...
57
     * @return \Illuminate\Http\Response
58
     */
59
    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...
60
    {
61
        $input = $request->input();
62
63
        $degrees = $input['degrees'];
64
65
        $validatedDegreeData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedDegreeData is dead and can be removed.
Loading history...
66
            'degrees.new.*.degree_type_id' => 'required',
67
            'degrees.new.*.area_of_study'  => 'required',
68
            'degrees.new.*.institution'    => 'required',
69
            'degrees.new.*.thesis'         => 'nullable',
70
            'degrees.new.*.start_date'     => 'required|date',
71
            'degrees.new.*.end_date'       => 'required|date',
72
        ]);
73
74
        //Delete old degrees that weren't resubmitted
75
        //Note: this must be done before adding new degrees, so we don't delete
76
        // them right after adding them
77
        foreach ($applicant->degrees as $oldDegree) {
78
            //Check if no degrees were resubmitted, or if this specific one wasn't
79
            if (!isset($degrees['old']) ||
80
            !isset($degrees['old'][$oldDegree->id])) {
81
                $oldDegree->delete();
82
            }
83
        }
84
85
        //Save new degrees
86
        if (isset($degrees['new'])) {
87
            foreach ($degrees['new'] as $degreeInput) {
88
                $degree = new Degree();
89
                $degree->applicant_id = $applicant->id;
90
                $degree->fill([
91
                    'degree_type_id' => $degreeInput['degree_type_id'],
92
                    'area_of_study' => $degreeInput['area_of_study'],
93
                    'institution' => $degreeInput['institution'],
94
                    'thesis' => $degreeInput['thesis'],
95
                    'start_date' => $degreeInput['start_date'],
96
                    'end_date' => $degreeInput['end_date']
97
                ]);
98
                $degree->save();
99
            }
100
        }
101
102
        //Update old degrees
103
        if (isset($degrees['old'])) {
104
            foreach ($degrees['old'] as $id => $degreeInput) {
105
                //Ensure this degree belongs to this applicant
106
                $degree = $applicant->degrees->firstWhere('id', $id);
107
                if ($degree != null) {
108
                    $degree->fill([
109
                        'degree_type_id' => $degreeInput['degree_type_id'],
110
                        'area_of_study' => $degreeInput['area_of_study'],
111
                        'institution' => $degreeInput['institution'],
112
                        'thesis' => $degreeInput['thesis'],
113
                        'start_date' => $degreeInput['start_date'],
114
                        'end_date' => $degreeInput['end_date']
115
                    ]);
116
                    $degree->save();
117
                } else {
118
                    Log::warning('Applicant '.$applicant->id.' attempted to update degree with invalid id '.$id);
119
                }
120
            }
121
        }
122
123
        $courses = $input['courses'];
124
125
        $validatedCourseData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedCourseData is dead and can be removed.
Loading history...
126
            'courses.new.*.name'             => 'required',
127
            'courses.new.*.institution'      => 'required',
128
            'courses.new.*.course_status_id' => 'required',
129
            'courses.new.*.start_date'       => 'required|date',
130
            'courses.new.*.end_date'         => 'required|date',
131
        ]);
132
133
        //Delete old courses that weren't resubmitted
134
        //Note: this must be done before adding new ones, so we don't delete
135
        // them right after adding them
136
        foreach ($applicant->courses as $oldCourse) {
137
            //Check if no courses were resubmitted, or if this specific one wasn't
138
            if (!isset($courses['old']) ||
139
            !isset($courses['old'][$oldCourse->id])) {
140
                $oldCourse->delete();
141
            }
142
        }
143
144
        //Save new courses
145
        if (isset($courses['new'])) {
146
            foreach ($courses['new'] as $courseInput) {
147
                $course = new Course();
148
                $course->applicant_id = $applicant->id;
149
                $course->fill([
150
                    'name' => $courseInput['name'],
151
                    'institution' => $courseInput['institution'],
152
                    'course_status_id' => $courseInput['course_status_id'],
153
                    'start_date' => $courseInput['start_date'],
154
                    'end_date' => $courseInput['end_date']
155
                ]);
156
                $course->save();
157
            }
158
        }
159
160
        //Update old courses
161
        if (isset($courses['old'])) {
162
            foreach ($courses['old'] as $id => $courseInput) {
163
                //Ensure this course belongs to this applicant
164
                $course = $applicant->courses->firstWhere('id', $id);
165
                if ($course != null) {
166
                    $course->fill([
167
                        'name' => $courseInput['name'],
168
                        'institution' => $courseInput['institution'],
169
                        'course_status_id' => $courseInput['course_status_id'],
170
                        'start_date' => $courseInput['start_date'],
171
                        'end_date' => $courseInput['end_date']
172
                    ]);
173
                    $course->save();
174
                } else {
175
                    Log::warning('Applicant '.$applicant->id.' attempted to update course with invalid id '.$id);
176
                }
177
            }
178
        }
179
180
        $work_experiences = $input['work_experiences'];
181
182
        $validatedWorkData = $request->validate([
0 ignored issues
show
Unused Code introduced by
The assignment to $validatedWorkData is dead and can be removed.
Loading history...
183
            'work_experiences.new.*.role'        => 'required',
184
            'work_experiences.new.*.company'     => 'required',
185
            'work_experiences.new.*.description' => 'required',
186
            'work_experiences.new.*.start_date'  => 'required|date',
187
            'work_experiences.new.*.end_date'    => 'required|date',
188
        ]);
189
190
        //Delete old work_experiences that weren't resubmitted
191
        //Note: this must be done before adding new ones, so we don't delete
192
        // them right after adding them
193
        foreach ($applicant->work_experiences as $oldWorkExperience) {
194
            //Check if no work_experiences were resubmitted, or if this specific one wasn't
195
            if (!isset($work_experiences['old']) ||
196
            !isset($work_experiences['old'][$oldWorkExperience->id])) {
197
                $oldWorkExperience->delete();
198
            }
199
        }
200
201
        //Save new work_experiences
202
        if (isset($work_experiences['new'])) {
203
            foreach ($work_experiences['new'] as $workExperienceInput) {
204
                $workExperience = new WorkExperience();
205
                $workExperience->applicant_id = $applicant->id;
206
                $workExperience->fill([
207
                    'role' => $workExperienceInput['role'],
208
                    'company' => $workExperienceInput['company'],
209
                    'description' => $workExperienceInput['description'],
210
                    'start_date' => $workExperienceInput['start_date'],
211
                    'end_date' => $workExperienceInput['end_date']
212
                ]);
213
                $workExperience->save();
214
            }
215
        }
216
217
        //Update old work_experiences
218
        if (isset($work_experiences['old'])) {
219
            foreach ($work_experiences['old'] as $id => $workExperienceInput) {
220
                //Ensure this work_experience belongs to this applicant
221
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
222
                if ($workExperience != null) {
223
                    $workExperience->fill([
224
                        'role' => $workExperienceInput['role'],
225
                        'company' => $workExperienceInput['company'],
226
                        'description' => $workExperienceInput['description'],
227
                        'start_date' => $workExperienceInput['start_date'],
228
                        'end_date' => $workExperienceInput['end_date']
229
                    ]);
230
                    $workExperience->save();
231
                } else {
232
                    Log::warning('Applicant '.$applicant->id.' attempted to update work_experience with invalid id '.$id);
233
                }
234
            }
235
        }
236
237
        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...
238
    }
239
}
240