| Conditions | 10 |
| Paths | 321 |
| Total Lines | 85 |
| Code Lines | 55 |
| Lines | 15 |
| Ratio | 17.65 % |
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 | if (empty($gatewaydata['token'])) { |
||
| 53 | $gatewaydata['card'] = $this->getCreditCard($data); |
||
| 54 | } |
||
| 55 | |||
| 56 | $this->extend('onBeforePurchase', $gatewaydata); |
||
| 57 | $request = $this->oGateway()->purchase($gatewaydata); |
||
| 58 | $this->extend('onAfterPurchase', $request); |
||
| 59 | |||
| 60 | $message = $this->createMessage('PurchaseRequest', $request); |
||
| 61 | $message->SuccessURL = $this->returnurl; |
||
| 62 | $message->FailureURL = $this->cancelurl; |
||
| 63 | $message->write(); |
||
| 64 | |||
| 65 | $gatewayresponse = $this->createGatewayResponse(); |
||
| 66 | try { |
||
| 67 | $response = $this->response = $request->send(); |
||
| 68 | $this->extend('onAfterSendPurchase', $request, $response); |
||
| 69 | $gatewayresponse->setOmnipayResponse($response); |
||
| 70 | //update payment model |
||
| 71 | if (GatewayInfo::is_manual($this->payment->Gateway)) { |
||
| 72 | //initiate manual payment |
||
| 73 | $this->createMessage('AuthorizedResponse', $response); |
||
| 74 | $this->payment->Status = 'Authorized'; |
||
| 75 | $this->payment->write(); |
||
| 76 | $gatewayresponse->setMessage("Manual payment authorised"); |
||
| 77 | View Code Duplication | } elseif ($response->isSuccessful()) { |
|
| 78 | //successful payment |
||
| 79 | $this->createMessage('PurchasedResponse', $response); |
||
| 80 | $this->payment->Status = 'Captured'; |
||
| 81 | $gatewayresponse->setMessage("Payment successful"); |
||
| 82 | $this->payment->write(); |
||
| 83 | $this->payment->extend('onCaptured', $gatewayresponse); |
||
| 84 | } elseif ($response->isRedirect()) { |
||
| 85 | // redirect to off-site payment gateway |
||
| 86 | $this->createMessage('PurchaseRedirectResponse', $response); |
||
| 87 | $this->payment->Status = 'Authorized'; |
||
| 88 | $this->payment->write(); |
||
| 89 | $gatewayresponse->setMessage("Redirecting to gateway"); |
||
| 90 | View Code Duplication | } else { |
|
| 91 | //handle error |
||
| 92 | $this->createMessage('PurchaseError', $response); |
||
| 93 | $gatewayresponse->setMessage( |
||
| 94 | "Error (".$response->getCode()."): ".$response->getMessage() |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | } catch (Omnipay\Common\Exception\OmnipayException $e) { |
||
| 98 | $this->createMessage('PurchaseError', $e); |
||
| 99 | $gatewayresponse->setMessage($e->getMessage()); |
||
| 100 | } |
||
| 101 | $gatewayresponse->setRedirectURL($this->getRedirectURL()); |
||
| 102 | |||
| 103 | return $gatewayresponse; |
||
| 104 | } |
||
| 105 | |||
| 169 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.