Complex classes like MailChimp often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MailChimp, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class MailChimp |
||
| 14 | { |
||
| 15 | private $api_key; |
||
| 16 | private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; |
||
| 17 | |||
| 18 | const TIMEOUT = 10; |
||
| 19 | |||
| 20 | /* SSL Verification |
||
| 21 | Read before disabling: |
||
| 22 | http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/ |
||
| 23 | */ |
||
| 24 | public $verify_ssl = true; |
||
| 25 | |||
| 26 | private $request_successful = false; |
||
| 27 | private $last_error = ''; |
||
| 28 | private $last_response = array(); |
||
| 29 | private $last_request = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create a new instance |
||
| 33 | * |
||
| 34 | * @param string $api_key Your MailChimp API key |
||
| 35 | * @param string $api_endpoint Optional custom API endpoint |
||
| 36 | * |
||
| 37 | * @throws \Exception |
||
| 38 | */ |
||
| 39 | public function __construct($api_key, $api_endpoint = null) |
||
| 40 | { |
||
| 41 | if (!function_exists('curl_init') || !function_exists('curl_setopt')) { |
||
| 42 | throw new \Exception("cURL support is required, but can't be found."); |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->api_key = $api_key; |
||
| 46 | |||
| 47 | if ($api_endpoint === null) { |
||
| 48 | if (strpos($this->api_key, '-') === false) { |
||
| 49 | throw new \Exception("Invalid MailChimp API key `{$api_key}` supplied."); |
||
| 50 | } |
||
| 51 | list(, $data_center) = explode('-', $this->api_key); |
||
| 52 | $this->api_endpoint = str_replace('<dc>', $data_center, $this->api_endpoint); |
||
| 53 | } else { |
||
| 54 | $this->api_endpoint = $api_endpoint; |
||
| 55 | } |
||
| 56 | |||
| 57 | $this->last_response = array('headers' => null, 'body' => null); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create a new instance of a Batch request. Optionally with the ID of an existing batch. |
||
| 62 | * |
||
| 63 | * @param string $batch_id Optional ID of an existing batch, if you need to check its status for example. |
||
| 64 | * |
||
| 65 | * @return Batch New Batch object. |
||
| 66 | */ |
||
| 67 | public function new_batch($batch_id = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return string The url to the API endpoint |
||
| 74 | */ |
||
| 75 | public function getApiEndpoint() |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL |
||
| 83 | * |
||
| 84 | * @param string $email The subscriber's email address |
||
| 85 | * |
||
| 86 | * @return string Hashed version of the input |
||
| 87 | */ |
||
| 88 | public function subscriberHash($email) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Was the last request successful? |
||
| 95 | * |
||
| 96 | * @return bool True for success, false for failure |
||
| 97 | */ |
||
| 98 | public function success() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get the last error returned by either the network transport, or by the API. |
||
| 105 | * If something didn't work, this should contain the string describing the problem. |
||
| 106 | * |
||
| 107 | * @return string|false describing the error |
||
| 108 | */ |
||
| 109 | public function getLastError() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get an array containing the HTTP headers and the body of the API response. |
||
| 116 | * |
||
| 117 | * @return array Assoc array with keys 'headers' and 'body' |
||
| 118 | */ |
||
| 119 | public function getLastResponse() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get an array containing the HTTP headers and the body of the API request. |
||
| 126 | * |
||
| 127 | * @return array Assoc array |
||
| 128 | */ |
||
| 129 | public function getLastRequest() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Make an HTTP DELETE request - for deleting data |
||
| 136 | * |
||
| 137 | * @param string $method URL of the API request method |
||
| 138 | * @param array $args Assoc array of arguments (if any) |
||
| 139 | * @param int $timeout Timeout limit for request in seconds |
||
| 140 | * |
||
| 141 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 142 | */ |
||
| 143 | public function delete($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Make an HTTP GET request - for retrieving data |
||
| 150 | * |
||
| 151 | * @param string $method URL of the API request method |
||
| 152 | * @param array $args Assoc array of arguments (usually your data) |
||
| 153 | * @param int $timeout Timeout limit for request in seconds |
||
| 154 | * |
||
| 155 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 156 | */ |
||
| 157 | public function get($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Make an HTTP PATCH request - for performing partial updates |
||
| 164 | * |
||
| 165 | * @param string $method URL of the API request method |
||
| 166 | * @param array $args Assoc array of arguments (usually your data) |
||
| 167 | * @param int $timeout Timeout limit for request in seconds |
||
| 168 | * |
||
| 169 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 170 | */ |
||
| 171 | public function patch($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Make an HTTP POST request - for creating and updating items |
||
| 178 | * |
||
| 179 | * @param string $method URL of the API request method |
||
| 180 | * @param array $args Assoc array of arguments (usually your data) |
||
| 181 | * @param int $timeout Timeout limit for request in seconds |
||
| 182 | * |
||
| 183 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 184 | */ |
||
| 185 | public function post($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Make an HTTP PUT request - for creating new items |
||
| 192 | * |
||
| 193 | * @param string $method URL of the API request method |
||
| 194 | * @param array $args Assoc array of arguments (usually your data) |
||
| 195 | * @param int $timeout Timeout limit for request in seconds |
||
| 196 | * |
||
| 197 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 198 | */ |
||
| 199 | public function put($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Performs the underlying HTTP request. Not very exciting. |
||
| 206 | * |
||
| 207 | * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete |
||
| 208 | * @param string $method The API method to be called |
||
| 209 | * @param array $args Assoc array of parameters to be passed |
||
| 210 | * @param int $timeout |
||
| 211 | * |
||
| 212 | * @return array|false Assoc array of decoded result |
||
| 213 | */ |
||
| 214 | private function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $http_verb |
||
| 283 | * @param string $method |
||
| 284 | * @param string $url |
||
| 285 | * @param integer $timeout |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | private function prepareStateForRequest($http_verb, $method, $url, $timeout) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get the HTTP headers as an array of header-name => header-value pairs. |
||
| 314 | * |
||
| 315 | * The "Link" header is parsed into an associative array based on the |
||
| 316 | * rel names it contains. The original value is available under |
||
| 317 | * the "_raw" key. |
||
| 318 | * |
||
| 319 | * @param string $headersAsString |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | private function getHeadersAsArray($headersAsString) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Extract all rel => URL pairs from the provided Link header value |
||
| 354 | * |
||
| 355 | * Mailchimp only implements the URI reference and relation type from |
||
| 356 | * RFC 5988, so the value of the header is something like this: |
||
| 357 | * |
||
| 358 | * 'https://us13.api.mailchimp.com/schema/3.0/Lists/Instance.json; rel="describedBy", |
||
| 359 | * <https://us13.admin.mailchimp.com/lists/members/?id=XXXX>; rel="dashboard"' |
||
| 360 | * |
||
| 361 | * @param string $linkHeaderAsString |
||
| 362 | * |
||
| 363 | * @return array |
||
| 364 | */ |
||
| 365 | private function getLinkHeaderAsArray($linkHeaderAsString) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Encode the data and attach it to the request |
||
| 380 | * |
||
| 381 | * @param resource $ch cURL session handle, used by reference |
||
| 382 | * @param array $data Assoc array of data to attach |
||
| 383 | */ |
||
| 384 | private function attachRequestPayload(&$ch, $data) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Decode the response and format any error messages for debugging |
||
| 393 | * |
||
| 394 | * @param array $response The response from the curl request |
||
| 395 | * |
||
| 396 | * @return array|false The JSON decoded into an array |
||
| 397 | */ |
||
| 398 | private function formatResponse($response) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Do post-request formatting and setting state from the response |
||
| 411 | * |
||
| 412 | * @param array $response The response from the curl request |
||
| 413 | * @param string $responseContent The body of the response from the curl request |
||
| 414 | * @param resource $ch The curl resource |
||
| 415 | * |
||
| 416 | * @return array The modified response |
||
| 417 | */ |
||
| 418 | private function setResponseState($response, $responseContent, $ch) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Check if the response was successful or a failure. If it failed, store the error. |
||
| 439 | * |
||
| 440 | * @param array $response The response from the curl request |
||
| 441 | * @param array|false $formattedResponse The response body payload from the curl request |
||
| 442 | * @param int $timeout The timeout supplied to the curl request. |
||
| 443 | * |
||
| 444 | * @return bool If the request was successful |
||
| 445 | */ |
||
| 446 | private function determineSuccess($response, $formattedResponse, $timeout) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Find the HTTP status code from the headers or API response body |
||
| 471 | * |
||
| 472 | * @param array $response The response from the curl request |
||
| 473 | * @param array|false $formattedResponse The response body payload from the curl request |
||
| 474 | * |
||
| 475 | * @return int HTTP status code |
||
| 476 | */ |
||
| 477 | private function findHTTPStatus($response, $formattedResponse) |
||
| 489 | } |
||
| 490 |