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