Conditions | 12 |
Paths | 961 |
Total Lines | 91 |
Code Lines | 59 |
Lines | 15 |
Ratio | 16.48 % |
Changes | 1 | ||
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 |
||
20 | public function purchase($data = array()) { |
||
|
|||
21 | if ($this->payment->Status !== "Created") { |
||
22 | return null; //could be handled better? send payment response? |
||
23 | } |
||
24 | if (!$this->payment->isInDB()) { |
||
25 | $this->payment->write(); |
||
26 | } |
||
27 | //update success/fail urls |
||
28 | $this->update($data); |
||
29 | |||
30 | //set the client IP address, if not already set |
||
31 | if(!isset($data['clientIp'])){ |
||
32 | $data['clientIp'] = Controller::curr()->getRequest()->getIP(); |
||
33 | } |
||
34 | |||
35 | $gatewaydata = array_merge($data, array( |
||
36 | 'amount' => (float) $this->payment->MoneyAmount, |
||
37 | 'currency' => $this->payment->MoneyCurrency, |
||
38 | //set all gateway return/cancel/notify urls to PaymentGatewayController endpoint |
||
39 | 'returnUrl' => $this->getEndpointURL("complete", $this->payment->Identifier), |
||
40 | 'cancelUrl' => $this->getEndpointURL("cancel", $this->payment->Identifier), |
||
41 | 'notifyUrl' => $this->getEndpointURL("notify", $this->payment->Identifier) |
||
42 | )); |
||
43 | |||
44 | // Often, the shop will want to pass in a transaction ID (order #, etc), but if there's |
||
45 | // not one we need to set it as Ominpay requires this. |
||
46 | if(!isset($gatewaydata['transactionId'])){ |
||
47 | $gatewaydata['transactionId'] = $this->payment->Identifier; |
||
48 | } |
||
49 | |||
50 | // We only look for a card if we aren't already provided with a token |
||
51 | // Increasingly we can expect tokens or nonce's to be more common (e.g. Stripe and Braintree) |
||
52 | $tokenKey = Payment::config()->token_key ?: 'token'; |
||
53 | if (empty($gatewaydata[$tokenKey])) { |
||
54 | $gatewaydata['card'] = $this->getCreditCard($data); |
||
55 | } elseif ($tokenKey !== 'token') { |
||
56 | // some gateways (eg. braintree) use a different key but we need |
||
57 | // to normalize that for omnipay |
||
58 | $gatewaydata['token'] = $gatewaydata[$tokenKey]; |
||
59 | unset($gatewaydata[$tokenKey]); |
||
60 | } |
||
61 | |||
62 | $this->extend('onBeforePurchase', $gatewaydata); |
||
63 | $request = $this->oGateway()->purchase($gatewaydata); |
||
64 | $this->extend('onAfterPurchase', $request); |
||
65 | |||
66 | $message = $this->createMessage('PurchaseRequest', $request); |
||
67 | $message->SuccessURL = $this->returnurl; |
||
68 | $message->FailureURL = $this->cancelurl; |
||
69 | $message->write(); |
||
70 | |||
71 | $gatewayresponse = $this->createGatewayResponse(); |
||
72 | try { |
||
73 | $response = $this->response = $request->send(); |
||
74 | $this->extend('onAfterSendPurchase', $request, $response); |
||
75 | $gatewayresponse->setOmnipayResponse($response); |
||
76 | //update payment model |
||
77 | if (GatewayInfo::isManual($this->payment->Gateway)) { |
||
78 | //initiate manual payment |
||
79 | $this->createMessage('AuthorizedResponse', $response); |
||
80 | $this->payment->Status = 'Authorized'; |
||
81 | $this->payment->write(); |
||
82 | $gatewayresponse->setMessage("Manual payment authorised"); |
||
83 | View Code Duplication | } elseif ($response->isSuccessful()) { |
|
84 | //successful payment |
||
85 | $this->createMessage('PurchasedResponse', $response); |
||
86 | $this->payment->Status = 'Captured'; |
||
87 | $gatewayresponse->setMessage("Payment successful"); |
||
88 | $this->payment->write(); |
||
89 | $this->payment->extend('onCaptured', $gatewayresponse); |
||
90 | } elseif ($response->isRedirect()) { |
||
91 | // redirect to off-site payment gateway |
||
92 | $this->createMessage('PurchaseRedirectResponse', $response); |
||
93 | $this->payment->Status = 'Authorized'; |
||
94 | $this->payment->write(); |
||
95 | $gatewayresponse->setMessage("Redirecting to gateway"); |
||
96 | View Code Duplication | } else { |
|
97 | //handle error |
||
98 | $this->createMessage('PurchaseError', $response); |
||
99 | $gatewayresponse->setMessage( |
||
100 | "Error (".$response->getCode()."): ".$response->getMessage() |
||
101 | ); |
||
102 | } |
||
103 | } catch (Omnipay\Common\Exception\OmnipayException $e) { |
||
104 | $this->createMessage('PurchaseError', $e); |
||
105 | $gatewayresponse->setMessage($e->getMessage()); |
||
106 | } |
||
107 | $gatewayresponse->setRedirectURL($this->getRedirectURL()); |
||
108 | |||
109 | return $gatewayresponse; |
||
110 | } |
||
111 | |||
175 |
A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.
You can also find more information in the “Code” section of your repository.