| Conditions | 25 |
| Paths | 1728 |
| Total Lines | 154 |
| Code Lines | 91 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 650 |
| 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 | //Delete old degrees that weren't resubmitted |
||
| 65 | //Note: this must be done before adding new degrees, so we don't delete |
||
| 66 | // them right after adding them |
||
| 67 | foreach($applicant->degrees as $oldDegree) { |
||
| 68 | //Check if no degrees were resubmitted, or if this specific one wasn't |
||
| 69 | if (!isset($degrees['old']) || |
||
| 70 | !isset($degrees['old'][$oldDegree->id])) { |
||
| 71 | $oldDegree->delete(); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | //Save new degrees |
||
| 76 | if (isset($degrees['new'])) { |
||
| 77 | foreach($degrees['new'] as $degreeInput) { |
||
| 78 | $degree = new Degree(); |
||
| 79 | $degree->applicant_id = $applicant->id; |
||
| 80 | $degree->fill([ |
||
| 81 | 'degree_type_id' => $degreeInput['degree_type_id'], |
||
| 82 | 'area_of_study' => $degreeInput['area_of_study'], |
||
| 83 | 'institution' => $degreeInput['institution'], |
||
| 84 | 'thesis' => $degreeInput['thesis'], |
||
| 85 | 'start_date' => $degreeInput['start_date'], |
||
| 86 | 'end_date' => $degreeInput['end_date'] |
||
| 87 | ]); |
||
| 88 | $degree->save(); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | //Update old degrees |
||
| 93 | if (isset($degrees['old'])) { |
||
| 94 | foreach($degrees['old'] as $id=>$degreeInput) { |
||
| 95 | //Ensure this degree belongs to this applicant |
||
| 96 | $degree = $applicant->degrees->firstWhere('id', $id); |
||
| 97 | if ($degree != null) { |
||
| 98 | $degree->fill([ |
||
| 99 | 'degree_type_id' => $degreeInput['degree_type_id'], |
||
| 100 | 'area_of_study' => $degreeInput['area_of_study'], |
||
| 101 | 'institution' => $degreeInput['institution'], |
||
| 102 | 'thesis' => $degreeInput['thesis'], |
||
| 103 | 'start_date' => $degreeInput['start_date'], |
||
| 104 | 'end_date' => $degreeInput['end_date'] |
||
| 105 | ]); |
||
| 106 | $degree->save(); |
||
| 107 | } else { |
||
| 108 | Debugbar::warning('Applicant '.$applicant->id.' attempted to update degree with invalid id '.$id); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | $courses = $input['courses']; |
||
| 114 | |||
| 115 | //Delete old courses that weren't resubmitted |
||
| 116 | //Note: this must be done before adding new ones, so we don't delete |
||
| 117 | // them right after adding them |
||
| 118 | foreach($applicant->courses as $oldCourse) { |
||
| 119 | //Check if no courses were resubmitted, or if this specific one wasn't |
||
| 120 | if (!isset($courses['old']) || |
||
| 121 | !isset($courses['old'][$oldCourse->id])) { |
||
| 122 | $oldCourse->delete(); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | //Save new courses |
||
| 127 | if (isset($courses['new'])) { |
||
| 128 | foreach($courses['new'] as $courseInput) { |
||
| 129 | $course = new Course(); |
||
| 130 | $course->applicant_id = $applicant->id; |
||
| 131 | $course->fill([ |
||
| 132 | 'name' => $courseInput['name'], |
||
| 133 | 'institution' => $courseInput['institution'], |
||
| 134 | 'course_status_id' => $courseInput['course_status_id'], |
||
| 135 | 'start_date' => $courseInput['start_date'], |
||
| 136 | 'end_date' => $courseInput['end_date'] |
||
| 137 | ]); |
||
| 138 | $course->save(); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | //Update old courses |
||
| 143 | if (isset($courses['old'])) { |
||
| 144 | foreach($courses['old'] as $id=>$courseInput) { |
||
| 145 | //Ensure this course belongs to this applicant |
||
| 146 | $course = $applicant->courses->firstWhere('id', $id); |
||
| 147 | if ($course != null) { |
||
| 148 | $course->fill([ |
||
| 149 | 'name' => $courseInput['name'], |
||
| 150 | 'institution' => $courseInput['institution'], |
||
| 151 | 'course_status_id' => $courseInput['course_status_id'], |
||
| 152 | 'start_date' => $courseInput['start_date'], |
||
| 153 | 'end_date' => $courseInput['end_date'] |
||
| 154 | ]); |
||
| 155 | $course->save(); |
||
| 156 | } else { |
||
| 157 | Debugbar::warning('Applicant '.$applicant->id.' attempted to update course with invalid id '.$id); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | $work_experiences = $input['work_experiences'] ; |
||
| 163 | |||
| 164 | //Delete old work_experiences that weren't resubmitted |
||
| 165 | //Note: this must be done before adding new ones, so we don't delete |
||
| 166 | // them right after adding them |
||
| 167 | foreach($applicant->work_experiences as $oldWorkExperience) { |
||
| 168 | //Check if no work_experiences were resubmitted, or if this specific one wasn't |
||
| 169 | if (!isset($work_experiences['old']) || |
||
| 170 | !isset($work_experiences['old'][$oldWorkExperience->id])) { |
||
| 171 | $oldWorkExperience->delete(); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | //Save new work_experiences |
||
| 176 | if (isset($work_experiences['new'])) { |
||
| 177 | foreach($work_experiences['new'] as $workExperienceInput) { |
||
| 178 | $workExperience = new WorkExperience(); |
||
| 179 | $workExperience->applicant_id = $applicant->id; |
||
| 180 | $workExperience->fill([ |
||
| 181 | 'role' => $workExperienceInput['role'], |
||
| 182 | 'company' => $workExperienceInput['company'], |
||
| 183 | 'description' => $workExperienceInput['description'], |
||
| 184 | 'start_date' => $workExperienceInput['start_date'], |
||
| 185 | 'end_date' => $workExperienceInput['end_date'] |
||
| 186 | ]); |
||
| 187 | $workExperience->save(); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | //Update old work_experiences |
||
| 192 | if (isset($work_experiences['old'])) { |
||
| 193 | foreach($work_experiences['old'] as $id=>$workExperienceInput) { |
||
| 194 | //Ensure this work_experience belongs to this applicant |
||
| 195 | $workExperience = $applicant->work_experiences->firstWhere('id', $id); |
||
| 196 | if ($workExperience != null) { |
||
| 197 | $workExperience->fill([ |
||
| 198 | 'role' => $workExperienceInput['role'], |
||
| 199 | 'company' => $workExperienceInput['company'], |
||
| 200 | 'description' => $workExperienceInput['description'], |
||
| 201 | 'start_date' => $workExperienceInput['start_date'], |
||
| 202 | 'end_date' => $workExperienceInput['end_date'] |
||
| 203 | ]); |
||
| 204 | $workExperience->save(); |
||
| 205 | } else { |
||
| 206 | Debugbar::warning('Applicant '.$applicant->id.' attempted to update work_experience with invalid id '.$id); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | return redirect( route('profile.experience.edit', $applicant) ); |
||
|
1 ignored issue
–
show
|
|||
| 212 | // Debugbar::info($input); |
||
| 226 |