Conditions | 6 |
Paths | 12 |
Total Lines | 53 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
68 | public function update(CustomerContactRequest $request, $id) |
||
69 | { |
||
70 | $cust = Customer::findOrFail($request->cust_id); |
||
71 | $cust_id = $cust->cust_id; |
||
72 | |||
73 | // If the equipment is shared, it must be assigned to the parent site |
||
74 | if($request->shared && $cust->parent_id > 0) |
||
75 | { |
||
76 | $cust_id = $cust->parent_id; |
||
77 | } |
||
78 | |||
79 | CustomerContact::find($id)->update([ |
||
80 | 'cust_id' => $cust_id, |
||
81 | 'name' => $request->name, |
||
82 | 'email' => $request->email, |
||
83 | 'shared' => $request->shared, |
||
84 | 'title' => $request->title, |
||
85 | 'note' => $request->note, |
||
86 | ]); |
||
87 | |||
88 | $updatedNumbers = []; |
||
89 | foreach($request->phones as $phone) |
||
90 | { |
||
91 | // If the number is an existing number, update it |
||
92 | if(isset($phone['id'])) |
||
93 | { |
||
94 | CustomerContactPhone::find($phone['id'])->update([ |
||
95 | 'phone_type_id' => PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id, |
||
96 | 'phone_number' => $this->cleanPhoneNumber($phone['phone_number']), |
||
97 | 'extension' => $phone['extension'], |
||
98 | ]); |
||
99 | $updatedNumbers[] = $phone['id']; |
||
100 | } |
||
101 | // Otherwise enter a new number |
||
102 | else |
||
103 | { |
||
104 | $new = CustomerContactPhone::create([ |
||
105 | 'cont_id' => $id, |
||
106 | 'phone_type_id' =>PhoneNumberType::where('description', $phone['phone_number_type']['description'])->first()->phone_type_id, |
||
107 | 'phone_number' => $this->cleanPhoneNumber($phone['phone_number']), |
||
108 | 'extension' => $phone['extension'], |
||
109 | ]); |
||
110 | $updatedNumbers[] = $new->id; |
||
111 | } |
||
112 | } |
||
113 | |||
114 | $oldContacts = CustomerContactPhone::where('cont_id', $id)->whereNotIn('id', $updatedNumbers)->get(); |
||
115 | foreach($oldContacts as $cont) |
||
116 | { |
||
117 | $cont->delete(); |
||
118 | } |
||
119 | |||
120 | return back()->with(['message' => 'Contact Updated', 'type' => 'success']); |
||
121 | } |
||
165 |