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