| Conditions | 10 |
| Paths | 216 |
| Total Lines | 69 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 59 | public function purchase() |
||
| 60 | { |
||
| 61 | $details = $this->invoice->getDetails(); |
||
| 62 | |||
| 63 | // convert to toman |
||
| 64 | $toman = $this->invoice->getAmount() * 10; |
||
| 65 | |||
| 66 | $orderId = crc32($this->invoice->getUuid()).time(); |
||
| 67 | if (!empty($details['orderId'])) { |
||
| 68 | $orderId = $details['orderId']; |
||
| 69 | } elseif (!empty($details['order_id'])) { |
||
| 70 | $orderId = $details['order_id']; |
||
| 71 | } |
||
| 72 | |||
| 73 | $mobile = null; |
||
| 74 | if (!empty($details['mobile'])) { |
||
| 75 | $mobile = $details['mobile']; |
||
| 76 | } elseif (!empty($details['phone'])) { |
||
| 77 | $mobile = $details['phone']; |
||
| 78 | } |
||
| 79 | |||
| 80 | $description = null; |
||
| 81 | if (!empty($details['description'])) { |
||
| 82 | $description = $details['description']; |
||
| 83 | } else { |
||
| 84 | $description = $this->settings->description; |
||
| 85 | } |
||
| 86 | |||
| 87 | $data = array( |
||
| 88 | "merchant"=> $this->settings->merchantId, //required |
||
| 89 | "callbackUrl"=> $this->settings->callbackUrl, //required |
||
| 90 | "amount"=> $toman, //required |
||
| 91 | "orderId"=> $orderId, //optional |
||
| 92 | 'mobile' => $mobile, //optional for mpg |
||
| 93 | "description" => $description, //optional |
||
| 94 | ); |
||
| 95 | |||
| 96 | //checking if optional allowedCards parameter exists |
||
| 97 | $allowedCards = null; |
||
| 98 | if (!empty($details['allowedCards'])) { |
||
| 99 | $allowedCards = $details['allowedCards']; |
||
| 100 | } else if(!empty($this->settings->allowedCards)) { |
||
| 101 | $allowedCards = $this->settings->allowedCards; |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($allowedCards != null) { |
||
| 105 | $allowedCards = array( |
||
| 106 | 'allowedCards' => $allowedCards, |
||
| 107 | ); |
||
| 108 | $data = array_merge($data, $allowedCards); |
||
| 109 | } |
||
| 110 | |||
| 111 | $response = $this->client->request( |
||
| 112 | 'POST', |
||
| 113 | $this->settings->apiPurchaseUrl, |
||
| 114 | ["json" => $data, "http_errors" => false] |
||
| 115 | ); |
||
| 116 | |||
| 117 | $body = json_decode($response->getBody()->getContents(), false); |
||
| 118 | |||
| 119 | if ($body->result != 100) { |
||
| 120 | // some error has happened |
||
| 121 | throw new PurchaseFailedException($body->message); |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->invoice->transactionId($body->trackId); |
||
| 125 | |||
| 126 | // return the transaction's id |
||
| 127 | return $this->invoice->getTransactionId(); |
||
| 128 | } |
||
| 219 |