| Conditions | 17 |
| Paths | 1728 |
| Total Lines | 181 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 306 |
| 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 | $newDegrees = []; |
||
| 63 | $newCourses = []; |
||
| 64 | $newWorkExperiences = []; |
||
| 65 | |||
| 66 | $oldDegrees = []; |
||
| 67 | $oldCourses = []; |
||
| 68 | $oldWorkExperiences = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * If an obj exists at $array[$key], return it. |
||
| 72 | * Otherwise, set $array[$key] to be a new instance of $class, |
||
| 73 | * set applicant_id, and return it. |
||
| 74 | * |
||
| 75 | * @param [type] $array [description] |
||
| 76 | * @param [type] $key [description] |
||
| 77 | * @param [type] $class [description] |
||
| 78 | * @return [type] [description] |
||
| 79 | */ |
||
| 80 | function getModelFromArrayOrSetNew(&$array, $key, $class, $applicant) { |
||
| 81 | if (isset($array[$key]) && $array[$key] != null ) { |
||
| 82 | $obj = $array[$key]; |
||
| 83 | } else { |
||
| 84 | $obj = new $class; |
||
| 85 | $obj->applicant_id = $applicant->id; |
||
| 86 | $array[$key] = $obj; |
||
| 87 | } |
||
| 88 | return $obj; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * For each item in an array, create a new model instance and set the |
||
| 93 | * specified attribute with the value in the array. |
||
| 94 | * |
||
| 95 | * If an instance alread exists in the $instances array that matches |
||
| 96 | * the $inputArray key, then set the attribute on that instance instead |
||
| 97 | * of creating a new instance. |
||
| 98 | * |
||
| 99 | * @param array $inputArray An array of input key-values |
||
| 100 | * @param array $instances Array of previously created Model instances. |
||
| 101 | * @param string $model Fully qualified class name. |
||
| 102 | * @param string $attribute Name of attribute to set in model. |
||
| 103 | * @param \App\Models\Applicant Owner of the object |
||
| 104 | */ |
||
| 105 | function setAttrFromInputNew($inputArray, &$instances, $model, $attribute, $applicant) { |
||
| 106 | if (isset($inputArray) && is_array($inputArray)) { |
||
| 107 | foreach($inputArray as $key => $value) { |
||
| 108 | $obj = getModelFromArrayOrSetNew($instances, $key, $model, $applicant); |
||
| 109 | $obj->setAttribute($attribute, $value); |
||
| 110 | Debugbar::info($instances); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * For each item in an input array, retrieve the modelName instance with |
||
| 117 | * id matching input key, and belonging to $applicant, and set $attribute |
||
| 118 | * on that model instance with the inputArray value. |
||
| 119 | * |
||
| 120 | * Retrieved instances are cached in $instances array, so they don't |
||
| 121 | * have to be retrieved from database multiple times. |
||
| 122 | * |
||
| 123 | * @param [type] $inputArray [description] |
||
| 124 | * @param [type] $instances [description] |
||
| 125 | * @param [type] $modelName [description] |
||
| 126 | * @param [type] $attribute [description] |
||
| 127 | */ |
||
| 128 | function setAttrFromInputOld($input, $inputField, &$instances, $modelName, $attribute, $applicant) { |
||
| 129 | if (isset($input[$inputField]) && |
||
| 130 | is_array($input[$inputField]) && |
||
| 131 | isset($input[$inputField]['old']) && |
||
| 132 | is_array($input[$inputField]['old']) ) { |
||
| 133 | |||
| 134 | $inputArray = $input[$inputField]['old']; |
||
| 135 | Debugbar::info($inputArray); |
||
| 136 | foreach($inputArray as $key => $value) { |
||
| 137 | if (isset($instances[$key])) { |
||
| 138 | $obj = $instances[$key]; |
||
| 139 | } else { |
||
| 140 | $model = new $modelName; |
||
| 141 | $obj = $model->newQuery()->where('applicant_id', $applicant->id) |
||
| 142 | ->where('id', $key)->first(); |
||
| 143 | $instances[$key] = $obj; |
||
| 144 | } |
||
| 145 | if ($obj != null) { |
||
| 146 | $obj->setAttribute($attribute, $value); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | function get(&$var, $default=null) { |
||
| 153 | return isset($var) ? $var : $default; |
||
| 154 | } |
||
| 155 | |||
| 156 | // New Entitities |
||
| 157 | setAttrFromInputNew(get($input['degree_type']['new']), $newDegrees,'App\Models\Degree', 'degree_type_id', $applicant); |
||
| 158 | setAttrFromInputNew(get($input['degree_area']['new']), $newDegrees,'App\Models\Degree', 'area_of_study', $applicant); |
||
| 159 | setAttrFromInputNew(get($input['degree_institution']['new']), $newDegrees,'App\Models\Degree', 'institution', $applicant); |
||
| 160 | setAttrFromInputNew(get($input['degree_thesis']['new']), $newDegrees,'App\Models\Degree', 'thesis', $applicant); |
||
| 161 | setAttrFromInputNew(get($input['degree_start_date']['new']), $newDegrees,'App\Models\Degree', 'start_date', $applicant); |
||
| 162 | setAttrFromInputNew(get($input['degree_end_date']['new']), $newDegrees,'App\Models\Degree', 'end_date', $applicant); |
||
| 163 | |||
| 164 | setAttrFromInputNew(get($input['course_name']['new']), $newCourses,'App\Models\Course', 'name', $applicant); |
||
| 165 | setAttrFromInputNew(get($input['course_institution']['new']), $newCourses,'App\Models\Course', 'institution', $applicant); |
||
| 166 | setAttrFromInputNew(get($input['course_status']['new']), $newCourses,'App\Models\Course', 'course_status_id', $applicant); |
||
| 167 | setAttrFromInputNew(get($input['course_start_date']['new']), $newCourses,'App\Models\Course', 'start_date', $applicant); |
||
| 168 | setAttrFromInputNew(get($input['course_end_date']['new']), $newCourses,'App\Models\Course', 'end_date', $applicant); |
||
| 169 | |||
| 170 | setAttrFromInputNew(get($input['work_role']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'role', $applicant); |
||
| 171 | setAttrFromInputNew(get($input['work_company']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'company', $applicant); |
||
| 172 | setAttrFromInputNew(get($input['work_description']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'description', $applicant); |
||
| 173 | setAttrFromInputNew(get($input['work_start_date']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'start_date', $applicant); |
||
| 174 | setAttrFromInputNew(get($input['work_end_date']['new']), $newWorkExperiences,'App\Models\WorkExperience', 'end_date', $applicant); |
||
| 175 | |||
| 176 | // PreExisting entities |
||
| 177 | setAttrFromInputOld($input, 'degree_type', $oldDegrees,'App\Models\Degree', 'degree_type_id', $applicant); |
||
| 178 | setAttrFromInputOld($input, 'degree_area', $oldDegrees,'App\Models\Degree', 'area_of_study', $applicant); |
||
| 179 | setAttrFromInputOld($input, 'degree_institution', $oldDegrees,'App\Models\Degree', 'institution', $applicant); |
||
| 180 | setAttrFromInputOld($input, 'degree_thesis', $oldDegrees,'App\Models\Degree', 'thesis', $applicant); |
||
| 181 | setAttrFromInputOld($input, 'degree_start_date', $oldDegrees,'App\Models\Degree', 'start_date', $applicant); |
||
| 182 | setAttrFromInputOld($input, 'degree_end_date', $oldDegrees,'App\Models\Degree', 'end_date', $applicant); |
||
| 183 | |||
| 184 | setAttrFromInputOld($input, 'course_name', $oldCourses,'App\Models\Course', 'name', $applicant); |
||
| 185 | setAttrFromInputOld($input, 'course_institution', $oldCourses,'App\Models\Course', 'institution', $applicant); |
||
| 186 | setAttrFromInputOld($input, 'course_status', $oldCourses,'App\Models\Course', 'course_status_id', $applicant); |
||
| 187 | setAttrFromInputOld($input, 'course_start_date', $oldCourses,'App\Models\Course', 'start_date', $applicant); |
||
| 188 | setAttrFromInputOld($input, 'course_end_date', $oldCourses,'App\Models\Course', 'end_date', $applicant); |
||
| 189 | |||
| 190 | setAttrFromInputOld($input, 'work_role', $oldWorkExperiences,'App\Models\WorkExperience', 'role', $applicant); |
||
| 191 | setAttrFromInputOld($input, 'work_company', $oldWorkExperiences,'App\Models\WorkExperience', 'company', $applicant); |
||
| 192 | setAttrFromInputOld($input, 'work_description', $oldWorkExperiences,'App\Models\WorkExperience', 'description', $applicant); |
||
| 193 | setAttrFromInputOld($input, 'work_start_date', $oldWorkExperiences,'App\Models\WorkExperience', 'start_date', $applicant); |
||
| 194 | setAttrFromInputOld($input, 'work_end_date', $oldWorkExperiences,'App\Models\WorkExperience', 'end_date', $applicant); |
||
| 195 | |||
| 196 | //Delete old entities that weren't resubmitted |
||
| 197 | foreach($applicant->degrees as $degree) { |
||
| 198 | Debugbar::info($oldCourses); |
||
| 199 | Debugbar::info($degree); |
||
| 200 | if (!isset($oldDegrees[$degree->id]) || $oldDegrees[$degree->id] === null) { |
||
| 201 | Debugbar::info('Deleting degree ' . $degree->id); |
||
| 202 | $degree->delete(); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | foreach($applicant->courses as $course) { |
||
| 206 | if (!isset($oldCourses[$course->id]) || $oldCourses[$course->id] === null) { |
||
| 207 | $course->delete(); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | foreach($applicant->work_experiences as $work) { |
||
| 211 | if (!isset($oldWorkExperiences[$work->id]) || $oldWorkExperiences[$work->id] === null) { |
||
| 212 | $work->delete(); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | //Save new entities |
||
| 217 | foreach($newDegrees as $entity) { |
||
| 218 | $entity->save(); |
||
| 219 | } |
||
| 220 | foreach($newCourses as $entity) { |
||
| 221 | $entity->save(); |
||
| 222 | } |
||
| 223 | foreach($newWorkExperiences as $entity) { |
||
| 224 | $entity->save(); |
||
| 225 | } |
||
| 226 | |||
| 227 | //Save old entities |
||
| 228 | foreach($oldDegrees as $entity) { |
||
| 229 | $entity->save(); |
||
| 230 | } |
||
| 231 | foreach($oldCourses as $entity) { |
||
| 232 | $entity->save(); |
||
| 233 | } |
||
| 234 | foreach($oldWorkExperiences as $entity) { |
||
| 235 | $entity->save(); |
||
| 236 | } |
||
| 237 | |||
| 238 | return redirect( route('profile.experience.edit', $applicant) ); |
||
|
1 ignored issue
–
show
|
|||
| 239 | //Debugbar::info($oldDegrees); |
||
| 253 |