Conditions | 7 |
Paths | 16 |
Total Lines | 52 |
Code Lines | 29 |
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 |
||
60 | public function purchase() |
||
61 | { |
||
62 | if (!empty($this->invoice->getDetails()['description'])) { |
||
63 | $description = $this->invoice->getDetails()['description']; |
||
64 | } else { |
||
65 | $description = $this->settings->description; |
||
66 | } |
||
67 | |||
68 | if (!empty($this->invoice->getDetails()['mobile'])) { |
||
69 | $mobile = $this->invoice->getDetails()['mobile']; |
||
70 | } |
||
71 | |||
72 | if (!empty($this->invoice->getDetails()['email'])) { |
||
73 | $email = $this->invoice->getDetails()['email']; |
||
74 | } |
||
75 | |||
76 | $metadata = ['email' => $email, $mobile => $mobile]; |
||
77 | |||
78 | $data = [ |
||
79 | "merchant_id" => $this->settings->merchantId, |
||
80 | "amount" => $this->invoice->getAmount(), |
||
81 | "callback_url" => $this->settings->callbackUrl, |
||
82 | "description" => $description, |
||
83 | "metadata" => array_merge($this->invoice->getDetails() ?? [], $metadata), |
||
84 | ]; |
||
85 | |||
86 | |||
87 | $response = $this |
||
88 | ->client |
||
89 | ->request( |
||
90 | 'POST', |
||
91 | $this->settings->apiPurchaseUrl, |
||
92 | [ |
||
93 | "json" => $data, |
||
94 | "headers" => [ |
||
95 | 'Content-Type' => 'application/json', |
||
96 | ], |
||
97 | "http_errors" => false, |
||
98 | ] |
||
99 | ); |
||
100 | |||
101 | $result = json_decode($response->getBody()->getContents(), true); |
||
102 | |||
103 | // some error has happened |
||
104 | if (! empty($result['errors']) || empty($result['data']) || $result['data']['code'] != 100) { |
||
105 | throw new PurchaseFailedException($result['errors']['message'], $result['errors']['code']); |
||
106 | } |
||
107 | |||
108 | $this->invoice->transactionId($result['data']["authority"]); |
||
109 | |||
110 | // return the transaction's id |
||
111 | return $this->invoice->getTransactionId(); |
||
112 | } |
||
212 |