Skip to content

Commit 33e2854

Browse files
committed
Refactoring
1 parent 7cd9c82 commit 33e2854

1 file changed

Lines changed: 58 additions & 37 deletions

File tree

src/MailChimp.php

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ public function new_batch($batch_id = null)
6161
return new Batch($this, $batch_id);
6262
}
6363

64+
/**
65+
* @return string The url to the API endpoint
66+
*/
67+
public function getApiEndpoint()
68+
{
69+
return $this->api_endpoint;
70+
}
71+
72+
6473
/**
6574
* Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL
6675
* @param string $email The subscriber's email address
@@ -185,22 +194,7 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se
185194

186195
$url = $this->api_endpoint . '/' . $method;
187196

188-
$this->last_error = '';
189-
$this->request_successful = false;
190-
$response = array(
191-
'headers' => null, // array of details from curl_getinfo()
192-
'httpHeaders' => null, // array of HTTP headers
193-
'body' => null // content of the response
194-
);
195-
$this->last_response = $response;
196-
197-
$this->last_request = array(
198-
'method' => $http_verb,
199-
'path' => $method,
200-
'url' => $url,
201-
'body' => '',
202-
'timeout' => $timeout,
203-
);
197+
$response = $this->prepareStateForRequest($http_verb, $method, $url, $timeout);
204198

205199
$ch = curl_init();
206200
curl_setopt($ch, CURLOPT_URL, $url);
@@ -245,37 +239,39 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se
245239
break;
246240
}
247241

248-
$responseContent = curl_exec($ch);
249-
242+
$responseContent = curl_exec($ch);
250243
$response['headers'] = curl_getinfo($ch);
251-
if ($responseContent === false) {
252-
$this->last_error = curl_error($ch);
253-
} else {
254-
$headerSize = $response['headers']['header_size'];
255-
256-
$response['httpHeaders'] = $this->getHeadersAsArray(substr($responseContent, 0, $headerSize));
257-
$response['body'] = substr($responseContent, $headerSize);
258-
259-
if (isset($response['headers']['request_header'])) {
260-
$this->last_request['headers'] = $response['headers']['request_header'];
261-
}
262-
}
263-
264244
curl_close($ch);
265245

266-
$formattedResponse = $this->formatResponse($response);
246+
$response = $this->setResponseState($response, $responseContent);
247+
$formattedResponse = $this->formatResponse($response);
267248

268249
$this->determineSuccess($response, $formattedResponse, $timeout);
269250

270251
return $formattedResponse;
271252
}
272253

273-
/**
274-
* @return string The url to the API endpoint
275-
*/
276-
public function getApiEndpoint()
254+
private function prepareStateForRequest($http_verb, $method, $url, $timeout)
277255
{
278-
return $this->api_endpoint;
256+
$this->last_error = '';
257+
258+
$this->request_successful = false;
259+
260+
$this->last_response = array(
261+
'headers' => null, // array of details from curl_getinfo()
262+
'httpHeaders' => null, // array of HTTP headers
263+
'body' => null // content of the response
264+
);
265+
266+
$this->last_request = array(
267+
'method' => $http_verb,
268+
'path' => $method,
269+
'url' => $url,
270+
'body' => '',
271+
'timeout' => $timeout,
272+
);
273+
274+
return $this->last_response;
279275
}
280276

281277
/**
@@ -369,6 +365,31 @@ private function formatResponse($response)
369365
return false;
370366
}
371367

368+
/**
369+
* Do post-request formatting and setting state from the response
370+
* @param array $response The response from the curl request
371+
* @param string $responseContent The body of the response from the curl request
372+
* * @return array The modified response
373+
*/
374+
private function setResponseState($response, $responseContent)
375+
{
376+
if ($responseContent === false) {
377+
$this->last_error = curl_error($ch);
378+
} else {
379+
380+
$headerSize = $response['headers']['header_size'];
381+
382+
$response['httpHeaders'] = $this->getHeadersAsArray(substr($responseContent, 0, $headerSize));
383+
$response['body'] = substr($responseContent, $headerSize);
384+
385+
if (isset($response['headers']['request_header'])) {
386+
$this->last_request['headers'] = $response['headers']['request_header'];
387+
}
388+
}
389+
390+
return $response;
391+
}
392+
372393
/**
373394
* Check if the response was successful or a failure. If it failed, store the error.
374395
* @param array $response The response from the curl request

0 commit comments

Comments
 (0)