Skip to content

Commit 6f84817

Browse files
committed
Refactor extract method
1 parent 4f656b6 commit 6f84817

File tree

1 file changed

+43
-98
lines changed

1 file changed

+43
-98
lines changed

src/Client.php

Lines changed: 43 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Copyright © 2017 SeQura Engineering. All rights reserved.
45
*/
@@ -43,25 +44,18 @@ public function isValidAuth()
4344
$this->initCurl($this->_endpoint . '/orders');
4445
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
4546
$this->sendRequest();
46-
return $this->status != 401;
47+
return $this->status != 401;
4748
}
4849

4950
public function startSolicitation($order)
5051
{
51-
if ( ! $this->qualifyForSolicitation($order) && ! $this->_debug) {
52+
if (!$this->qualifyForSolicitation($order) && !$this->_debug) {
5253
return;
5354
}
5455

5556
$this->initCurl($this->_endpoint . '/orders');
5657
$this->verbThePayload('POST', array('order' => $order));
57-
if ($this->status == 204) {
58-
$this->success = true;
59-
$this->log("Start " . $this->status . ": Ok!");
60-
} elseif ($this->status >= 200 && $this->status <= 299 || $this->status == 409) {
61-
$this->success = false;
62-
$this->json = json_decode($this->curl_result, true); // return array, not object
63-
$this->log("Start " . $this->status . ": " . $this->curl_result);
64-
}
58+
$this->dealWithResponse();
6559
curl_close($this->ch);
6660
}
6761

@@ -70,7 +64,7 @@ public function qualifyForSolicitation($order)
7064
if ($order['cart']['order_total_with_tax'] <= 0) {
7165
return false;
7266
}
73-
if ( ! Helper::isConsistentCart($order['cart'])) {
67+
if (!Helper::isConsistentCart($order['cart'])) {
7468
return false;
7569
}
7670

@@ -98,7 +92,10 @@ private function verbThePayload($verb, $payload)
9892
$data_string = json_encode(Helper::removeNulls($payload));
9993
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $verb);
10094
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data_string);
101-
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(
95+
curl_setopt(
96+
$this->ch,
97+
CURLOPT_HTTPHEADER,
98+
array(
10299
'Accept: application/json',
103100
'Content-Type: application/json',
104101
'Content-Length: ' . strlen($data_string)
@@ -109,19 +106,32 @@ private function verbThePayload($verb, $payload)
109106

110107
private function sendRequest()
111108
{
109+
$this->succeess = false;
112110
if ($this->_debug) {
113111
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
114112
}
115113
$this->curl_result = curl_exec($this->ch);
116114
$this->status = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
117115
}
118116

119-
function log($msg)
117+
private function dealWithResponse()
118+
{
119+
if (200 <= $this->status && $this->status <= 299) {
120+
$this->success = true;
121+
$this->log("Start " . $this->status . ": Ok!");
122+
} else {
123+
$this->success = false;
124+
$this->json = json_decode($this->curl_result, true);
125+
$this->log("Start " . $this->status . ": " . $this->curl_result);
126+
}
127+
}
128+
129+
private function log($msg)
120130
{
121-
if ( ! $this->_debug) {
131+
if (!$this->_debug) {
122132
return;
123133
}
124-
if ( ! $this->_logfile) {
134+
if (!$this->_logfile) {
125135
error_log($msg);
126136
} else {
127137
error_log($msg . "\n", 3, $this->_logfile);
@@ -136,15 +146,7 @@ public function getIdentificationForm($uri, $options = array())
136146
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
137147
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Accept: text/html'));
138148
$this->sendRequest();
139-
140-
if ($this->status >= 200 && $this->status <= 299) {
141-
curl_close($this->ch);
142-
$this->success = true;
143-
return $this->curl_result;
144-
} else {
145-
$this->success = false;
146-
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
147-
}
149+
$this->dealWithResponse();
148150
curl_close($this->ch);
149151
}
150152

@@ -155,15 +157,7 @@ public function sendIdentificationForm($uri, $options = array())
155157
$options["channel"] = array_key_exists('channel', $options) ? $options["channel"] : "sms";
156158
$this->initCurl($uri . '/form_deliveries');
157159
$this->verbThePayload('POST', $options);
158-
159-
if ($this->status >= 200 && $this->status <= 299) {
160-
curl_close($this->ch);
161-
$this->success = true;
162-
return $this->curl_result;
163-
} else {
164-
$this->success = false;
165-
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
166-
}
160+
$this->dealWithResponse();
167161
curl_close($this->ch);
168162
}
169163

@@ -175,14 +169,7 @@ public function startCards($order)
175169

176170
$this->initCurl($this->_endpoint . '/cards');
177171
$this->verbThePayload('POST', array('order' => $order));
178-
if ($this->status == 204) {
179-
$this->success = true;
180-
$this->log("Start " . $this->status . ": Ok!");
181-
} elseif ($this->status >= 200 && $this->status <= 299 || $this->status == 409) {
182-
$this->success = false;
183-
$this->json = json_decode($this->curl_result, true); // return array, not object
184-
$this->log("Start " . $this->status . ": " . $this->curl_result);
185-
}
172+
$this->dealWithResponse();
186173
curl_close($this->ch);
187174
}
188175

@@ -192,26 +179,18 @@ public function getCardsForm($uri, $options = array())
192179
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
193180
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Accept: text/html'));
194181
$this->sendRequest();
195-
$this->success = false;
196-
if ($this->status >= 200 && $this->status <= 299) {
197-
$this->success = true;
198-
$this->json = json_decode($this->curl_result, true); // return array, not object
199-
} elseif ($this->status >= 400 && $this->status <= 499) {
200-
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
201-
} else {
202-
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
203-
}
182+
$this->dealWithResponse();
204183
curl_close($this->ch);
205184
}
206185

207186
public function qualifyForstartCards($order)
208187
{
209188
return
210-
isset($order['customer']['ref']) && $order['customer']['ref']!='' &&
211-
isset($order['customer']['email']) && $order['customer']['email']!='' &&
189+
isset($order['customer']['ref']) && $order['customer']['ref'] != '' &&
190+
isset($order['customer']['email']) && $order['customer']['email'] != '' &&
212191
(
213-
(isset($order['delivery_address']['mobile_phone']) && $order['delivery_address']['mobile_phone']!='') ||
214-
(isset($order['delivery_address']['phone']) && $order['delivery_address']['phone']!='')
192+
(isset($order['delivery_address']['mobile_phone']) && $order['delivery_address']['mobile_phone'] != '') ||
193+
(isset($order['delivery_address']['phone']) && $order['delivery_address']['phone'] != '')
215194
);
216195
}
217196

@@ -222,19 +201,11 @@ public function getMerchantPaymentMethods($merchant)
222201

223202
public function getPaymentMethods($uri, $options = array())
224203
{
225-
$this->initCurl($uri . '/payment_methods' . (count($options)>0? '?' . http_build_query($options):''));
204+
$this->initCurl($uri . '/payment_methods' . (count($options) > 0 ? '?' . http_build_query($options) : ''));
226205
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
227206
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Accept: text/html'));
228207
$this->sendRequest();
229-
$this->success = false;
230-
if ($this->status >= 200 && $this->status <= 299) {
231-
$this->success = true;
232-
$this->json = json_decode($this->curl_result, true); // return array, not object
233-
} elseif ($this->status >= 400 && $this->status <= 499) {
234-
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
235-
} else {
236-
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
237-
}
208+
$this->dealWithResponse();
238209
curl_close($this->ch);
239210
}
240211

@@ -244,31 +215,20 @@ public function getCreditAgreements($amount, $merchant)
244215
$this->initCurl($uri);
245216
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'GET');
246217
$this->sendRequest();
247-
if ($this->status >= 200 && $this->status <= 299) {
248-
$this->success = true;
249-
curl_close($this->ch);
250-
return json_decode($this->curl_result, true);
251-
} else {
252-
$this->success = false;
253-
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
254-
}
218+
$this->dealWithResponse();
255219
curl_close($this->ch);
256220
}
257221

258222
public function updateOrder($uri, $order, $verb = 'PUT')
259223
{
260-
if ( ! preg_match('!^https?://!', $uri)) {
224+
if (!preg_match('!^https?://!', $uri)) {
261225
$uri = $this->_endpoint . '/orders/' . $uri;
262226
}
263227
$this->initCurl($uri);
264228
$this->verbThePayload($verb, array('order' => $order));
265-
266-
if ($this->status >= 200 && $this->status <= 299) {
267-
$this->success = true;
268-
} elseif ($this->status == 409) {
269-
$this->success = false;
229+
$this->dealWithResponse();
230+
if ($this->status == 409) {
270231
$this->cart_has_changed = true;
271-
$this->json = json_decode($this->curl_result, true);
272232
}
273233
curl_close($this->ch);
274234
}
@@ -277,33 +237,18 @@ public function sendDeliveryReport($delivery_report)
277237
{
278238
$this->initCurl($this->_endpoint . '/delivery_reports');
279239
$this->verbThePayload('POST', array('delivery_report' => $delivery_report));
280-
281-
if ($this->status >= 200 && $this->status <= 299) {
282-
$this->success = true;
283-
$this->log("Delivery " . $this->status . ": Ok!");
284-
} elseif ($this->status >= 200 && $this->status <= 299 || $this->status == 409) {
285-
$this->success = false;
286-
$this->json = json_decode($this->curl_result, true); // return array, not object
287-
$this->log("Delivery " . $this->status . ": " . print_r($this->json, true));
288-
}
240+
$this->dealWithResponse();
289241
curl_close($this->ch);
290242
}
291243

292244
public function orderUpdate($order)
293245
{
294246
$uri = $this->_endpoint .
295-
'/merchants/' . $order['merchant']['id'] .
296-
'/orders/' . $order['merchant_reference']['order_ref_1'];
247+
'/merchants/' . $order['merchant']['id'] .
248+
'/orders/' . $order['merchant_reference']['order_ref_1'];
297249
$this->initCurl($uri);
298250
$this->verbThePayload('PUT', array('order' => $order));
299-
300-
if ($this->status >= 200 && $this->status <= 299) {
301-
$this->success = true;
302-
} elseif ($this->status == 409) {
303-
$this->success = false;
304-
$this->cart_has_changed = true;
305-
$this->json = json_decode($this->curl_result, true);
306-
}
251+
$this->dealWithResponse();
307252
curl_close($this->ch);
308253
}
309254

0 commit comments

Comments
 (0)