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