| Conditions | 18 |
| Paths | 12 |
| Total Lines | 119 |
| Code Lines | 72 |
| 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 |
||
| 56 | public function updateAll(Request $request, Applicant $applicant) |
||
| 57 | { |
||
| 58 | $input = $request->input(); |
||
| 59 | |||
| 60 | $references = $input['references']; |
||
| 61 | |||
| 62 | //Delete old references that weren't resubmitted |
||
| 63 | //Note: this must be done before adding new references, so we don't delete |
||
| 64 | // them right after adding them |
||
| 65 | foreach($applicant->references as $oldReference) { |
||
| 66 | //Check if no references were resubmitted, or if this specific one wasn't |
||
| 67 | if (!isset($references['old']) || |
||
| 68 | !isset($references['old'][$oldReference->id])) { |
||
| 69 | $oldReference->delete(); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | //Save new references |
||
| 74 | if (isset($references['new'])) { |
||
| 75 | foreach($references['new'] as $referenceInput) { |
||
| 76 | $reference = new Reference(); |
||
| 77 | $reference->applicant_id = $applicant->id; |
||
| 78 | $reference->fill([ |
||
| 79 | 'name' => $referenceInput['name'], |
||
| 80 | 'email' => $referenceInput['email'], |
||
| 81 | 'relationship_id' => $referenceInput['relationship_id'], |
||
| 82 | 'description' => $referenceInput['description'], |
||
| 83 | ]); |
||
| 84 | |||
| 85 | $reference->save(); |
||
| 86 | |||
| 87 | $projectIds = []; |
||
| 88 | $projects = $referenceInput['projects']; |
||
| 89 | if (isset($projects['new'])) { |
||
| 90 | foreach($projects['new'] as $projectInput) { |
||
| 91 | $project = new Project(); |
||
| 92 | $project->applicant_id = $applicant->id; |
||
| 93 | $project->fill([ |
||
| 94 | 'name' => $projectInput['name'], |
||
| 95 | 'start_date' => $projectInput['start_date'], |
||
| 96 | 'end_date' => $projectInput['end_date'], |
||
| 97 | ]); |
||
| 98 | $project->save(); |
||
| 99 | $projectIds[] = $project->id; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | //Sync attaches the specified ids, and detaches all others |
||
| 103 | $reference->projects()->sync($projectIds); |
||
| 104 | |||
| 105 | |||
| 106 | $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); |
||
| 107 | $reference->skill_declarations()->sync($skillDeclarationIds); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | //Update old references |
||
| 112 | if (isset($references['old'])) { |
||
| 113 | foreach($references['old'] as $id=>$referenceInput) { |
||
| 114 | //Ensure this reference belongs to this applicant |
||
| 115 | $reference = $applicant->references->firstWhere('id', $id); |
||
| 116 | if ($reference != null) { |
||
| 117 | $reference->fill([ |
||
| 118 | 'name' => $referenceInput['name'], |
||
| 119 | 'email' => $referenceInput['email'], |
||
| 120 | 'relationship_id' => $referenceInput['relationship_id'], |
||
| 121 | 'description' => $referenceInput['description'], |
||
| 122 | ]); |
||
| 123 | $reference->save(); |
||
| 124 | |||
| 125 | $projectIds = []; |
||
| 126 | $projects = $referenceInput['projects']; |
||
| 127 | if (isset($projects['new'])) { |
||
| 128 | foreach($projects['new'] as $projectInput) { |
||
| 129 | $project = new Project(); |
||
| 130 | $project->applicant_id = $applicant->id; |
||
| 131 | $project->fill([ |
||
| 132 | 'name' => $projectInput['name'], |
||
| 133 | 'start_date' => $projectInput['start_date'], |
||
| 134 | 'end_date' => $projectInput['end_date'], |
||
| 135 | ]); |
||
| 136 | $project->save(); |
||
| 137 | $projectIds[] = $project->id; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | if (isset($projects['old'])) { |
||
| 141 | foreach($projects['old'] as $projectId=>$projectInput) { |
||
| 142 | //Ensure this project belongs to this applicant |
||
| 143 | $project = $applicant->projects->firstWhere('id', $projectId); |
||
| 144 | if ($project != null) { |
||
| 145 | $project->fill([ |
||
| 146 | 'name' => $projectInput['name'], |
||
| 147 | 'start_date' => $projectInput['start_date'], |
||
| 148 | 'end_date' => $projectInput['end_date'], |
||
| 149 | ]); |
||
| 150 | $project->save(); |
||
| 151 | $projectIds[] = $project->id; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | //TODO: when projects exists independpently on profile, don't delete them Here |
||
| 156 | // Delete projects that will be detached from this reference |
||
| 157 | foreach($reference->projects as $project) { |
||
| 158 | if (!in_array($project->id, $projectIds)) { |
||
| 159 | $project->delete(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | //Sync attaches the specified ids, and detaches all others |
||
| 164 | $reference->projects()->sync($projectIds); |
||
| 165 | |||
| 166 | $skillDeclarationIds =$this->getRelativeIds($referenceInput, 'skills'); |
||
| 167 | $reference->skill_declarations()->sync($skillDeclarationIds); |
||
| 168 | } else { |
||
| 169 | Log::warning('Applicant '.$applicant->id.' attempted to update reference with invalid id '.$id); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | |||
| 174 | return redirect( route('profile.references.edit', $applicant) ); |
||
|
1 ignored issue
–
show
|
|||
| 175 | } |
||
| 269 |