| Conditions | 5 |
| Paths | 2 |
| Total Lines | 52 |
| Code Lines | 30 |
| 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 |
||
| 81 | public function updateProfile(UpdateApplicantProfile $request, Applicant $applicant) |
||
| 82 | { |
||
| 83 | $validatedRequest = $request->validated(); |
||
| 84 | // If there are no applicant classifications in the request, |
||
| 85 | // then delete all applicant classifications attached to applicant. |
||
| 86 | if (!array_key_exists('applicant_classifications', $validatedRequest)) { |
||
| 87 | $applicant->applicant_classifications()->delete(); |
||
| 88 | } else { |
||
| 89 | $newApplicantClassifications = collect($validatedRequest['applicant_classifications'])->unique( |
||
| 90 | // Remove all duplicate classification-level combinations from the collection. |
||
| 91 | function ($newApplicantClassification) { |
||
| 92 | return $newApplicantClassification['classification_id'].$newApplicantClassification['level']; |
||
| 93 | } |
||
| 94 | ); |
||
| 95 | $oldApplicantClassifications = $applicant->applicant_classifications; |
||
| 96 | |||
| 97 | // Delete old applicant classifications that were not resubmitted. |
||
| 98 | foreach ($oldApplicantClassifications as $oldApplicantClassification) { |
||
| 99 | $newApplicantClassification = $newApplicantClassifications->firstWhere( |
||
| 100 | 'id', |
||
| 101 | $oldApplicantClassification['id'] |
||
| 102 | ); |
||
| 103 | if ($newApplicantClassification === null) { |
||
| 104 | $oldApplicantClassification->delete(); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | |||
| 109 | // Update old applicant classifications and/or create them if it doesn't exist. |
||
| 110 | $newApplicantClassifications->map(function ($newApplicantClassification) use ($oldApplicantClassifications) { |
||
| 111 | $applicantClassification = $oldApplicantClassifications->firstWhere( |
||
| 112 | 'id', |
||
| 113 | $newApplicantClassification['id'] |
||
| 114 | ); |
||
| 115 | if (!$applicantClassification) { |
||
| 116 | $applicantClassification = new ApplicantClassification(); |
||
| 117 | } |
||
| 118 | $applicantClassification->applicant_id = $newApplicantClassification['applicant_id']; |
||
| 119 | $applicantClassification->classification_id = $newApplicantClassification['classification_id']; |
||
| 120 | $applicantClassification->fill($newApplicantClassification); |
||
| 121 | $applicantClassification->save(); |
||
| 122 | }); |
||
| 123 | } |
||
| 124 | |||
| 125 | $applicant->citizenship_declaration_id = $validatedRequest['citizenship_declaration_id']; |
||
| 126 | $applicant->veteran_status_id = $validatedRequest['veteran_status_id']; |
||
| 127 | $applicant->save(); |
||
| 128 | |||
| 129 | $applicant->refresh(); |
||
| 130 | $applicant->loadMissing('applicant_classifications'); |
||
| 131 | |||
| 132 | return new ApplicantProfileResource($applicant); |
||
| 133 | } |
||
| 135 |