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 |
||
| 14 | class MailChimp |
||
| 15 | { |
||
| 16 | private $api_key; |
||
| 17 | private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; |
||
| 18 | |||
| 19 | const TIMEOUT = 10; |
||
| 20 | |||
| 21 | /* SSL Verification |
||
| 22 | Read before disabling: |
||
| 23 | http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/ |
||
| 24 | */ |
||
| 25 | public $verify_ssl = true; |
||
| 26 | |||
| 27 | private $request_successful = false; |
||
| 28 | private $last_error = ''; |
||
| 29 | private $last_response = array(); |
||
| 30 | private $last_request = array(); |
||
| 31 | private $errors_array = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Create a new instance |
||
| 35 | * |
||
| 36 | * @param string $api_key Your MailChimp API key |
||
| 37 | * @param string $api_endpoint Optional custom API endpoint |
||
| 38 | * |
||
| 39 | * @throws \Exception |
||
| 40 | */ |
||
| 41 | public function __construct($api_key, $api_endpoint = null) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Create a new instance of a Batch request. Optionally with the ID of an existing batch. |
||
| 64 | * |
||
| 65 | * @param string $batch_id Optional ID of an existing batch, if you need to check its status for example. |
||
| 66 | * |
||
| 67 | * @return Batch New Batch object. |
||
| 68 | */ |
||
| 69 | public function new_batch($batch_id = null) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return string The url to the API endpoint |
||
| 76 | */ |
||
| 77 | public function getApiEndpoint() |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL |
||
| 85 | * |
||
| 86 | * @param string $email The subscriber's email address |
||
| 87 | * |
||
| 88 | * @return string Hashed version of the input |
||
| 89 | */ |
||
| 90 | public function subscriberHash($email) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Was the last request successful? |
||
| 97 | * |
||
| 98 | * @return bool True for success, false for failure |
||
| 99 | */ |
||
| 100 | public function success() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the last error returned by either the network transport, or by the API. |
||
| 107 | * If something didn't work, this should contain the string describing the problem. |
||
| 108 | * |
||
| 109 | * @return string|false describing the error |
||
| 110 | */ |
||
| 111 | public function getLastError() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get errors array, in case of multiple errors got back from body response. |
||
| 118 | * Generically, this errors are thrown from wrong inserted MERGE_FIELDS value, giving a 400 status code |
||
| 119 | * |
||
| 120 | * @return array the errors array |
||
| 121 | */ |
||
| 122 | public function getErrorsArray() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get an array containing the HTTP headers and the body of the API response. |
||
| 129 | * |
||
| 130 | * @return array Assoc array with keys 'headers' and 'body' |
||
| 131 | */ |
||
| 132 | public function getLastResponse() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get an array containing the HTTP headers and the body of the API request. |
||
| 139 | * |
||
| 140 | * @return array Assoc array |
||
| 141 | */ |
||
| 142 | public function getLastRequest() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Make an HTTP DELETE request - for deleting data |
||
| 149 | * |
||
| 150 | * @param string $method URL of the API request method |
||
| 151 | * @param array $args Assoc array of arguments (if any) |
||
| 152 | * @param int $timeout Timeout limit for request in seconds |
||
| 153 | * |
||
| 154 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 155 | */ |
||
| 156 | public function delete($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Make an HTTP GET request - for retrieving data |
||
| 163 | * |
||
| 164 | * @param string $method URL of the API request method |
||
| 165 | * @param array $args Assoc array of arguments (usually your data) |
||
| 166 | * @param int $timeout Timeout limit for request in seconds |
||
| 167 | * |
||
| 168 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 169 | */ |
||
| 170 | public function get($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Make an HTTP PATCH request - for performing partial updates |
||
| 177 | * |
||
| 178 | * @param string $method URL of the API request method |
||
| 179 | * @param array $args Assoc array of arguments (usually your data) |
||
| 180 | * @param int $timeout Timeout limit for request in seconds |
||
| 181 | * |
||
| 182 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 183 | */ |
||
| 184 | public function patch($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Make an HTTP POST request - for creating and updating items |
||
| 191 | * |
||
| 192 | * @param string $method URL of the API request method |
||
| 193 | * @param array $args Assoc array of arguments (usually your data) |
||
| 194 | * @param int $timeout Timeout limit for request in seconds |
||
| 195 | * |
||
| 196 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 197 | */ |
||
| 198 | public function post($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Make an HTTP PUT request - for creating new items |
||
| 205 | * |
||
| 206 | * @param string $method URL of the API request method |
||
| 207 | * @param array $args Assoc array of arguments (usually your data) |
||
| 208 | * @param int $timeout Timeout limit for request in seconds |
||
| 209 | * |
||
| 210 | * @return array|false Assoc array of API response, decoded from JSON |
||
| 211 | */ |
||
| 212 | public function put($method, $args = array(), $timeout = self::TIMEOUT) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Performs the underlying HTTP request. Not very exciting. |
||
| 219 | * |
||
| 220 | * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete |
||
| 221 | * @param string $method The API method to be called |
||
| 222 | * @param array $args Assoc array of parameters to be passed |
||
| 223 | * @param int $timeout |
||
| 224 | * |
||
| 225 | * @return array|false Assoc array of decoded result |
||
| 226 | */ |
||
| 227 | private function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $http_verb |
||
| 296 | * @param string $method |
||
| 297 | * @param string $url |
||
| 298 | * @param integer $timeout |
||
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | private function prepareStateForRequest($http_verb, $method, $url, $timeout) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get the HTTP headers as an array of header-name => header-value pairs. |
||
| 327 | * |
||
| 328 | * The "Link" header is parsed into an associative array based on the |
||
| 329 | * rel names it contains. The original value is available under |
||
| 330 | * the "_raw" key. |
||
| 331 | * |
||
| 332 | * @param string $headersAsString |
||
| 333 | * |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | private function getHeadersAsArray($headersAsString) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Extract all rel => URL pairs from the provided Link header value |
||
| 367 | * |
||
| 368 | * Mailchimp only implements the URI reference and relation type from |
||
| 369 | * RFC 5988, so the value of the header is something like this: |
||
| 370 | * |
||
| 371 | * 'https://us13.api.mailchimp.com/schema/3.0/Lists/Instance.json; rel="describedBy", |
||
| 372 | * <https://us13.admin.mailchimp.com/lists/members/?id=XXXX>; rel="dashboard"' |
||
| 373 | * |
||
| 374 | * @param string $linkHeaderAsString |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | private function getLinkHeaderAsArray($linkHeaderAsString) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Encode the data and attach it to the request |
||
| 393 | * |
||
| 394 | * @param resource $ch cURL session handle, used by reference |
||
| 395 | * @param array $data Assoc array of data to attach |
||
| 396 | */ |
||
| 397 | private function attachRequestPayload(&$ch, $data) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Decode the response and format any error messages for debugging |
||
| 406 | * |
||
| 407 | * @param array $response The response from the curl request |
||
| 408 | * |
||
| 409 | * @return array|false The JSON decoded into an array |
||
| 410 | */ |
||
| 411 | private function formatResponse($response) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Do post-request formatting and setting state from the response |
||
| 424 | * |
||
| 425 | * @param array $response The response from the curl request |
||
| 426 | * @param string $responseContent The body of the response from the curl request |
||
| 427 | * @param resource $ch The curl resource |
||
| 428 | * |
||
| 429 | * @return array The modified response |
||
| 430 | */ |
||
| 431 | private function setResponseState($response, $responseContent, $ch) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Check if the response was successful or a failure. If it failed, store the error. |
||
| 452 | * |
||
| 453 | * @param array $response The response from the curl request |
||
| 454 | * @param array|false $formattedResponse The response body payload from the curl request |
||
| 455 | * @param int $timeout The timeout supplied to the curl request. |
||
| 456 | * |
||
| 457 | * @return bool If the request was successful |
||
| 458 | */ |
||
| 459 | private function determineSuccess($response, $formattedResponse, $timeout) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Find the HTTP status code from the headers or API response body |
||
| 488 | * |
||
| 489 | * @param array $response The response from the curl request |
||
| 490 | * @param array|false $formattedResponse The response body payload from the curl request |
||
| 491 | * |
||
| 492 | * @return int HTTP status code |
||
| 493 | */ |
||
| 494 | private function findHTTPStatus($response, $formattedResponse) |
||
| 506 | } |
||
| 507 |