Total Complexity | 42 |
Total Lines | 448 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MembersController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MembersController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class MembersController extends Controller |
||
22 | { |
||
23 | public function __construct() |
||
24 | { |
||
25 | $this->middleware('auth'); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Display a listing of the resource. |
||
30 | * |
||
31 | * @return Response |
||
32 | */ |
||
33 | public function index(Request $request) |
||
34 | { |
||
35 | $members = Member::indexQuery($request->sort_field, $request->sort_direction, $request->drp_start, $request->drp_end)->search('"'.$request->input('search').'"')->paginate(10); |
||
36 | $memberTotal = Member::indexQuery($request->sort_field, $request->sort_direction, $request->drp_start, $request->drp_end)->search('"'.$request->input('search').'"')->get(); |
||
37 | $count = $memberTotal->count(); |
||
38 | |||
39 | if (! $request->has('drp_start') or ! $request->has('drp_end')) { |
||
40 | $drp_placeholder = 'Select daterange filter'; |
||
41 | } else { |
||
42 | $drp_placeholder = $request->drp_start.' - '.$request->drp_end; |
||
43 | } |
||
44 | |||
45 | $request->flash(); |
||
46 | |||
47 | return view('members.index', compact('members', 'count', 'drp_placeholder', 'old_sort')); |
||
48 | } |
||
49 | |||
50 | public function active(Request $request) |
||
51 | { |
||
52 | $members = Member::active($request->sort_field, $request->sort_direction, $request->drp_start, $request->drp_end)->search('"'.$request->input('search').'"')->paginate(10); |
||
53 | $Totalmembers = Member::active($request->sort_field, $request->sort_direction, $request->drp_start, $request->drp_end)->search('"'.$request->input('search').'"')->get(); |
||
54 | $count = $Totalmembers->count(); |
||
55 | |||
56 | if (! $request->has('drp_start') or ! $request->has('drp_end')) { |
||
57 | $drp_placeholder = 'Select daterange filter'; |
||
58 | } else { |
||
59 | $drp_placeholder = $request->drp_start.' - '.$request->drp_end; |
||
60 | } |
||
61 | |||
62 | $request->flash(); |
||
63 | |||
64 | return view('members.active', compact('members', 'count', 'drp_placeholder', 'old_sort')); |
||
65 | } |
||
66 | |||
67 | public function inactive(Request $request) |
||
68 | { |
||
69 | $members = Member::inactive($request->sort_field, $request->sort_direction, $request->drp_start, $request->drp_end)->search('"'.$request->input('search').'"')->paginate(10); |
||
70 | $Totalmembers = Member::inactive($request->sort_field, $request->sort_direction, $request->drp_start, $request->drp_end)->search('"'.$request->input('search').'"')->get(); |
||
71 | $count = $Totalmembers->count(); |
||
72 | |||
73 | if (! $request->has('drp_start') or ! $request->has('drp_end')) { |
||
74 | $drp_placeholder = 'Select daterange filter'; |
||
75 | } else { |
||
76 | $drp_placeholder = $request->drp_start.' - '.$request->drp_end; |
||
77 | } |
||
78 | |||
79 | $request->flash(); |
||
80 | |||
81 | return view('members.inactive', compact('members', 'count', 'drp_placeholder', 'old_sort')); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Display the specified resource. |
||
86 | * |
||
87 | * @param int $id |
||
88 | * @return Response |
||
89 | */ |
||
90 | public function show($id) |
||
91 | { |
||
92 | $member = Member::findOrFail($id); |
||
93 | |||
94 | return view('members.show', compact('member')); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Show the form for creating a new resource. |
||
99 | * |
||
100 | * @return Response |
||
101 | */ |
||
102 | public function create() |
||
103 | { |
||
104 | // For Tax calculation |
||
105 | JavaScript::put([ |
||
106 | 'taxes' => \Utilities::getSetting('taxes'), |
||
107 | 'gymieToday' => Carbon::today()->format('Y-m-d'), |
||
108 | 'servicesCount' => Service::count(), |
||
109 | ]); |
||
110 | |||
111 | //Get Numbering mode |
||
112 | $invoice_number_mode = \Utilities::getSetting('invoice_number_mode'); |
||
113 | $member_number_mode = \Utilities::getSetting('member_number_mode'); |
||
114 | |||
115 | //Generating Invoice number |
||
116 | if ($invoice_number_mode == \constNumberingMode::Auto) { |
||
117 | $invoiceCounter = \Utilities::getSetting('invoice_last_number') + 1; |
||
118 | $invoicePrefix = \Utilities::getSetting('invoice_prefix'); |
||
119 | $invoice_number = $invoicePrefix.$invoiceCounter; |
||
120 | } else { |
||
121 | $invoice_number = ''; |
||
122 | $invoiceCounter = ''; |
||
123 | } |
||
124 | |||
125 | //Generating Member Counter |
||
126 | if ($member_number_mode == \constNumberingMode::Auto) { |
||
127 | $memberCounter = \Utilities::getSetting('member_last_number') + 1; |
||
128 | $memberPrefix = \Utilities::getSetting('member_prefix'); |
||
129 | $member_code = $memberPrefix.$memberCounter; |
||
130 | } else { |
||
131 | $member_code = ''; |
||
132 | $memberCounter = ''; |
||
133 | } |
||
134 | |||
135 | return view('members.create', compact('invoice_number', 'invoiceCounter', 'member_code', 'memberCounter', 'member_number_mode', 'invoice_number_mode')); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Store a newly created resource in storage. |
||
140 | * |
||
141 | * @return Response |
||
142 | */ |
||
143 | public function store(Request $request) |
||
144 | { |
||
145 | // Member Model Validation |
||
146 | $this->validate($request, ['email' => 'unique:mst_members,email', |
||
147 | 'contact' => 'unique:mst_members,contact', |
||
148 | 'member_code' => 'unique:mst_members,member_code', ]); |
||
149 | |||
150 | // Start Transaction |
||
151 | DB::beginTransaction(); |
||
152 | |||
153 | try { |
||
154 | // Store member's personal details |
||
155 | $memberData = ['name'=>$request->name, |
||
156 | 'DOB'=> $request->DOB, |
||
157 | 'gender'=> $request->gender, |
||
158 | 'contact'=> $request->contact, |
||
159 | 'emergency_contact'=> $request->emergency_contact, |
||
160 | 'health_issues'=> $request->health_issues, |
||
161 | 'email'=> $request->email, |
||
162 | 'address'=> $request->address, |
||
163 | 'member_id'=> $request->member_id, |
||
164 | 'proof_name'=> $request->proof_name, |
||
165 | 'member_code'=> $request->member_code, |
||
166 | 'status'=> $request->status, |
||
167 | 'pin_code'=> $request->pin_code, |
||
168 | 'occupation'=> $request->occupation, |
||
169 | 'aim'=> $request->aim, |
||
170 | 'source'=> $request->source, ]; |
||
171 | |||
172 | $member = new Member($memberData); |
||
173 | $member->createdBy()->associate(Auth::user()); |
||
174 | $member->updatedBy()->associate(Auth::user()); |
||
175 | $member->save(); |
||
176 | |||
177 | // Adding media i.e. Profile & proof photo |
||
178 | if ($request->hasFile('photo')) { |
||
179 | $member->addMedia($request->file('photo'))->usingFileName('profile_'.$member->id.$request->photo->getClientOriginalExtension())->toCollection('profile'); |
||
180 | } |
||
181 | |||
182 | if ($request->hasFile('proof_photo')) { |
||
183 | $member->addMedia($request->file('proof_photo'))->usingFileName('proof_'.$member->id.$request->proof_photo->getClientOriginalExtension())->toCollection('proof'); |
||
184 | } |
||
185 | |||
186 | // Helper function for calculating payment status |
||
187 | $invoice_total = $request->admission_amount + $request->subscription_amount + $request->taxes_amount - $request->discount_amount; |
||
188 | $paymentStatus = \constPaymentStatus::Unpaid; |
||
189 | $pending = $invoice_total - $request->payment_amount; |
||
190 | |||
191 | if ($request->mode == 1) { |
||
192 | if ($request->payment_amount == $invoice_total) { |
||
193 | $paymentStatus = \constPaymentStatus::Paid; |
||
194 | } elseif ($request->payment_amount > 0 && $request->payment_amount < $invoice_total) { |
||
195 | $paymentStatus = \constPaymentStatus::Partial; |
||
196 | } elseif ($request->payment_amount == 0) { |
||
197 | $paymentStatus = \constPaymentStatus::Unpaid; |
||
198 | } else { |
||
199 | $paymentStatus = \constPaymentStatus::Overpaid; |
||
200 | } |
||
201 | } |
||
202 | |||
203 | // Storing Invoice |
||
204 | $invoiceData = ['invoice_number'=> $request->invoice_number, |
||
205 | 'member_id'=> $member->id, |
||
206 | 'total'=> $invoice_total, |
||
207 | 'status'=> $paymentStatus, |
||
208 | 'pending_amount'=> $pending, |
||
209 | 'discount_amount'=> $request->discount_amount, |
||
210 | 'discount_percent'=> $request->discount_percent, |
||
211 | 'discount_note'=> $request->discount_note, |
||
212 | 'tax'=> $request->taxes_amount, |
||
213 | 'additional_fees'=> $request->additional_fees, |
||
214 | 'note'=>' ', ]; |
||
215 | |||
216 | $invoice = new Invoice($invoiceData); |
||
217 | $invoice->createdBy()->associate(Auth::user()); |
||
218 | $invoice->updatedBy()->associate(Auth::user()); |
||
219 | $invoice->save(); |
||
220 | |||
221 | // Storing subscription |
||
222 | foreach ($request->plan as $plan) { |
||
223 | $subscriptionData = ['member_id'=> $member->id, |
||
224 | 'invoice_id'=> $invoice->id, |
||
225 | 'plan_id'=> $plan['id'], |
||
226 | 'start_date'=> $plan['start_date'], |
||
227 | 'end_date'=> $plan['end_date'], |
||
228 | 'status'=> \constSubscription::onGoing, |
||
229 | 'is_renewal'=>'0', ]; |
||
230 | |||
231 | $subscription = new Subscription($subscriptionData); |
||
232 | $subscription->createdBy()->associate(Auth::user()); |
||
233 | $subscription->updatedBy()->associate(Auth::user()); |
||
234 | $subscription->save(); |
||
235 | |||
236 | //Adding subscription to invoice(Invoice Details) |
||
237 | $detailsData = ['invoice_id'=> $invoice->id, |
||
238 | 'plan_id'=> $plan['id'], |
||
239 | 'item_amount'=> $plan['price'], ]; |
||
240 | |||
241 | $invoice_details = new Invoice_detail($detailsData); |
||
242 | $invoice_details->createdBy()->associate(Auth::user()); |
||
243 | $invoice_details->updatedBy()->associate(Auth::user()); |
||
244 | $invoice_details->save(); |
||
245 | } |
||
246 | |||
247 | // Store Payment Details |
||
248 | $paymentData = ['invoice_id'=> $invoice->id, |
||
249 | 'payment_amount'=> $request->payment_amount, |
||
250 | 'mode'=> $request->mode, |
||
251 | 'note'=> ' ', ]; |
||
252 | |||
253 | $payment_details = new Payment_detail($paymentData); |
||
254 | $payment_details->createdBy()->associate(Auth::user()); |
||
255 | $payment_details->updatedBy()->associate(Auth::user()); |
||
256 | $payment_details->save(); |
||
257 | |||
258 | if ($request->mode == 0) { |
||
259 | // Store Cheque Details |
||
260 | $chequeData = ['payment_id'=> $payment_details->id, |
||
261 | 'number'=> $request->number, |
||
262 | 'date'=> $request->date, |
||
263 | 'status'=> \constChequeStatus::Recieved, ]; |
||
264 | |||
265 | $cheque_details = new Cheque_detail($chequeData); |
||
266 | $cheque_details->createdBy()->associate(Auth::user()); |
||
267 | $cheque_details->updatedBy()->associate(Auth::user()); |
||
268 | $cheque_details->save(); |
||
269 | } |
||
270 | |||
271 | // On member transfer update enquiry Status |
||
272 | if ($request->has('transfer_id')) { |
||
273 | $enquiry = Enquiry::findOrFail($request->transfer_id); |
||
274 | $enquiry->status = \constEnquiryStatus::Member; |
||
275 | $enquiry->updatedBy()->associate(Auth::user()); |
||
276 | $enquiry->save(); |
||
277 | } |
||
278 | |||
279 | //Updating Numbering Counters |
||
280 | Setting::where('key', '=', 'invoice_last_number')->update(['value' => $request->invoiceCounter]); |
||
281 | Setting::where('key', '=', 'member_last_number')->update(['value' => $request->memberCounter]); |
||
282 | $sender_id = \Utilities::getSetting('sms_sender_id'); |
||
283 | $gym_name = \Utilities::getSetting('gym_name'); |
||
284 | |||
285 | //SMS Trigger |
||
286 | if ($invoice->status == \constPaymentStatus::Paid) { |
||
287 | $sms_trigger = Sms_trigger::where('alias', '=', 'member_admission_with_paid_invoice')->first(); |
||
288 | $message = $sms_trigger->message; |
||
289 | $sms_text = sprintf($message, $member->name, $gym_name, $payment_details->payment_amount, $invoice->invoice_number); |
||
290 | $sms_status = $sms_trigger->status; |
||
291 | |||
292 | \Utilities::Sms($sender_id, $member->contact, $sms_text, $sms_status); |
||
293 | } elseif ($invoice->status == \constPaymentStatus::Partial) { |
||
294 | $sms_trigger = Sms_trigger::where('alias', '=', 'member_admission_with_partial_invoice')->first(); |
||
295 | $message = $sms_trigger->message; |
||
296 | $sms_text = sprintf($message, $member->name, $gym_name, $payment_details->payment_amount, $invoice->invoice_number, $invoice->pending_amount); |
||
297 | $sms_status = $sms_trigger->status; |
||
298 | |||
299 | \Utilities::Sms($sender_id, $member->contact, $sms_text, $sms_status); |
||
300 | } elseif ($invoice->status == \constPaymentStatus::Unpaid) { |
||
301 | if ($request->mode == 0) { |
||
302 | $sms_trigger = Sms_trigger::where('alias', '=', 'payment_with_cheque')->first(); |
||
303 | $message = $sms_trigger->message; |
||
304 | $sms_text = sprintf($message, $member->name, $payment_details->payment_amount, $cheque_details->number, $invoice->invoice_number, $gym_name); |
||
305 | $sms_status = $sms_trigger->status; |
||
306 | |||
307 | \Utilities::Sms($sender_id, $member->contact, $sms_text, $sms_status); |
||
308 | } else { |
||
309 | $sms_trigger = Sms_trigger::where('alias', '=', 'member_admission_with_unpaid_invoice')->first(); |
||
310 | $message = $sms_trigger->message; |
||
311 | $sms_text = sprintf($message, $member->name, $gym_name, $invoice->pending_amount, $invoice->invoice_number); |
||
312 | $sms_status = $sms_trigger->status; |
||
313 | |||
314 | \Utilities::Sms($sender_id, $member->contact, $sms_text, $sms_status); |
||
315 | } |
||
316 | } |
||
317 | |||
318 | if ($subscription->start_date < $member->created_at) { |
||
319 | $member->created_at = $subscription->start_date; |
||
320 | $member->updated_at = $subscription->start_date; |
||
321 | $member->save(); |
||
322 | |||
323 | $invoice->created_at = $subscription->start_date; |
||
324 | $invoice->updated_at = $subscription->start_date; |
||
325 | $invoice->save(); |
||
326 | |||
327 | foreach ($invoice->invoice_details as $invoice_detail) { |
||
328 | $invoice_detail->created_at = $subscription->start_date; |
||
329 | $invoice_detail->updated_at = $subscription->start_date; |
||
330 | $invoice_detail->save(); |
||
331 | } |
||
332 | |||
333 | $payment_details->created_at = $subscription->start_date; |
||
334 | $payment_details->updated_at = $subscription->start_date; |
||
335 | $payment_details->save(); |
||
336 | |||
337 | $subscription->created_at = $subscription->start_date; |
||
338 | $subscription->updated_at = $subscription->start_date; |
||
339 | $subscription->save(); |
||
340 | } |
||
341 | |||
342 | DB::commit(); |
||
343 | flash()->success('Member was successfully created'); |
||
344 | |||
345 | return redirect(action('MembersController@show', ['id' => $member->id])); |
||
346 | } catch (\Exception $e) { |
||
347 | DB::rollback(); |
||
348 | flash()->error('Error while creating the member'); |
||
349 | |||
350 | return redirect(action('MembersController@index')); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | //End of new Member |
||
355 | |||
356 | // End of store method |
||
357 | |||
358 | /** |
||
359 | * Edit a created resource in storage. |
||
360 | * |
||
361 | * @return Response |
||
362 | */ |
||
363 | public function edit($id) |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * Update an edited resource in storage. |
||
374 | * |
||
375 | * @return Response |
||
376 | */ |
||
377 | public function update($id, Request $request) |
||
378 | { |
||
379 | //dd($request->all()); |
||
380 | $member = Member::findOrFail($id); |
||
381 | $member->update($request->all()); |
||
382 | |||
383 | if ($request->hasFile('photo')) { |
||
384 | $member->clearMediaCollection('profile'); |
||
385 | $member->addMedia($request->file('photo'))->usingFileName('profile_'.$member->id.$request->photo->getClientOriginalExtension())->toCollection('profile'); |
||
386 | } |
||
387 | |||
388 | if ($request->hasFile('proof_photo')) { |
||
389 | $member->clearMediaCollection('proof'); |
||
390 | $member->addMedia($request->file('proof_photo'))->usingFileName('proof_'.$member->id.$request->proof_photo->getClientOriginalExtension())->toCollection('proof'); |
||
391 | } |
||
392 | |||
393 | $member->updatedBy()->associate(Auth::user()); |
||
394 | $member->save(); |
||
395 | |||
396 | flash()->success('Member details were successfully updated'); |
||
397 | |||
398 | return redirect(action('MembersController@show', ['id' => $member->id])); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Archive a resource in storage. |
||
403 | * |
||
404 | * @return Response |
||
405 | */ |
||
406 | public function archive($id, Request $request) |
||
431 | } |
||
432 | |||
433 | public function transfer($id, Request $request) |
||
469 | } |
||
470 | } |
||
471 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths