Conditions | 5 |
Paths | 2 |
Total Lines | 52 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
33 | public function updateProfile(UpdateApplicantProfile $request, Applicant $applicant) |
||
34 | { |
||
35 | $validatedRequest = $request->validated(); |
||
36 | // If there are no applicant classifications in the request, |
||
37 | // then delete all applicant classifications attached to applicant. |
||
38 | if (!array_key_exists('applicant_classifications', $validatedRequest)) { |
||
39 | $applicant->applicant_classifications()->delete(); |
||
40 | } else { |
||
41 | $newApplicantClassifications = collect($validatedRequest['applicant_classifications'])->unique( |
||
|
|||
42 | // Remove all duplicate classification-level combinations from the collection. |
||
43 | function ($newApplicantClassification) { |
||
44 | return $newApplicantClassification['classification_id'].$newApplicantClassification['level']; |
||
45 | } |
||
46 | ); |
||
47 | $oldApplicantClassifications = $applicant->applicant_classifications; |
||
48 | |||
49 | // Delete old applicant classifications that were not resubmitted. |
||
50 | foreach ($oldApplicantClassifications as $oldApplicantClassification) { |
||
51 | $newApplicantClassification = $newApplicantClassifications->firstWhere( |
||
52 | 'id', |
||
53 | $oldApplicantClassification['id'] |
||
54 | ); |
||
55 | if ($newApplicantClassification === null) { |
||
56 | $oldApplicantClassification->delete(); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | |||
61 | // Update old applicant classifications and/or create them if it doesn't exist. |
||
62 | $newApplicantClassifications->map(function ($newApplicantClassification) use ($oldApplicantClassifications) { |
||
63 | $applicantClassification = $oldApplicantClassifications->firstWhere( |
||
64 | 'id', |
||
65 | $newApplicantClassification['id'] |
||
66 | ); |
||
67 | if (!$applicantClassification) { |
||
68 | $applicantClassification = new ApplicantClassification(); |
||
69 | } |
||
70 | $applicantClassification->applicant_id = $newApplicantClassification['applicant_id']; |
||
71 | $applicantClassification->classification_id = $newApplicantClassification['classification_id']; |
||
72 | $applicantClassification->fill($newApplicantClassification); |
||
73 | $applicantClassification->save(); |
||
74 | }); |
||
75 | } |
||
76 | |||
77 | $applicant->citizenship_declaration_id = $validatedRequest['citizenship_declaration_id']; |
||
78 | $applicant->veteran_status_id = $validatedRequest['veteran_status_id']; |
||
79 | $applicant->save(); |
||
80 | |||
81 | $applicant->refresh(); |
||
82 | $applicant->loadMissing('applicant_classifications'); |
||
83 | |||
84 | return new ApplicantProfileResource($applicant); |
||
85 | } |
||
87 |