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