| Conditions | 9 |
| Paths | 153 |
| Total Lines | 75 |
| Code Lines | 51 |
| Lines | 15 |
| Ratio | 20 % |
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 |
||
| 18 | public function purchase($data = array()) { |
||
| 19 | if ($this->payment->Status !== "Created") { |
||
|
|
|||
| 20 | return null; //could be handled better? send payment response? |
||
| 21 | } |
||
| 22 | if (!$this->payment->isInDB()) { |
||
| 23 | $this->payment->write(); |
||
| 24 | } |
||
| 25 | //update success/fail urls |
||
| 26 | $this->update($data); |
||
| 27 | |||
| 28 | //set the client IP address, if not already set |
||
| 29 | if(!isset($data['clientIp'])){ |
||
| 30 | $data['clientIp'] = Controller::curr()->getRequest()->getIP(); |
||
| 31 | } |
||
| 32 | |||
| 33 | $gatewaydata = array_merge($data,array( |
||
| 34 | 'card' => $this->getCreditCard($data), |
||
| 35 | 'amount' => (float) $this->payment->MoneyAmount, |
||
| 36 | 'currency' => $this->payment->MoneyCurrency, |
||
| 37 | //set all gateway return/cancel/notify urls to PaymentGatewayController endpoint |
||
| 38 | 'returnUrl' => $this->getEndpointURL("complete", $this->payment->Identifier), |
||
| 39 | 'cancelUrl' => $this->getEndpointURL("cancel", $this->payment->Identifier), |
||
| 40 | 'notifyUrl' => $this->getEndpointURL("notify", $this->payment->Identifier) |
||
| 41 | )); |
||
| 42 | |||
| 43 | if(!isset($gatewaydata['transactionId'])){ |
||
| 44 | $gatewaydata['transactionId'] = $this->payment->Identifier; |
||
| 45 | } |
||
| 46 | |||
| 47 | $request = $this->oGateway()->purchase($gatewaydata); |
||
| 48 | |||
| 49 | $message = $this->createMessage('PurchaseRequest', $request); |
||
| 50 | $message->SuccessURL = $this->returnurl; |
||
| 51 | $message->FailureURL = $this->cancelurl; |
||
| 52 | $message->write(); |
||
| 53 | |||
| 54 | $gatewayresponse = $this->createGatewayResponse(); |
||
| 55 | try { |
||
| 56 | $response = $this->response = $request->send(); |
||
| 57 | $gatewayresponse->setOmnipayResponse($response); |
||
| 58 | //update payment model |
||
| 59 | if (GatewayInfo::is_manual($this->payment->Gateway)) { |
||
| 60 | //initiate manual payment |
||
| 61 | $this->createMessage('AuthorizedResponse', $response); |
||
| 62 | $this->payment->Status = 'Authorized'; |
||
| 63 | $this->payment->write(); |
||
| 64 | $gatewayresponse->setMessage("Manual payment authorised"); |
||
| 65 | View Code Duplication | } elseif ($response->isSuccessful()) { |
|
| 66 | //successful payment |
||
| 67 | $this->createMessage('PurchasedResponse', $response); |
||
| 68 | $this->payment->Status = 'Captured'; |
||
| 69 | $gatewayresponse->setMessage("Payment successful"); |
||
| 70 | $this->payment->write(); |
||
| 71 | $this->payment->extend('onCaptured', $gatewayresponse); |
||
| 72 | } elseif ($response->isRedirect()) { |
||
| 73 | // redirect to off-site payment gateway |
||
| 74 | $this->createMessage('PurchaseRedirectResponse', $response); |
||
| 75 | $this->payment->Status = 'Authorized'; |
||
| 76 | $this->payment->write(); |
||
| 77 | $gatewayresponse->setMessage("Redirecting to gateway"); |
||
| 78 | View Code Duplication | } else { |
|
| 79 | //handle error |
||
| 80 | $this->createMessage('PurchaseError', $response); |
||
| 81 | $gatewayresponse->setMessage( |
||
| 82 | "Error (".$response->getCode()."): ".$response->getMessage() |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | } catch (Omnipay\Common\Exception\OmnipayException $e) { |
||
| 86 | $this->createMessage('PurchaseError', $e); |
||
| 87 | $gatewayresponse->setMessage($e->getMessage()); |
||
| 88 | } |
||
| 89 | $gatewayresponse->setRedirectURL($this->getRedirectURL()); |
||
| 90 | |||
| 91 | return $gatewayresponse; |
||
| 92 | } |
||
| 93 | |||
| 155 |
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.