| Conditions | 8 |
| Paths | 54 |
| Total Lines | 65 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 58 | public function purchase() |
||
| 59 | { |
||
| 60 | $details = $this->invoice->getDetails(); |
||
| 61 | |||
| 62 | $phone = null; |
||
| 63 | if (!empty($details['phone'])) { |
||
| 64 | $phone = $details['phone']; |
||
| 65 | } else if (!empty($details['mobile'])) { |
||
| 66 | $phone = $details['mobile']; |
||
| 67 | } |
||
| 68 | |||
| 69 | $mail = null; |
||
| 70 | if (!empty($details['mail'])) { |
||
| 71 | $mail = $details['mail']; |
||
| 72 | } else if (!empty($details['email'])) { |
||
| 73 | $mail = $details['email']; |
||
| 74 | } |
||
| 75 | |||
| 76 | $desc = null; |
||
| 77 | if (!empty($details['desc'])) { |
||
| 78 | $desc = $details['desc']; |
||
| 79 | } else if (!empty($details['description'])) { |
||
| 80 | $desc = $details['description']; |
||
| 81 | } else { |
||
| 82 | $desc = $this->settings->description; |
||
| 83 | } |
||
| 84 | |||
| 85 | $data = array( |
||
| 86 | 'order_id' => $this->invoice->getUuid(), |
||
| 87 | 'amount' => $this->invoice->getAmount(), |
||
| 88 | 'name' => $details['name'] ?? null, |
||
| 89 | 'phone' => $phone, |
||
| 90 | 'mail' => $mail, |
||
| 91 | 'desc' => $desc, |
||
| 92 | 'callback' => $this->settings->callbackUrl, |
||
| 93 | 'reseller' => $details['reseller'] ?? null, |
||
| 94 | ); |
||
| 95 | |||
| 96 | $response = $this |
||
| 97 | ->client |
||
| 98 | ->request( |
||
| 99 | 'POST', |
||
| 100 | $this->settings->apiPurchaseUrl, |
||
| 101 | [ |
||
| 102 | "json" => $data, |
||
| 103 | "headers" => [ |
||
| 104 | 'X-API-KEY' => $this->settings->merchantId, |
||
| 105 | 'Content-Type' => 'application/json', |
||
| 106 | 'X-SANDBOX' => (int) $this->settings->sandbox, |
||
| 107 | ], |
||
| 108 | "http_errors" => false, |
||
| 109 | ] |
||
| 110 | ); |
||
| 111 | |||
| 112 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 113 | if (empty($body['id'])) { |
||
| 114 | // error has happened |
||
| 115 | $message = $body['error_message'] ?? 'خطا در هنگام درخواست برای پرداخت رخ داده است.'; |
||
| 116 | throw new PurchaseFailedException($message); |
||
| 117 | } |
||
| 118 | |||
| 119 | $this->invoice->transactionId($body['id']); |
||
| 120 | |||
| 121 | // return the transaction's id |
||
| 122 | return $this->invoice->getTransactionId(); |
||
| 123 | } |
||
| 243 |