Skip to content

Commit

Permalink
refactor connection and response classes
Browse files Browse the repository at this point in the history
  • Loading branch information
John Hutcheson committed Jul 18, 2018
1 parent d50066f commit eaf8d15
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 106 deletions.
87 changes: 81 additions & 6 deletions src/Utilities/MailchimpConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
* TODO should make over wire request
*/

use MailchimpAPI\MailchimpException;

/**
* Class MailchimpConnection
* @package MailchimpAPI\Utilities
*/
class MailchimpConnection implements HttpRequest
{

Expand All @@ -14,8 +20,14 @@ class MailchimpConnection implements HttpRequest
*/
const USER_AGENT = 'jhut89/Mailchimp-API-3.0-PHP (https://github.com/Jhut89/Mailchimp-API-3.0-PHP)';

/**
* the url used to request an access token from mailchimp
*/
const TOKEN_REQUEST_URL = 'https://login.mailchimp.com/oauth2/token';

/**
* the url used to request metadata about an access token
*/
const OAUTH_METADATA_URL = 'https://login.mailchimp.com/oauth2/metadata/';

/**
Expand All @@ -29,9 +41,28 @@ class MailchimpConnection implements HttpRequest
private $current_settings;

/**
* @var MailchimpResponse
* raw response from mailchimp api
* @var string
*/
private $response;

/**
* response body
* @var string
*/
private $response_body;

/**
* an integer representation of the http response code
* @var int
*/
private $mc_response;
private $http_code;

/**
* the parsed response headers from the request
* @var array
*/
private $headers = [];

/**
* @var resource
Expand Down Expand Up @@ -86,7 +117,8 @@ private function prepareHandle()
// set verify ssl
$this->setOption(CURLOPT_SSL_VERIFYPEER, $this->current_settings->shouldVerifySsl());

$this->setOption(CURLOPT_HEADERFUNCTION, [&$this->mc_response, "handleResponseHeader"]);
// set the callback to run against each of the response headers
$this->setOption(CURLOPT_HEADERFUNCTION, [&$this, "parseResponseHeader"]);
}

/**
Expand Down Expand Up @@ -118,13 +150,29 @@ private function setHandlerOptionsForMethod()
}

/**
* @throws MailchimpException
* @return MailchimpResponse
*/
public function execute()
{
$this->mc_response = new MailchimpResponse($this);
$this->mc_response->parseRaw($this->executeCurl());
return $this->mc_response;
$this->response = $this->executeCurl();
if (!$this->response) {
throw new MailchimpException("The curl request failed");
}

$this->http_code = $this->getInfo(CURLINFO_HTTP_CODE);
$head_len = $this->getInfo(CURLINFO_HEADER_SIZE);
$this->response_body = substr(
$this->response,
$head_len,
strlen($this->response)
);

return new MailchimpResponse(
$this->headers,
$this->response_body,
$this->http_code
);
}

/**
Expand Down Expand Up @@ -187,4 +235,31 @@ public function close()
{
curl_close($this->handle);
}

/**
* Called statically during prepareHandle();
*
* @param $handle
* @param $header
* @return int
*/
private function parseResponseHeader($handle, $header)
{
$header_length = strlen($header);
$header_array = explode(':', $header, 2);
if (count($header_array) == 2) {
$this->pushToHeaders($header_array);
}

var_dump($header);
return $header_length;
}

/**
* @param array $header
*/
private function pushToHeaders($header)
{
$this->headers[$header[0]] = trim($header[1]);
}
}
114 changes: 14 additions & 100 deletions src/Utilities/MailchimpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,20 @@

class MailchimpResponse
{
// The head of the response document
private $head;

// The headers received as an array of key value pairs
private $headers = [];

// raw response
private $raw;

// MailchimpConnection
private $connection;

// Response from MailChimp API
private $body;

// HTTP Response Code
private $http_code;

public function __construct(MailchimpConnection $connection)
public function __construct($headers, $body, $http_code)
{
$this->connection = $connection;
}

public function parseRaw($raw_response)
{
$this->setRaw($raw_response);
$this->http_code = $this->connection->getInfo(CURLINFO_HTTP_CODE);
$head_len = $this->connection->getInfo(CURLINFO_HEADER_SIZE);

$this->setHead(substr($raw_response, 0, $head_len));
$this->setBody(substr($raw_response, $head_len, strlen($raw_response)));
$this->setHeaders($headers);
$this->setBody($body);
$this->setHttpCode($http_code);
}

/**
Expand All @@ -55,23 +38,6 @@ public function setHeaders($headers)
$this->headers = $headers;
}

/**
* @return mixed
*/
public function getHead()
{
return $this->head;
}

/**
* @param mixed $head
*/
public function setHead($head)
{
$this->head = $head;
}


/**
* @return mixed
*/
Expand All @@ -80,16 +46,6 @@ public function getHttpCode()
return $this->http_code;
}

/**
* @return mixed
*
* @throws MailchimpException when cant deserialize response
*/
public function deserialize()
{
return $this->deserializeResponse($this->body);
}

/**
* @param mixed $http_code
*/
Expand All @@ -107,68 +63,26 @@ public function setBody($body)
}

/**
* @param $response
* @return mixed
* @throws MailchimpException
*/
public function deserializeResponse($response)
{
$decoded = json_decode($response);

if (!$decoded) {
throw new MailchimpException("Unable to deserialize response");
}

return $decoded;
}

/**
* @param array $header
*/
public function pushToHeaders($header)
{
$this->headers[$header[0]] = trim($header[1]);
}

/**
* Called statically during prepareHandle();
*
* @param $handle
* @param $header
* @return int
*/
public function handleResponseHeader($handle, $header)
public function getBody()
{
$header_length = strlen($header);
$header_array = explode(':', $header, 2);
if (count($header_array) == 2) {
$this->pushToHeaders($header_array);
}

return $header_length;
return $this->body;
}

/**
* @return mixed
*
* @throws MailchimpException when cant deserialize response
*/
public function getRaw()
public function deserialize()
{
return $this->raw;
}
$decoded = json_decode($this->body);

/**
* @param mixed $raw
*/
public function setRaw($raw)
{
$this->raw = $raw;
}
if (!$decoded) {
throw new MailchimpException("Unable to deserialize response");
}

/**
* @return mixed
*/
public function getBody()
{
return $this->body;
return $decoded;
}
}

0 comments on commit eaf8d15

Please sign in to comment.