| Conditions | 6 |
| Paths | 12 |
| Total Lines | 53 |
| Code Lines | 22 |
| Lines | 3 |
| Ratio | 5.66 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 namespace BB\Services; |
||
| 71 | public function billMembers() |
||
| 72 | { |
||
| 73 | $subCharges = $this->subscriptionChargeRepository->getDue(); |
||
| 74 | |||
| 75 | //Check each of the due charges, if they have previous attempted payments ignore them |
||
| 76 | // we don't want to retry failed payments as for DD's this will generate bank charges |
||
| 77 | $subCharges->reject(function ($charge) { |
||
| 78 | return $this->paymentRepository->getPaymentsByReference($charge->id)->count() > 0; |
||
| 79 | }); |
||
| 80 | |||
| 81 | //Filter the list into two gocardless and balance subscriptions |
||
| 82 | $goCardlessUsers = $subCharges->filter(function ($charge) { |
||
| 83 | return $charge->user->payment_method == 'gocardless-variable'; |
||
| 84 | }); |
||
| 85 | |||
| 86 | $balanceUsers = $subCharges->filter(function ($charge) { |
||
| 87 | return $charge->user->payment_method == 'balance'; |
||
| 88 | }); |
||
| 89 | |||
| 90 | |||
| 91 | //Charge the balance users |
||
| 92 | foreach ($balanceUsers as $charge) { |
||
| 93 | if (($charge->user->monthly_subscription * 100) > $charge->user->cash_balance) { |
||
| 94 | //user doesn't have enough money |
||
| 95 | |||
| 96 | //If they have a secondary payment method of gocardless try that |
||
| 97 | if ($charge->user->secondary_payment_method == 'gocardless-variable') { |
||
| 98 | |||
| 99 | //Add the charge to the gocardless list for processing |
||
| 100 | $goCardlessUsers->push($charge); |
||
| 101 | |||
| 102 | event(new SubscriptionPayment\InsufficientFundsTryingDirectDebit($charge->user->id, $charge->id)); |
||
| 103 | } else { |
||
| 104 | event(new SubscriptionPayment\FailedInsufficientFunds($charge->user->id, $charge->id)); |
||
| 105 | } |
||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'balance', '', $charge->user->monthly_subscription, 'paid', 0, $charge->id); |
||
| 110 | |||
| 111 | event(new MemberBalanceChanged($charge->user->id)); |
||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | //Charge the gocardless users |
||
| 116 | foreach ($goCardlessUsers as $charge) { |
||
| 117 | $bill = $this->goCardless->newBill($charge->user->subscription_id, $charge->user->monthly_subscription, $this->goCardless->getNameFromReason('subscription')); |
||
| 118 | View Code Duplication | if ($bill) { |
|
| 119 | $this->paymentRepository->recordSubscriptionPayment($charge->user->id, 'gocardless-variable', $bill->id, $bill->amount, $bill->status, $bill->gocardless_fees, $charge->id); |
||
| 120 | } |
||
| 121 | }; |
||
| 122 | |||
| 123 | } |
||
| 124 | |||
| 139 | } |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.