Conditions | 25 |
Paths | 1728 |
Total Lines | 179 |
Code Lines | 110 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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)); |
||
237 | } |
||
239 |