Passed
Push — feature/experience-api ( 5f417f...bbfe27 )
by Tristan
04:24 queued 22s
created

ExperienceController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 223
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 122
c 0
b 0
f 0
dl 0
loc 223
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A editAuthenticated() 0 4 1
A edit() 0 13 1
F update() 0 179 25
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\Http\Controllers\Controller;
13
14
class ExperienceController extends Controller
15
{
16
    /**
17
     * Show the form for editing the logged-in applicant's experience
18
     *
19
     * @param  \Illuminate\Http\Request $request Incoming Request.
20
     * @return \Illuminate\Http\Response
21
     */
22
    public function editAuthenticated(Request $request)
23
    {
24
        $applicant = $request->user()->applicant;
25
        return redirect(route('profile.experience.edit', $applicant));
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

25
        return /** @scrutinizer ignore-call */ redirect(route('profile.experience.edit', $applicant));
Loading history...
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

25
        return redirect(/** @scrutinizer ignore-call */ route('profile.experience.edit', $applicant));
Loading history...
26
    }
27
28
    /**
29
     * Show the form for editing the applicant's experience
30
     *
31
     * @param  \Illuminate\Http\Request $request   Incoming request object.
32
     * @param  \App\Models\Applicant    $applicant Incoming applicant object.
33
     * @return \Illuminate\Http\Response
34
     */
35
    public function edit(Request $request, Applicant $applicant)
36
    {
37
        $custom_breadcrumbs = [
38
            'home' => route('home'),
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

38
            'home' => /** @scrutinizer ignore-call */ route('home'),
Loading history...
39
            'profile' => '',
40
        ];
41
42
        return view('applicant/profile_02_experience', [
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

42
        return /** @scrutinizer ignore-call */ view('applicant/profile_02_experience', [
Loading history...
43
            'applicant' => $applicant,
44
            'profile' => Lang::get('applicant/profile_experience'),
45
            'work_template' => Lang::get('common/work_experience'),
46
            'form_submit_action' => route('profile.experience.update', $applicant),
47
            'custom_breadcrumbs' => $custom_breadcrumbs,
48
        ]);
49
    }
50
51
    /**
52
     * Update the applicant's profile in storage.
53
     *
54
     * @param  \Illuminate\Http\Request $request   Incoming Request.
55
     * @param  \App\Models\Applicant    $applicant Incoming Applicant.
56
     * @return \Illuminate\Http\Response
57
     */
58
    public function update(Request $request, Applicant $applicant)
59
    {
60
        $input = $request->input();
61
62
        $degrees = $input['degrees'];
63
64
        $request->validate([
65
            'degrees.new.*.degree_type_id' => 'required',
66
            'degrees.new.*.area_of_study'  => 'required',
67
            'degrees.new.*.institution'    => 'required',
68
            'degrees.new.*.thesis'         => 'nullable',
69
            'degrees.new.*.start_date'     => 'required|date',
70
            'degrees.new.*.end_date'       => 'required|date',
71
            'degrees.new.*.blockcert_url'       => 'nullable|string',
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->fill([
90
                    'degree_type_id' => $degreeInput['degree_type_id'],
91
                    'area_of_study' => $degreeInput['area_of_study'],
92
                    'institution' => $degreeInput['institution'],
93
                    'thesis' => $degreeInput['thesis'],
94
                    'start_date' => $degreeInput['start_date'],
95
                    'end_date' => $degreeInput['end_date'],
96
                    'blockcert_url' => $degreeInput['blockcert_url'],
97
                ]);
98
                $applicant->degrees()->save($degree);
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
                        'blockcert_url' => $degreeInput['blockcert_url'],
116
                    ]);
117
                    $degree->save();
118
                } else {
119
                    Log::warning("Applicant $applicant->id attempted to update degree with invalid id: $id");
120
                }
121
            }
122
        }
123
124
        $courses = $input['courses'];
125
126
        $request->validate([
127
            'courses.new.*.name'             => 'required',
128
            'courses.new.*.institution'      => 'required',
129
            'courses.new.*.course_status_id' => 'required',
130
            'courses.new.*.start_date'       => 'required|date',
131
            'courses.new.*.end_date'         => 'required|date',
132
        ]);
133
134
        // Delete old courses that weren't resubmitted.
135
        // Note: this must be done before adding new ones, so we don't delete
136
        // them right after adding them.
137
        foreach ($applicant->courses as $oldCourse) {
138
            // Check if no courses were resubmitted, or if this specific one wasn't.
139
            if (!isset($courses['old']) ||
140
            !isset($courses['old'][$oldCourse->id])) {
141
                $oldCourse->delete();
142
            }
143
        }
144
145
        // Save new courses.
146
        if (isset($courses['new'])) {
147
            foreach ($courses['new'] as $courseInput) {
148
                $course = new Course();
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
                $applicant->courses()->save($course);
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
        $request->validate([
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->fill([
206
                    'role' => $workExperienceInput['role'],
207
                    'company' => $workExperienceInput['company'],
208
                    'description' => $workExperienceInput['description'],
209
                    'start_date' => $workExperienceInput['start_date'],
210
                    'end_date' => $workExperienceInput['end_date']
211
                ]);
212
                $applicant->work_experiences()->save($workExperience);
213
            }
214
        }
215
216
        // Update old work_experiences.
217
        if (isset($work_experiences['old'])) {
218
            foreach ($work_experiences['old'] as $id => $workExperienceInput) {
219
                // Ensure this work_experience belongs to this applicant.
220
                $workExperience = $applicant->work_experiences->firstWhere('id', $id);
221
                if ($workExperience != null) {
222
                    $workExperience->fill([
223
                        'role' => $workExperienceInput['role'],
224
                        'company' => $workExperienceInput['company'],
225
                        'description' => $workExperienceInput['description'],
226
                        'start_date' => $workExperienceInput['start_date'],
227
                        'end_date' => $workExperienceInput['end_date']
228
                    ]);
229
                    $workExperience->save();
230
                } else {
231
                    Log::warning("Applicant $applicant->id attempted to update work_experience with invalid id: $id");
232
                }
233
            }
234
        }
235
236
        return redirect(route('profile.experience.edit', $applicant));
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

236
        return redirect(/** @scrutinizer ignore-call */ route('profile.experience.edit', $applicant));
Loading history...
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

236
        return /** @scrutinizer ignore-call */ redirect(route('profile.experience.edit', $applicant));
Loading history...
237
    }
238
}
239