Conditions | 14 |
Paths | 12 |
Total Lines | 74 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 210 |
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 updateAll(Request $request, Applicant $applicant) |
||
59 | { |
||
60 | $input = $request->input(); |
||
61 | |||
62 | $validator = new BulkSkillDeclarationValidator($applicant, $input); |
||
63 | $validator->validate($input); |
||
64 | |||
65 | $skillDeclarations = $request->input('skill_declarations'); |
||
66 | $claimedStatusId = SkillStatus::where('name', 'claimed')->firstOrFail()->id; |
||
67 | |||
68 | //Delete old skill declarations that weren't resubmitted |
||
69 | //Note: this must be done before adding new ones, so we don't delete |
||
70 | // them right after adding them |
||
71 | foreach($applicant->skill_declarations as $oldDeclaration) { |
||
72 | //Check if none were resubmitted, or if this specific one wasn't |
||
73 | $type = $oldDeclaration->skill->skill_type->name; |
||
74 | if (!isset($skillDeclarations['old']) || |
||
75 | !isset($skillDeclarations['old'][$type]) || |
||
76 | !isset($skillDeclarations['old'][$type][$oldDeclaration->id])) { |
||
77 | $oldDeclaration->delete(); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | //Save new skill declarartions |
||
82 | if (isset($skillDeclarations['new'])) { |
||
83 | foreach($skillDeclarations['new'] as $skillType => $typeInput) { |
||
84 | foreach($typeInput as $skillDeclarationInput) { |
||
85 | $skillDeclaration = new SkillDeclaration(); |
||
86 | $skillDeclaration->applicant_id = $applicant->id; |
||
87 | $skillDeclaration->skill_id = $skillDeclarationInput['skill_id']; |
||
88 | $skillDeclaration->skill_status_id = $claimedStatusId; |
||
89 | $skillDeclaration->fill([ |
||
90 | 'description' => $skillDeclarationInput['description'], |
||
91 | 'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
||
92 | ]); |
||
93 | $skillDeclaration->save(); |
||
94 | |||
95 | $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
||
96 | $skillDeclaration->references()->sync($referenceIds); |
||
97 | |||
98 | $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
||
99 | $skillDeclaration->work_samples()->sync($sampleIds); |
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | |||
104 | //Update old declarations |
||
105 | if (isset($skillDeclarations['old'])) { |
||
106 | foreach($skillDeclarations['old'] as $skillType => $typeInput) { |
||
107 | foreach($typeInput as $id=>$skillDeclarationInput) { |
||
108 | //Ensure this declaration belongs to this applicant |
||
109 | $skillDeclaration = $applicant->skill_declarations->firstWhere('id', $id); |
||
110 | if ($skillDeclaration != null) { |
||
111 | //skill_id and skill_status cannot be changed |
||
112 | $skillDeclaration->fill([ |
||
113 | 'description' => $skillDeclarationInput['description'], |
||
114 | 'skill_level_id' => isset($skillDeclarationInput['skill_level_id']) ? $skillDeclarationInput['skill_level_id'] : null, |
||
115 | ]); |
||
116 | $skillDeclaration->save(); |
||
117 | |||
118 | $referenceIds = $this->getRelativeIds($skillDeclarationInput, 'references'); |
||
119 | $skillDeclaration->references()->sync($referenceIds); |
||
120 | |||
121 | $sampleIds = $this->getRelativeIds($skillDeclarationInput, 'samples'); |
||
122 | $skillDeclaration->work_samples()->sync($sampleIds); |
||
123 | } else { |
||
124 | Log::warning('Applicant '.$applicant->id.' attempted to update skill declaration with invalid id '.$id); |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | |||
131 | return redirect( route('profile.skills.edit', $applicant) ); |
||
1 ignored issue
–
show
|
|||
132 | } |
||
229 |