| Conditions | 6 |
| Paths | 16 |
| Total Lines | 66 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 73 | public function purchase(): string |
||
| 74 | { |
||
| 75 | $phone = $this->invoice->getDetail('phone') ?? $this->invoice->getDetail('mobile'); |
||
| 76 | $phoneNumber = preg_replace('/\D/', '', $phone); |
||
| 77 | |||
| 78 | // Check if the number starts with '0' and is 11 digits long |
||
| 79 | if (strlen($phoneNumber) === 11 && $phoneNumber[0] === '0') { |
||
| 80 | // Replace the leading '0' with '+98' |
||
| 81 | $phoneNumber = '+98' . substr($phoneNumber, 1); |
||
| 82 | } |
||
| 83 | $data = [ |
||
| 84 | "amount" => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), |
||
| 85 | "cartList" => [ |
||
| 86 | [ |
||
| 87 | "cartId" => 1, |
||
| 88 | "cartItems" => [ |
||
| 89 | [ |
||
| 90 | "amount" => 0, |
||
| 91 | "category" => "string", |
||
| 92 | "count" => 1, |
||
| 93 | "id" => 0, |
||
| 94 | "name" => "string", |
||
| 95 | "commissionType" => 0 |
||
| 96 | ] |
||
| 97 | ], |
||
| 98 | "isShipmentIncluded" => false, |
||
| 99 | "isTaxIncluded" => false, |
||
| 100 | "shippingAmount" => 0, |
||
| 101 | "taxAmount" => 0, |
||
| 102 | "totalAmount" => $this->invoice->getAmount() * ($this->settings->currency == 'T' ? 10 : 1), |
||
| 103 | ] |
||
| 104 | ], |
||
| 105 | |||
| 106 | |||
| 107 | "discountAmount" => 0, |
||
| 108 | "externalSourceAmount" => 0, |
||
| 109 | "mobile" => $phoneNumber, |
||
| 110 | "paymentMethodTypeDto" => "INSTALLMENT", |
||
| 111 | "returnURL" => $this->settings->callbackUrl, |
||
| 112 | "transactionId" => $this->invoice->getUuid() |
||
| 113 | ]; |
||
| 114 | |||
| 115 | $response = $this->client->request( |
||
| 116 | 'POST', |
||
| 117 | $this->settings->apiPurchaseUrl, |
||
| 118 | [ |
||
| 119 | RequestOptions::BODY => json_encode($data), |
||
| 120 | RequestOptions::HEADERS => [ |
||
| 121 | 'Content-Type' => 'application/json', |
||
| 122 | 'Authorization' => 'Bearer ' . $this->oauthToken, |
||
| 123 | ], |
||
| 124 | RequestOptions::HTTP_ERRORS => false, |
||
| 125 | ] |
||
| 126 | ); |
||
| 127 | |||
| 128 | $body = json_decode($response->getBody()->getContents(), true); |
||
| 129 | if ($response->getStatusCode() != 200) { |
||
| 130 | // error has happened |
||
| 131 | $message = $body['result']['message'] ?? 'خطا در هنگام درخواست برای پرداخت رخ داده است.'; |
||
| 132 | throw new PurchaseFailedException($message); |
||
| 133 | } |
||
| 134 | $this->invoice->transactionId($body['response']['paymentToken']); |
||
| 135 | $this->setPaymentUrl($body['response']['paymentPageUrl']); |
||
| 136 | |||
| 137 | // return the transaction's id |
||
| 138 | return $this->invoice->getTransactionId(); |
||
| 139 | } |
||
| 246 |