| Conditions | 6 |
| Paths | 18 |
| Total Lines | 51 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 57 | public function purchase() |
||
| 58 | { |
||
| 59 | $details = $this->invoice->getDetails(); |
||
| 60 | |||
| 61 | // convert to toman |
||
| 62 | $toman = $this->invoice->getAmount() * 10; |
||
| 63 | |||
| 64 | if (!empty($details['orderId'])) { |
||
| 65 | $orderId = $details['orderId']; |
||
| 66 | } else if(!empty($details['order_id'])) { |
||
| 67 | $orderId = $details['order_id']; |
||
| 68 | } else { |
||
| 69 | $orderId = crc32($this->invoice->getUuid()).time(); |
||
| 70 | } |
||
| 71 | |||
| 72 | $mobile = null; |
||
| 73 | if(!empty($details['mobile'])) { |
||
| 74 | $mobile = $details['mobile']; |
||
| 75 | } else if(!empty($details['phone'])) { |
||
| 76 | $mobile = $details['phone']; |
||
| 77 | } |
||
| 78 | |||
| 79 | $data = array( |
||
| 80 | "merchant"=> $this->settings->merchantId, //required |
||
| 81 | "callbackUrl"=> $this->settings->callbackUrl, //required |
||
| 82 | "amount"=> $toman, //required |
||
| 83 | "orderId"=> $orderId, //optional |
||
| 84 | 'mobile' => $mobile, //optional for mpg |
||
| 85 | ); |
||
| 86 | |||
| 87 | $json = json_encode($data, JSON_UNESCAPED_UNICODE); |
||
| 88 | |||
| 89 | $response = $this->client->request( |
||
| 90 | 'POST', |
||
| 91 | $this->settings->apiPurchaseUrl, |
||
| 92 | [ |
||
| 93 | "form_params" => $json, |
||
| 94 | "http_errors" => false, |
||
| 95 | ] |
||
| 96 | ); |
||
| 97 | $body = json_decode($response->getBody()->getContents(), true); |
||
|
|
|||
| 98 | |||
| 99 | if ($response->result != 100) { |
||
| 100 | // some error has happened |
||
| 101 | throw new PurchaseFailedException($response->message); |
||
| 102 | } else { |
||
| 103 | $this->invoice->transactionId($response->trackId); |
||
| 104 | } |
||
| 105 | |||
| 106 | // return the transaction's id |
||
| 107 | return $this->invoice->getTransactionId(); |
||
| 108 | } |
||
| 197 |