Conditions | 14 |
Paths | 39 |
Total Lines | 63 |
Code Lines | 25 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
92 | public function updatePayment(string $paymentId) |
||
93 | { |
||
94 | try { |
||
95 | // Retrieve the payment's current state and its associated purchase |
||
96 | $payment = $this->apiClient->payments->get($paymentId); |
||
97 | $purchaseId = $payment->metadata->purchase_id; |
||
98 | $purchase = Purchase::find($purchaseId); |
||
99 | |||
100 | if( !$purchase ) { |
||
101 | throw new PaymentProviderException('Payment-Id "' . $paymentId . '" has no matching purchase!'); |
||
102 | } |
||
103 | |||
104 | if( $payment->isPaid() && !$payment->hasRefunds() && !$payment->hasChargebacks() ) { |
||
105 | /* |
||
106 | * The payment is paid and isn't refunded or charged back. |
||
107 | */ |
||
108 | // Only send an email with the tickets on the change of state to "paid" |
||
109 | if($purchase->state != 'paid') { |
||
110 | Log::info('[Purchase#' . $purchase->id . '] Sending Ticket-Mail.'); |
||
111 | Mail::to($purchase->customer)->send(new TicketsPaid($purchase)); |
||
112 | } |
||
113 | } elseif( $payment->isOpen() ) { |
||
114 | /* |
||
115 | * The payment is open. |
||
116 | */ |
||
117 | } elseif( $payment->isPending() ) { |
||
118 | /* |
||
119 | * The payment is pending. |
||
120 | */ |
||
121 | } elseif( $payment->isFailed() ) { |
||
122 | /* |
||
123 | * The payment has failed. |
||
124 | */ |
||
125 | $purchase->deleteWithAllData(); |
||
126 | } elseif( $payment->isExpired() ) { |
||
127 | /* |
||
128 | * The payment is expired. |
||
129 | */ |
||
130 | $purchase->deleteWithAllData(); |
||
131 | } elseif( $payment->isCanceled() ) { |
||
132 | /* |
||
133 | * The payment has been canceled. |
||
134 | */ |
||
135 | $purchase->deleteWithAllData(); |
||
136 | } elseif( $payment->hasRefunds() ) { |
||
137 | /* |
||
138 | * The payment has been (partially) refunded. |
||
139 | * The status of the payment is still "paid" |
||
140 | */ |
||
141 | } elseif( $payment->hasChargebacks() ) { |
||
142 | /* |
||
143 | * The payment has been (partially) charged back. |
||
144 | * The status of the payment is still "paid" |
||
145 | */ |
||
146 | } |
||
147 | |||
148 | // Update the purchase's state |
||
149 | $purchase->state = $payment->status; |
||
150 | $purchase->state_updated = new \DateTime(); |
||
151 | $purchase->save(); |
||
152 | |||
153 | } catch (\Mollie\Api\Exceptions\ApiException $e) { |
||
154 | throw new PaymentProviderException( $e ); |
||
155 | } |
||
159 |