diff --git a/src/MailChimp.php b/src/MailChimp.php index e810836..7743776 100644 --- a/src/MailChimp.php +++ b/src/MailChimp.php @@ -12,31 +12,31 @@ */ class MailChimp { - private $api_key; - private $api_endpoint = 'https://.api.mailchimp.com/3.0'; + private string $api_key; + private string $api_endpoint = 'https://.api.mailchimp.com/3.0'; - const TIMEOUT = 10; + private const TIMEOUT = 10; /* SSL Verification Read before disabling: http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/ */ - public $verify_ssl = true; + public bool $verify_ssl = true; - private $request_successful = false; - private $last_error = ''; - private $last_response = array(); - private $last_request = array(); + private bool $request_successful = false; + private string $last_error = ''; + private array $last_response = []; + private array $last_request = []; /** * Create a new instance * - * @param string $api_key Your MailChimp API key - * @param string $api_endpoint Optional custom API endpoint + * @param string $api_key Your MailChimp API key + * @param string|null $api_endpoint Optional custom API endpoint * * @throws \Exception */ - public function __construct($api_key, $api_endpoint = null) + public function __construct(string $api_key, string $api_endpoint = null) { if (!function_exists('curl_init') || !function_exists('curl_setopt')) { throw new \Exception("cURL support is required, but can't be found."); @@ -45,7 +45,7 @@ public function __construct($api_key, $api_endpoint = null) $this->api_key = $api_key; if ($api_endpoint === null) { - if (strpos($this->api_key, '-') === false) { + if (!str_contains($this->api_key, '-')) { throw new \Exception("Invalid MailChimp API key supplied."); } list(, $data_center) = explode('-', $this->api_key); @@ -60,11 +60,11 @@ public function __construct($api_key, $api_endpoint = null) /** * Create a new instance of a Batch request. Optionally with the ID of an existing batch. * - * @param string $batch_id Optional ID of an existing batch, if you need to check its status for example. + * @param string|null $batch_id Optional ID of an existing batch, if you need to check its status for example. * * @return Batch New Batch object. */ - public function new_batch($batch_id = null) + public function new_batch(string $batch_id = null): Batch { return new Batch($this, $batch_id); } @@ -72,7 +72,7 @@ public function new_batch($batch_id = null) /** * @return string The url to the API endpoint */ - public function getApiEndpoint() + public function getApiEndpoint(): string { return $this->api_endpoint; } @@ -81,11 +81,11 @@ public function getApiEndpoint() /** * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL * - * @param string $email The subscriber's email address + * @param string $email The subscriber's email address * * @return string Hashed version of the input */ - public static function subscriberHash($email) + public static function subscriberHash(string $email): string { return md5(strtolower($email)); } @@ -95,7 +95,7 @@ public static function subscriberHash($email) * * @return bool True for success, false for failure */ - public function success() + public function success(): bool { return $this->request_successful; } @@ -106,7 +106,7 @@ public function success() * * @return string|false describing the error */ - public function getLastError() + public function getLastError(): bool|string { return $this->last_error ?: false; } @@ -116,7 +116,7 @@ public function getLastError() * * @return array Assoc array with keys 'headers' and 'body' */ - public function getLastResponse() + public function getLastResponse(): array { return $this->last_response; } @@ -126,7 +126,7 @@ public function getLastResponse() * * @return array Assoc array */ - public function getLastRequest() + public function getLastRequest(): array { return $this->last_request; } @@ -134,13 +134,13 @@ public function getLastRequest() /** * Make an HTTP DELETE request - for deleting data * - * @param string $method URL of the API request method - * @param array $args Assoc array of arguments (if any) - * @param int $timeout Timeout limit for request in seconds + * @param string $method URL of the API request method + * @param array $args Assoc array of arguments (if any) + * @param int $timeout Timeout limit for request in seconds * * @return array|false Assoc array of API response, decoded from JSON */ - public function delete($method, $args = array(), $timeout = self::TIMEOUT) + public function delete(string $method, array $args = [], int $timeout = self::TIMEOUT): bool|array { return $this->makeRequest('delete', $method, $args, $timeout); } @@ -148,13 +148,13 @@ public function delete($method, $args = array(), $timeout = self::TIMEOUT) /** * Make an HTTP GET request - for retrieving data * - * @param string $method URL of the API request method - * @param array $args Assoc array of arguments (usually your data) - * @param int $timeout Timeout limit for request in seconds + * @param string $method URL of the API request method + * @param array $args Assoc array of arguments (usually your data) + * @param int $timeout Timeout limit for request in seconds * * @return array|false Assoc array of API response, decoded from JSON */ - public function get($method, $args = array(), $timeout = self::TIMEOUT) + public function get(string $method, array $args = [], int $timeout = self::TIMEOUT): bool|array { return $this->makeRequest('get', $method, $args, $timeout); } @@ -162,13 +162,13 @@ public function get($method, $args = array(), $timeout = self::TIMEOUT) /** * Make an HTTP PATCH request - for performing partial updates * - * @param string $method URL of the API request method - * @param array $args Assoc array of arguments (usually your data) - * @param int $timeout Timeout limit for request in seconds + * @param string $method URL of the API request method + * @param array $args Assoc array of arguments (usually your data) + * @param int $timeout Timeout limit for request in seconds * * @return array|false Assoc array of API response, decoded from JSON */ - public function patch($method, $args = array(), $timeout = self::TIMEOUT) + public function patch(string $method, array $args = [], int $timeout = self::TIMEOUT): bool|array { return $this->makeRequest('patch', $method, $args, $timeout); } @@ -176,13 +176,13 @@ public function patch($method, $args = array(), $timeout = self::TIMEOUT) /** * Make an HTTP POST request - for creating and updating items * - * @param string $method URL of the API request method - * @param array $args Assoc array of arguments (usually your data) - * @param int $timeout Timeout limit for request in seconds + * @param string $method URL of the API request method + * @param array $args Assoc array of arguments (usually your data) + * @param int $timeout Timeout limit for request in seconds * * @return array|false Assoc array of API response, decoded from JSON */ - public function post($method, $args = array(), $timeout = self::TIMEOUT) + public function post(string $method, array $args = [], int $timeout = self::TIMEOUT) { return $this->makeRequest('post', $method, $args, $timeout); } @@ -190,13 +190,13 @@ public function post($method, $args = array(), $timeout = self::TIMEOUT) /** * Make an HTTP PUT request - for creating new items * - * @param string $method URL of the API request method - * @param array $args Assoc array of arguments (usually your data) - * @param int $timeout Timeout limit for request in seconds + * @param string $method URL of the API request method + * @param array $args Assoc array of arguments (usually your data) + * @param int $timeout Timeout limit for request in seconds * * @return array|false Assoc array of API response, decoded from JSON */ - public function put($method, $args = array(), $timeout = self::TIMEOUT) + public function put(string $method, array $args = [], int $timeout = self::TIMEOUT): bool|array { return $this->makeRequest('put', $method, $args, $timeout); } @@ -204,15 +204,19 @@ public function put($method, $args = array(), $timeout = self::TIMEOUT) /** * Performs the underlying HTTP request. Not very exciting. * - * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete - * @param string $method The API method to be called - * @param array $args Assoc array of parameters to be passed - * @param int $timeout + * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete + * @param string $method The API method to be called + * @param array $args Assoc array of parameters to be passed + * @param int $timeout * * @return array|false Assoc array of decoded result */ - private function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT) - { + private function makeRequest( + string $http_verb, + string $method, + array $args = [], + int $timeout = self::TIMEOUT + ): bool|array { $url = $this->api_endpoint . '/' . $method; $response = $this->prepareStateForRequest($http_verb, $method, $url, $timeout); @@ -269,10 +273,10 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se break; } - $responseContent = curl_exec($ch); + $responseContent = curl_exec($ch); $response['headers'] = curl_getinfo($ch); - $response = $this->setResponseState($response, $responseContent, $ch); - $formattedResponse = $this->formatResponse($response); + $response = $this->setResponseState($response, $responseContent, $ch); + $formattedResponse = $this->formatResponse($response); curl_close($ch); @@ -282,30 +286,30 @@ private function makeRequest($http_verb, $method, $args = array(), $timeout = se } /** - * @param string $http_verb - * @param string $method - * @param string $url + * @param string $http_verb + * @param string $method + * @param string $url * @param integer $timeout * * @return array */ - private function prepareStateForRequest($http_verb, $method, $url, $timeout) + private function prepareStateForRequest(string $http_verb, string $method, string $url, int $timeout): array { $this->last_error = ''; $this->request_successful = false; $this->last_response = array( - 'headers' => null, // array of details from curl_getinfo() + 'headers' => null, // array of details from curl_getinfo() 'httpHeaders' => null, // array of HTTP headers - 'body' => null // content of the response + 'body' => null // content of the response ); $this->last_request = array( - 'method' => $http_verb, - 'path' => $method, - 'url' => $url, - 'body' => '', + 'method' => $http_verb, + 'path' => $method, + 'url' => $url, + 'body' => '', 'timeout' => $timeout, ); @@ -323,9 +327,9 @@ private function prepareStateForRequest($http_verb, $method, $url, $timeout) * * @return array */ - private function getHeadersAsArray($headersAsString) + private function getHeadersAsArray(string $headersAsString): array { - $headers = array(); + $headers = []; foreach (explode("\r\n", $headersAsString) as $i => $line) { if (preg_match('/HTTP\/[1-2]/', substr($line, 0, 7)) === 1) { // http code @@ -366,9 +370,9 @@ private function getHeadersAsArray($headersAsString) * * @return array */ - private function getLinkHeaderAsArray($linkHeaderAsString) + private function getLinkHeaderAsArray(string $linkHeaderAsString): array { - $urls = array(); + $urls = []; if (preg_match_all('/<(.*?)>\s*;\s*rel="(.*?)"\s*/', $linkHeaderAsString, $matches)) { foreach ($matches[2] as $i => $relName) { @@ -382,12 +386,12 @@ private function getLinkHeaderAsArray($linkHeaderAsString) /** * Encode the data and attach it to the request * - * @param resource $ch cURL session handle, used by reference - * @param array $data Assoc array of data to attach + * @param resource $ch cURL session handle, used by reference + * @param array $data Assoc array of data to attach */ - private function attachRequestPayload(&$ch, $data) + private function attachRequestPayload(&$ch, array $data): void { - $encoded = json_encode($data); + $encoded = json_encode($data); $this->last_request['body'] = $encoded; curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded); } @@ -399,7 +403,7 @@ private function attachRequestPayload(&$ch, $data) * * @return array|false The JSON decoded into an array */ - private function formatResponse($response) + private function formatResponse(array $response): bool|array { $this->last_response = $response; @@ -413,22 +417,21 @@ private function formatResponse($response) /** * Do post-request formatting and setting state from the response * - * @param array $response The response from the curl request - * @param string $responseContent The body of the response from the curl request - * @param resource $ch The curl resource + * @param array $response The response from the curl request + * @param string $responseContent The body of the response from the curl request + * @param resource $ch The curl resource * * @return array The modified response */ - private function setResponseState($response, $responseContent, $ch) + private function setResponseState(array $response, string $responseContent, $ch): array { if ($responseContent === false) { $this->last_error = curl_error($ch); } else { - $headerSize = $response['headers']['header_size']; $response['httpHeaders'] = $this->getHeadersAsArray(substr($responseContent, 0, $headerSize)); - $response['body'] = substr($responseContent, $headerSize); + $response['body'] = substr($responseContent, $headerSize); if (isset($response['headers']['request_header'])) { $this->last_request['headers'] = $response['headers']['request_header']; @@ -441,13 +444,13 @@ private function setResponseState($response, $responseContent, $ch) /** * Check if the response was successful or a failure. If it failed, store the error. * - * @param array $response The response from the curl request - * @param array|false $formattedResponse The response body payload from the curl request - * @param int $timeout The timeout supplied to the curl request. + * @param array $response The response from the curl request + * @param bool|array $formattedResponse The response body payload from the curl request + * @param int $timeout The timeout supplied to the curl request. * * @return bool If the request was successful */ - private function determineSuccess($response, $formattedResponse, $timeout) + private function determineSuccess(array $response, bool|array $formattedResponse, int $timeout): bool { $status = $this->findHTTPStatus($response, $formattedResponse); @@ -473,12 +476,12 @@ private function determineSuccess($response, $formattedResponse, $timeout) /** * Find the HTTP status code from the headers or API response body * - * @param array $response The response from the curl request - * @param array|false $formattedResponse The response body payload from the curl request + * @param array $response The response from the curl request + * @param bool|array $formattedResponse The response body payload from the curl request * * @return int HTTP status code */ - private function findHTTPStatus($response, $formattedResponse) + private function findHTTPStatus(array $response, bool|array $formattedResponse): int { if (!empty($response['headers']) && isset($response['headers']['http_code'])) { return (int)$response['headers']['http_code'];