From 09eda3df130d4d094fe37471a5ac21065e13af24 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Sun, 24 Jul 2016 12:40:50 +0100 Subject: [PATCH 1/3] PHP Code Sniffer --- .../Exceptions/GenericHTTPError.php | 37 ++-- .../Exceptions/InvalidCredentials.php | 4 +- .../Connection/Exceptions/MissingEndpoint.php | 4 +- .../Exceptions/MissingRequiredParameters.php | 4 +- .../Exceptions/NoDomainsConfigured.php | 4 +- src/Mailgun/Connection/RestClient.php | 41 +++-- src/Mailgun/Constants/Api.php | 3 +- src/Mailgun/Constants/ExceptionMessages.php | 3 +- src/Mailgun/Lists/OptInHandler.php | 41 +++-- src/Mailgun/Mailgun.php | 56 +++--- src/Mailgun/Messages/BatchMessage.php | 164 +++++++++--------- .../Messages/Exceptions/InvalidParameter.php | 4 +- .../Exceptions/InvalidParameterType.php | 4 +- .../MissingRequiredMIMEParameters.php | 4 +- .../Messages/Exceptions/TooManyParameters.php | 4 +- src/Mailgun/Messages/MessageBuilder.php | 30 ++-- 16 files changed, 225 insertions(+), 182 deletions(-) diff --git a/src/Mailgun/Connection/Exceptions/GenericHTTPError.php b/src/Mailgun/Connection/Exceptions/GenericHTTPError.php index b370649..35439d8 100644 --- a/src/Mailgun/Connection/Exceptions/GenericHTTPError.php +++ b/src/Mailgun/Connection/Exceptions/GenericHTTPError.php @@ -3,23 +3,26 @@ namespace Mailgun\Connection\Exceptions; class GenericHTTPError extends \Exception { - protected $httpResponseCode; - protected $httpResponseBody; - - public function __construct($message=null, $response_code=null, $response_body=null, $code=0, \Exception $previous=null) { - parent::__construct($message, $code, $previous); - - $this->httpResponseCode = $response_code; - $this->httpResponseBody = $response_body; - } - - public function getHttpResponseCode() { - return $this->httpResponseCode; - } - - public function getHttpResponseBody() { - return $this->httpResponseBody; - } + protected $httpResponseCode; + protected $httpResponseBody; + + public function __construct($message=null, $response_code=null, $response_body=null, $code=0, \Exception $previous=null) + { + parent::__construct($message, $code, $previous); + + $this->httpResponseCode = $response_code; + $this->httpResponseBody = $response_body; + } + + public function getHttpResponseCode() + { + return $this->httpResponseCode; + } + + public function getHttpResponseBody() + { + return $this->httpResponseBody; + } } ?> diff --git a/src/Mailgun/Connection/Exceptions/InvalidCredentials.php b/src/Mailgun/Connection/Exceptions/InvalidCredentials.php index bfcc743..b890ca7 100644 --- a/src/Mailgun/Connection/Exceptions/InvalidCredentials.php +++ b/src/Mailgun/Connection/Exceptions/InvalidCredentials.php @@ -1,4 +1,6 @@ getStatusCode(); switch ($httpResponseCode) { - case 200: - $data = (string)$responseObj->getBody(); - $jsonResponseData = json_decode($data, false); - $result = new \stdClass(); - // return response data as json if possible, raw if not - $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; - $result->http_response_code = $httpResponseCode; + case 200: + $data = (string)$responseObj->getBody(); + $jsonResponseData = json_decode($data, false); + $result = new \stdClass(); + // return response data as json if possible, raw if not + $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; + $result->http_response_code = $httpResponseCode; - return $result; - case 400: - throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj)); - case 401: - throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); - case 404: - throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj)); - default: - throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); + return $result; + case 400: + throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj)); + case 401: + throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); + case 404: + throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj)); + default: + throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); } } diff --git a/src/Mailgun/Constants/Api.php b/src/Mailgun/Constants/Api.php index 7a2908e..32a1dea 100644 --- a/src/Mailgun/Constants/Api.php +++ b/src/Mailgun/Constants/Api.php @@ -4,7 +4,8 @@ namespace Mailgun\Constants; -class Api { +class Api +{ const API_USER = "api"; const SDK_VERSION = "1.7"; const SDK_USER_AGENT = "mailgun-sdk-php"; diff --git a/src/Mailgun/Constants/ExceptionMessages.php b/src/Mailgun/Constants/ExceptionMessages.php index 8850041..99798c1 100644 --- a/src/Mailgun/Constants/ExceptionMessages.php +++ b/src/Mailgun/Constants/ExceptionMessages.php @@ -4,7 +4,8 @@ namespace Mailgun\Constants; -class ExceptionMessages { +class ExceptionMessages +{ const EXCEPTION_INVALID_CREDENTIALS = "Your credentials are incorrect."; const EXCEPTION_GENERIC_HTTP_ERROR = "An HTTP Error has occurred! Check your network connection and try again."; const EXCEPTION_MISSING_REQUIRED_PARAMETERS = "The parameters passed to the API were invalid. Check your inputs!"; diff --git a/src/Mailgun/Lists/OptInHandler.php b/src/Mailgun/Lists/OptInHandler.php index d929134..57347d0 100644 --- a/src/Mailgun/Lists/OptInHandler.php +++ b/src/Mailgun/Lists/OptInHandler.php @@ -12,7 +12,8 @@ use Mailgun\Messages\Expcetions\InvalidParameterType; * * @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Lists/README.md */ -class OptInHandler{ +class OptInHandler +{ /** * @param string $mailingList @@ -20,34 +21,36 @@ class OptInHandler{ * @param string $recipientAddress * @return string */ - public function generateHash($mailingList, $secretAppId, $recipientAddress){ - $innerPayload = array('r' => $recipientAddress, 'l' => $mailingList); - $encodedInnerPayload = base64_encode(json_encode($innerPayload)); + public function generateHash($mailingList, $secretAppId, $recipientAddress) + { + $innerPayload = array('r' => $recipientAddress, 'l' => $mailingList); + $encodedInnerPayload = base64_encode(json_encode($innerPayload)); - $innerHash = hash_hmac("sha1", $encodedInnerPayload, $secretAppId); - $outerPayload = array('h' => $innerHash, 'p' => $encodedInnerPayload); + $innerHash = hash_hmac("sha1", $encodedInnerPayload, $secretAppId); + $outerPayload = array('h' => $innerHash, 'p' => $encodedInnerPayload); - return urlencode(base64_encode(json_encode($outerPayload))); - } + return urlencode(base64_encode(json_encode($outerPayload))); + } /** * @param string $secretAppId * @param string $uniqueHash * @return array|bool */ - public function validateHash($secretAppId, $uniqueHash){ - $decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true); + public function validateHash($secretAppId, $uniqueHash) + { + $decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true); - $decodedHash = $decodedOuterPayload['h']; - $innerPayload = $decodedOuterPayload['p']; + $decodedHash = $decodedOuterPayload['h']; + $innerPayload = $decodedOuterPayload['p']; - $decodedInnerPayload = json_decode(base64_decode($innerPayload), true); - $computedInnerHash = hash_hmac("sha1", $innerPayload, $secretAppId); + $decodedInnerPayload = json_decode(base64_decode($innerPayload), true); + $computedInnerHash = hash_hmac("sha1", $innerPayload, $secretAppId); - if($computedInnerHash == $decodedHash){ - return array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']); - } + if($computedInnerHash == $decodedHash) { + return array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']); + } - return false; - } + return false; + } } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 16b3d9a..f6478f0 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -16,7 +16,8 @@ use Mailgun\Messages\MessageBuilder; * * @link https://github.com/mailgun/mailgun-php/blob/master/README.md */ -class Mailgun{ +class Mailgun +{ /** * @var RestClient @@ -30,8 +31,8 @@ class Mailgun{ /** * @param string|null $apiKey - * @param HttpClient $httpClient - * @param string $apiEndpoint + * @param HttpClient $httpClient + * @param string $apiEndpoint */ public function __construct( $apiKey = null, @@ -47,17 +48,18 @@ class Mailgun{ * MIME string. If sending MIME, the string must be passed in to the 3rd * position of the function call. * - * @param string $workingDomain - * @param array $postData - * @param array $postFiles + * @param string $workingDomain + * @param array $postData + * @param array $postFiles * @return \stdClass * @throws Exceptions\MissingRequiredMIMEParameters */ - public function sendMessage($workingDomain, $postData, $postFiles = array()){ - if(is_array($postFiles)){ + public function sendMessage($workingDomain, $postData, $postFiles = array()) + { + if(is_array($postFiles)) { return $this->post("$workingDomain/messages", $postData, $postFiles); } - else if(is_string($postFiles)){ + else if(is_string($postFiles)) { $tempFile = tempnam(sys_get_temp_dir(), "MG_TMP_MIME"); $fileHandle = fopen($tempFile, "w"); @@ -83,10 +85,11 @@ class Mailgun{ * If this function returns FALSE, you must not process the request. * You should reject the request with status code 403 Forbidden. * - * @param array|null $postData + * @param array|null $postData * @return bool */ - public function verifyWebhookSignature($postData = NULL) { + public function verifyWebhookSignature($postData = null) + { if($postData === null) { $postData = $_POST; } @@ -106,20 +109,22 @@ class Mailgun{ /** * @param string $endpointUrl - * @param array $postData - * @param array $files + * @param array $postData + * @param array $files * @return \stdClass */ - public function post($endpointUrl, $postData = array(), $files = array()){ + public function post($endpointUrl, $postData = array(), $files = array()) + { return $this->restClient->post($endpointUrl, $postData, $files); } /** * @param string $endpointUrl - * @param array $queryString + * @param array $queryString * @return \stdClass */ - public function get($endpointUrl, $queryString = array()){ + public function get($endpointUrl, $queryString = array()) + { return $this->restClient->get($endpointUrl, $queryString); } @@ -127,16 +132,18 @@ class Mailgun{ * @param string $endpointUrl * @return \stdClass */ - public function delete($endpointUrl){ + public function delete($endpointUrl) + { return $this->restClient->delete($endpointUrl); } /** * @param string $endpointUrl - * @param array $putData + * @param array $putData * @return \stdClass */ - public function put($endpointUrl, $putData){ + public function put($endpointUrl, $putData) + { return $this->restClient->put($endpointUrl, $putData); } @@ -167,23 +174,26 @@ class Mailgun{ /** * @return MessageBuilder */ - public function MessageBuilder(){ + public function MessageBuilder() + { return new MessageBuilder(); } /** * @return OptInHandler */ - public function OptInHandler(){ + public function OptInHandler() + { return new OptInHandler(); } /** * @param string $workingDomain - * @param bool $autoSend + * @param bool $autoSend * @return BatchMessage */ - public function BatchMessage($workingDomain, $autoSend = true){ + public function BatchMessage($workingDomain, $autoSend = true) + { return new BatchMessage($this->restClient, $workingDomain, $autoSend); } } diff --git a/src/Mailgun/Messages/BatchMessage.php b/src/Mailgun/Messages/BatchMessage.php index 36cdff7..53d7d97 100644 --- a/src/Mailgun/Messages/BatchMessage.php +++ b/src/Mailgun/Messages/BatchMessage.php @@ -13,134 +13,140 @@ use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters; * * @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md */ -class BatchMessage extends MessageBuilder{ +class BatchMessage extends MessageBuilder +{ /** * @var array */ - private $batchRecipientAttributes; + private $batchRecipientAttributes; /** * @var boolean */ - private $autoSend; + private $autoSend; /** * @var \Mailgun\Connection\RestClient */ - private $restClient; + private $restClient; /** * @var string */ - private $workingDomain; + private $workingDomain; /** * @var array */ - private $messageIds = array(); - + private $messageIds = array(); + /** * @var string */ - private $endpointUrl; + private $endpointUrl; /** * @param \Mailgun\Connection\RestClient $restClient - * @param string $workingDomain - * @param boolean $autoSend + * @param string $workingDomain + * @param boolean $autoSend */ - public function __construct($restClient, $workingDomain, $autoSend){ - $this->batchRecipientAttributes = array(); - $this->autoSend = $autoSend; - $this->restClient = $restClient; - $this->workingDomain = $workingDomain; - $this->endpointUrl = $workingDomain . "/messages"; - } + public function __construct($restClient, $workingDomain, $autoSend) + { + $this->batchRecipientAttributes = array(); + $this->autoSend = $autoSend; + $this->restClient = $restClient; + $this->workingDomain = $workingDomain; + $this->endpointUrl = $workingDomain . "/messages"; + } /** * @param string $headerName * @param string $address - * @param array $variables + * @param array $variables * @throws MissingRequiredMIMEParameters * @throws TooManyParameters */ - protected function addRecipient($headerName, $address, $variables){ - if(array_key_exists($headerName, $this->counters['recipients'])){ - if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT){ - if($this->autoSend == false){ - throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS); - } - $this->sendMessage(); - } - } + protected function addRecipient($headerName, $address, $variables) + { + if(array_key_exists($headerName, $this->counters['recipients'])) { + if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) { + if($this->autoSend == false) { + throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS); + } + $this->sendMessage(); + } + } - $compiledAddress = $this->parseAddress($address, $variables); + $compiledAddress = $this->parseAddress($address, $variables); - if(isset($this->message[$headerName])){ - array_push($this->message[$headerName], $compiledAddress); - } - elseif($headerName == "h:reply-to"){ - $this->message[$headerName] = $compiledAddress; - } - else{ - $this->message[$headerName] = array($compiledAddress); - } + if(isset($this->message[$headerName])) { + array_push($this->message[$headerName], $compiledAddress); + } + elseif($headerName == "h:reply-to") { + $this->message[$headerName] = $compiledAddress; + } + else{ + $this->message[$headerName] = array($compiledAddress); + } - if(array_key_exists($headerName, $this->counters['recipients'])){ - $this->counters['recipients'][$headerName] += 1; - if(!array_key_exists("id", $variables)){ - $variables['id'] = $this->counters['recipients'][$headerName]; - } - } - $this->batchRecipientAttributes["$address"] = $variables; - } + if(array_key_exists($headerName, $this->counters['recipients'])) { + $this->counters['recipients'][$headerName] += 1; + if(!array_key_exists("id", $variables)) { + $variables['id'] = $this->counters['recipients'][$headerName]; + } + } + $this->batchRecipientAttributes["$address"] = $variables; + } /** * @param array $message * @param array $files * @throws MissingRequiredMIMEParameters */ - public function sendMessage($message = array(), $files = array()){ - if(count($message) < 1){ - $message = $this->message; - $files = $this->files; - } - if(!array_key_exists("from", $message)){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif(!array_key_exists("to", $message)){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif(!array_key_exists("subject", $message)){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){ - throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - else{ - $message["recipient-variables"] = json_encode($this->batchRecipientAttributes); - $response = $this->restClient->post($this->endpointUrl, $message, $files); - $this->batchRecipientAttributes = array(); - $this->counters['recipients']['to'] = 0; - $this->counters['recipients']['cc'] = 0; - $this->counters['recipients']['bcc'] = 0; - unset($this->message["to"]); - array_push($this->messageIds, $response->http_response_body->id); - } - } + public function sendMessage($message = array(), $files = array()) + { + if(count($message) < 1) { + $message = $this->message; + $files = $this->files; + } + if(!array_key_exists("from", $message)) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } + elseif(!array_key_exists("to", $message)) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } + elseif(!array_key_exists("subject", $message)) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } + elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))) { + throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); + } + else{ + $message["recipient-variables"] = json_encode($this->batchRecipientAttributes); + $response = $this->restClient->post($this->endpointUrl, $message, $files); + $this->batchRecipientAttributes = array(); + $this->counters['recipients']['to'] = 0; + $this->counters['recipients']['cc'] = 0; + $this->counters['recipients']['bcc'] = 0; + unset($this->message["to"]); + array_push($this->messageIds, $response->http_response_body->id); + } + } /** * @throws MissingRequiredMIMEParameters */ - public function finalize(){ - $this->sendMessage(); - } + public function finalize() + { + $this->sendMessage(); + } /** * @return string[] */ - public function getMessageIds(){ - return $this->messageIds; - } + public function getMessageIds() + { + return $this->messageIds; + } } diff --git a/src/Mailgun/Messages/Exceptions/InvalidParameter.php b/src/Mailgun/Messages/Exceptions/InvalidParameter.php index 9ea1dfc..33c9070 100644 --- a/src/Mailgun/Messages/Exceptions/InvalidParameter.php +++ b/src/Mailgun/Messages/Exceptions/InvalidParameter.php @@ -1,4 +1,6 @@ Date: Sun, 24 Jul 2016 12:41:21 +0100 Subject: [PATCH 2/3] PHP CS Fixer --- .../Exceptions/GenericHTTPError.php | 17 +-- .../Exceptions/InvalidCredentials.php | 1 + .../Connection/Exceptions/MissingEndpoint.php | 1 + .../Exceptions/MissingRequiredParameters.php | 1 + .../Exceptions/NoDomainsConfigured.php | 1 + src/Mailgun/Connection/RestClient.php | 21 ++- src/Mailgun/Constants/Api.php | 11 +- src/Mailgun/Constants/ExceptionMessages.php | 15 +- src/Mailgun/Lists/OptInHandler.php | 13 +- src/Mailgun/Mailgun.php | 50 +++--- src/Mailgun/Messages/BatchMessage.php | 51 +++---- .../Messages/Exceptions/InvalidParameter.php | 1 + .../Exceptions/InvalidParameterType.php | 1 + .../MissingRequiredMIMEParameters.php | 1 + .../Messages/Exceptions/TooManyParameters.php | 1 + src/Mailgun/Messages/MessageBuilder.php | 142 +++++++++++------- 16 files changed, 174 insertions(+), 154 deletions(-) diff --git a/src/Mailgun/Connection/Exceptions/GenericHTTPError.php b/src/Mailgun/Connection/Exceptions/GenericHTTPError.php index 35439d8..20ebd96 100644 --- a/src/Mailgun/Connection/Exceptions/GenericHTTPError.php +++ b/src/Mailgun/Connection/Exceptions/GenericHTTPError.php @@ -1,28 +1,27 @@ httpResponseCode = $response_code; $this->httpResponseBody = $response_body; } - - public function getHttpResponseCode() + + public function getHttpResponseCode() { return $this->httpResponseCode; } - - public function getHttpResponseBody() + + public function getHttpResponseBody() { return $this->httpResponseBody; } } - -?> diff --git a/src/Mailgun/Connection/Exceptions/InvalidCredentials.php b/src/Mailgun/Connection/Exceptions/InvalidCredentials.php index b890ca7..d7159d5 100644 --- a/src/Mailgun/Connection/Exceptions/InvalidCredentials.php +++ b/src/Mailgun/Connection/Exceptions/InvalidCredentials.php @@ -1,4 +1,5 @@ getStatusCode(); + $httpResponseCode = (int) $responseObj->getStatusCode(); switch ($httpResponseCode) { case 200: - $data = (string)$responseObj->getBody(); + $data = (string) $responseObj->getBody(); $jsonResponseData = json_decode($data, false); $result = new \stdClass(); // return response data as json if possible, raw if not @@ -220,11 +220,11 @@ class RestClient return $result; case 400: - throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj)); + throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS.$this->getResponseExceptionMessage($responseObj)); case 401: throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); case 404: - throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj)); + throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT.$this->getResponseExceptionMessage($responseObj)); default: throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); } @@ -275,9 +275,7 @@ class RestClient ]; } - /** - * * @return HttpClient */ protected function getHttpClient() @@ -299,7 +297,6 @@ class RestClient return $this->generateEndpoint($this->apiHost, $this->apiVersion, $this->sslEnabled).$uri; } - /** * @param string $apiEndpoint * @param string $apiVersion @@ -309,7 +306,7 @@ class RestClient */ private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) { - return ($ssl ? 'https://' : 'http://') . $apiEndpoint . '/' . $apiVersion . '/'; + return ($ssl ? 'https://' : 'http://').$apiEndpoint.'/'.$apiVersion.'/'; } /** @@ -325,7 +322,7 @@ class RestClient } /** - * @param boolean $sslEnabled + * @param bool $sslEnabled * * @return RestClient */ diff --git a/src/Mailgun/Constants/Api.php b/src/Mailgun/Constants/Api.php index 32a1dea..a3ffdfb 100644 --- a/src/Mailgun/Constants/Api.php +++ b/src/Mailgun/Constants/Api.php @@ -1,17 +1,14 @@ $recipientAddress, 'l' => $mailingList); $encodedInnerPayload = base64_encode(json_encode($innerPayload)); - $innerHash = hash_hmac("sha1", $encodedInnerPayload, $secretAppId); + $innerHash = hash_hmac('sha1', $encodedInnerPayload, $secretAppId); $outerPayload = array('h' => $innerHash, 'p' => $encodedInnerPayload); return urlencode(base64_encode(json_encode($outerPayload))); @@ -35,6 +31,7 @@ class OptInHandler /** * @param string $secretAppId * @param string $uniqueHash + * * @return array|bool */ public function validateHash($secretAppId, $uniqueHash) @@ -45,9 +42,9 @@ class OptInHandler $innerPayload = $decodedOuterPayload['p']; $decodedInnerPayload = json_decode(base64_decode($innerPayload), true); - $computedInnerHash = hash_hmac("sha1", $innerPayload, $secretAppId); + $computedInnerHash = hash_hmac('sha1', $innerPayload, $secretAppId); - if($computedInnerHash == $decodedHash) { + if ($computedInnerHash == $decodedHash) { return array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']); } diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index f6478f0..6fed34e 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -18,7 +18,6 @@ use Mailgun\Messages\MessageBuilder; */ class Mailgun { - /** * @var RestClient */ @@ -48,29 +47,29 @@ class Mailgun * MIME string. If sending MIME, the string must be passed in to the 3rd * position of the function call. * - * @param string $workingDomain - * @param array $postData - * @param array $postFiles + * @param string $workingDomain + * @param array $postData + * @param array $postFiles + * * @return \stdClass + * * @throws Exceptions\MissingRequiredMIMEParameters */ public function sendMessage($workingDomain, $postData, $postFiles = array()) { - if(is_array($postFiles)) { + if (is_array($postFiles)) { return $this->post("$workingDomain/messages", $postData, $postFiles); - } - else if(is_string($postFiles)) { - - $tempFile = tempnam(sys_get_temp_dir(), "MG_TMP_MIME"); - $fileHandle = fopen($tempFile, "w"); + } elseif (is_string($postFiles)) { + $tempFile = tempnam(sys_get_temp_dir(), 'MG_TMP_MIME'); + $fileHandle = fopen($tempFile, 'w'); fwrite($fileHandle, $postFiles); - $result = $this->post("$workingDomain/messages.mime", $postData, array("message" => $tempFile)); + $result = $this->post("$workingDomain/messages.mime", $postData, array('message' => $tempFile)); fclose($fileHandle); unlink($tempFile); + return $result; - } - else{ + } else { throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); } } @@ -85,25 +84,25 @@ class Mailgun * If this function returns FALSE, you must not process the request. * You should reject the request with status code 403 Forbidden. * - * @param array|null $postData + * @param array|null $postData + * * @return bool */ - public function verifyWebhookSignature($postData = null) + public function verifyWebhookSignature($postData = null) { - if($postData === null) { + if ($postData === null) { $postData = $_POST; } - if(!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) { + if (!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) { return false; } - $hmac = hash_hmac('sha256', "{$postData["timestamp"]}{$postData["token"]}", $this->apiKey); + $hmac = hash_hmac('sha256', "{$postData['timestamp']}{$postData['token']}", $this->apiKey); $sig = $postData['signature']; - if(function_exists('hash_equals')) { + if (function_exists('hash_equals')) { // hash_equals is constant time, but will not be introduced until PHP 5.6 return hash_equals($hmac, $sig); - } - else { - return ($hmac === $sig); + } else { + return $hmac === $sig; } } @@ -111,6 +110,7 @@ class Mailgun * @param string $endpointUrl * @param array $postData * @param array $files + * * @return \stdClass */ public function post($endpointUrl, $postData = array(), $files = array()) @@ -121,6 +121,7 @@ class Mailgun /** * @param string $endpointUrl * @param array $queryString + * * @return \stdClass */ public function get($endpointUrl, $queryString = array()) @@ -130,6 +131,7 @@ class Mailgun /** * @param string $endpointUrl + * * @return \stdClass */ public function delete($endpointUrl) @@ -140,6 +142,7 @@ class Mailgun /** * @param string $endpointUrl * @param array $putData + * * @return \stdClass */ public function put($endpointUrl, $putData) @@ -160,7 +163,7 @@ class Mailgun } /** - * @param boolean $sslEnabled + * @param bool $sslEnabled * * @return Mailgun */ @@ -190,6 +193,7 @@ class Mailgun /** * @param string $workingDomain * @param bool $autoSend + * * @return BatchMessage */ public function BatchMessage($workingDomain, $autoSend = true) diff --git a/src/Mailgun/Messages/BatchMessage.php b/src/Mailgun/Messages/BatchMessage.php index 53d7d97..9bfbb22 100644 --- a/src/Mailgun/Messages/BatchMessage.php +++ b/src/Mailgun/Messages/BatchMessage.php @@ -15,14 +15,13 @@ use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters; */ class BatchMessage extends MessageBuilder { - /** * @var array */ private $batchRecipientAttributes; /** - * @var boolean + * @var bool */ private $autoSend; @@ -40,16 +39,16 @@ class BatchMessage extends MessageBuilder * @var array */ private $messageIds = array(); - + /** - * @var string + * @var string */ private $endpointUrl; /** * @param \Mailgun\Connection\RestClient $restClient * @param string $workingDomain - * @param boolean $autoSend + * @param bool $autoSend */ public function __construct($restClient, $workingDomain, $autoSend) { @@ -57,21 +56,22 @@ class BatchMessage extends MessageBuilder $this->autoSend = $autoSend; $this->restClient = $restClient; $this->workingDomain = $workingDomain; - $this->endpointUrl = $workingDomain . "/messages"; + $this->endpointUrl = $workingDomain.'/messages'; } /** * @param string $headerName * @param string $address * @param array $variables + * * @throws MissingRequiredMIMEParameters * @throws TooManyParameters */ protected function addRecipient($headerName, $address, $variables) { - if(array_key_exists($headerName, $this->counters['recipients'])) { - if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) { - if($this->autoSend == false) { + if (array_key_exists($headerName, $this->counters['recipients'])) { + if ($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) { + if ($this->autoSend == false) { throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS); } $this->sendMessage(); @@ -80,19 +80,17 @@ class BatchMessage extends MessageBuilder $compiledAddress = $this->parseAddress($address, $variables); - if(isset($this->message[$headerName])) { + if (isset($this->message[$headerName])) { array_push($this->message[$headerName], $compiledAddress); - } - elseif($headerName == "h:reply-to") { + } elseif ($headerName == 'h:reply-to') { $this->message[$headerName] = $compiledAddress; - } - else{ + } else { $this->message[$headerName] = array($compiledAddress); } - if(array_key_exists($headerName, $this->counters['recipients'])) { + if (array_key_exists($headerName, $this->counters['recipients'])) { $this->counters['recipients'][$headerName] += 1; - if(!array_key_exists("id", $variables)) { + if (!array_key_exists('id', $variables)) { $variables['id'] = $this->counters['recipients'][$headerName]; } } @@ -102,34 +100,31 @@ class BatchMessage extends MessageBuilder /** * @param array $message * @param array $files + * * @throws MissingRequiredMIMEParameters */ public function sendMessage($message = array(), $files = array()) { - if(count($message) < 1) { + if (count($message) < 1) { $message = $this->message; $files = $this->files; } - if(!array_key_exists("from", $message)) { + if (!array_key_exists('from', $message)) { throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif(!array_key_exists("to", $message)) { + } elseif (!array_key_exists('to', $message)) { throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif(!array_key_exists("subject", $message)) { + } elseif (!array_key_exists('subject', $message)) { throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))) { + } elseif ((!array_key_exists('text', $message) && !array_key_exists('html', $message))) { throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); - } - else{ - $message["recipient-variables"] = json_encode($this->batchRecipientAttributes); + } else { + $message['recipient-variables'] = json_encode($this->batchRecipientAttributes); $response = $this->restClient->post($this->endpointUrl, $message, $files); $this->batchRecipientAttributes = array(); $this->counters['recipients']['to'] = 0; $this->counters['recipients']['cc'] = 0; $this->counters['recipients']['bcc'] = 0; - unset($this->message["to"]); + unset($this->message['to']); array_push($this->messageIds, $response->http_response_body->id); } } diff --git a/src/Mailgun/Messages/Exceptions/InvalidParameter.php b/src/Mailgun/Messages/Exceptions/InvalidParameter.php index 33c9070..06c8612 100644 --- a/src/Mailgun/Messages/Exceptions/InvalidParameter.php +++ b/src/Mailgun/Messages/Exceptions/InvalidParameter.php @@ -1,4 +1,5 @@ array( - 'to' => 0, - 'cc' => 0, - 'bcc' => 0 + 'to' => 0, + 'cc' => 0, + 'bcc' => 0, ), 'attributes' => array( - 'attachment' => 0, - 'campaign_id' => 0, + 'attachment' => 0, + 'campaign_id' => 0, 'custom_option' => 0, - 'tag' => 0 - ) + 'tag' => 0, + ), ); /** * @param array $params * @param string $key * @param mixed $default + * * @return mixed */ protected function safeGet($params, $key, $default) @@ -66,23 +67,25 @@ class MessageBuilder /** * @param array $params + * * @return mixed|string */ protected function getFullName($params) { - if (array_key_exists("first", $params)) { - $first = $this->safeGet($params, "first", ""); - $last = $this->safeGet($params, "last", ""); + if (array_key_exists('first', $params)) { + $first = $this->safeGet($params, 'first', ''); + $last = $this->safeGet($params, 'last', ''); return trim("$first $last"); } - return $this->safeGet($params, "full_name", ""); + return $this->safeGet($params, 'full_name', ''); } /** * @param string $address * @param array $variables + * * @return string */ protected function parseAddress($address, $variables) @@ -109,7 +112,7 @@ class MessageBuilder if (isset($this->message[$headerName])) { array_push($this->message[$headerName], $compiledAddress); - } elseif ($headerName == "h:reply-to") { + } elseif ($headerName == 'h:reply-to') { $this->message[$headerName] = $compiledAddress; } else { $this->message[$headerName] = array($compiledAddress); @@ -122,7 +125,9 @@ class MessageBuilder /** * @param string $address * @param array|null $variables + * * @return mixed + * * @throws TooManyParameters */ public function addToRecipient($address, $variables = null) @@ -130,7 +135,7 @@ class MessageBuilder if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) { throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); } - $this->addRecipient("to", $address, $variables); + $this->addRecipient('to', $address, $variables); return end($this->message['to']); } @@ -138,7 +143,9 @@ class MessageBuilder /** * @param string $address * @param array|null $variables + * * @return mixed + * * @throws TooManyParameters */ public function addCcRecipient($address, $variables = null) @@ -146,7 +153,7 @@ class MessageBuilder if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) { throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); } - $this->addRecipient("cc", $address, $variables); + $this->addRecipient('cc', $address, $variables); return end($this->message['cc']); } @@ -154,7 +161,9 @@ class MessageBuilder /** * @param string $address * @param array|null $variables + * * @return mixed + * * @throws TooManyParameters */ public function addBccRecipient($address, $variables = null) @@ -162,7 +171,7 @@ class MessageBuilder if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) { throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); } - $this->addRecipient("bcc", $address, $variables); + $this->addRecipient('bcc', $address, $variables); return end($this->message['bcc']); } @@ -170,11 +179,12 @@ class MessageBuilder /** * @param string $address * @param array|null $variables + * * @return mixed */ public function setFromAddress($address, $variables = null) { - $this->addRecipient("from", $address, $variables); + $this->addRecipient('from', $address, $variables); return $this->message['from']; } @@ -182,23 +192,25 @@ class MessageBuilder /** * @param string $address * @param array|null $variables + * * @return mixed */ public function setReplyToAddress($address, $variables = null) { - $this->addRecipient("h:reply-to", $address, $variables); + $this->addRecipient('h:reply-to', $address, $variables); return $this->message['h:reply-to']; } /** * @param string $subject + * * @return mixed */ - public function setSubject($subject = "") + public function setSubject($subject = '') { - if ($subject == null || $subject == "") { - $subject = " "; + if ($subject == null || $subject == '') { + $subject = ' '; } $this->message['subject'] = $subject; @@ -208,12 +220,13 @@ class MessageBuilder /** * @param string $headerName * @param mixed $headerData + * * @return mixed */ public function addCustomHeader($headerName, $headerData) { - if (!preg_match("/^h:/i", $headerName)) { - $headerName = "h:" . $headerName; + if (!preg_match('/^h:/i', $headerName)) { + $headerName = 'h:'.$headerName; } $this->message[$headerName] = array($headerData); @@ -222,12 +235,13 @@ class MessageBuilder /** * @param string $textBody + * * @return string */ public function setTextBody($textBody) { - if ($textBody == null || $textBody == "") { - $textBody = " "; + if ($textBody == null || $textBody == '') { + $textBody = ' '; } $this->message['text'] = $textBody; @@ -236,12 +250,13 @@ class MessageBuilder /** * @param string $htmlBody + * * @return string */ public function setHtmlBody($htmlBody) { - if ($htmlBody == null || $htmlBody == "") { - $htmlBody = " "; + if ($htmlBody == null || $htmlBody == '') { + $htmlBody = ' '; } $this->message['html'] = $htmlBody; @@ -251,22 +266,23 @@ class MessageBuilder /** * @param string $attachmentPath * @param string|null $attachmentName + * * @return bool */ public function addAttachment($attachmentPath, $attachmentName = null) { - if (isset($this->files["attachment"])) { + if (isset($this->files['attachment'])) { $attachment = array( - 'filePath' => $attachmentPath, - 'remoteName' => $attachmentName + 'filePath' => $attachmentPath, + 'remoteName' => $attachmentName, ); - array_push($this->files["attachment"], $attachment); + array_push($this->files['attachment'], $attachment); } else { - $this->files["attachment"] = array( + $this->files['attachment'] = array( array( - 'filePath' => $attachmentPath, - 'remoteName' => $attachmentName - ) + 'filePath' => $attachmentPath, + 'remoteName' => $attachmentName, + ), ); } @@ -278,6 +294,7 @@ class MessageBuilder * @param string|null $inlineImageName * * @return bool|true + * * @throws InvalidParameter */ public function addInlineImage($inlineImagePath, $inlineImageName = null) @@ -285,16 +302,16 @@ class MessageBuilder if (strpos($inlineImagePath, '@') === 0) { if (isset($this->files['inline'])) { $inlineAttachment = array( - 'filePath' => $inlineImagePath, - 'remoteName' => $inlineImageName + 'filePath' => $inlineImagePath, + 'remoteName' => $inlineImageName, ); array_push($this->files['inline'], $inlineAttachment); } else { $this->files['inline'] = array( array( - 'filePath' => $inlineImagePath, - 'remoteName' => $inlineImageName - ) + 'filePath' => $inlineImagePath, + 'remoteName' => $inlineImageName, + ), ); } @@ -305,15 +322,16 @@ class MessageBuilder } /** - * @param boolean $testMode + * @param bool $testMode + * * @return string */ public function setTestMode($testMode) { if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) { - $testMode = "yes"; + $testMode = 'yes'; } else { - $testMode = "no"; + $testMode = 'no'; } $this->message['o:testmode'] = $testMode; @@ -322,7 +340,9 @@ class MessageBuilder /** * @param string|int $campaignId + * * @return string|int + * * @throws TooManyParameters */ public function addCampaignId($campaignId) @@ -343,6 +363,7 @@ class MessageBuilder /** * @param string $tag + * * @throws TooManyParameters */ public function addTag($tag) @@ -362,31 +383,33 @@ class MessageBuilder } /** - * @param boolean $enabled + * @param bool $enabled + * * @return mixed */ public function setDkim($enabled) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { - $enabled = "yes"; + $enabled = 'yes'; } else { - $enabled = "no"; + $enabled = 'no'; } - $this->message["o:dkim"] = $enabled; + $this->message['o:dkim'] = $enabled; - return $this->message["o:dkim"]; + return $this->message['o:dkim']; } /** - * @param boolean $enabled + * @param bool $enabled + * * @return string */ public function setOpenTracking($enabled) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { - $enabled = "yes"; + $enabled = 'yes'; } else { - $enabled = "no"; + $enabled = 'no'; } $this->message['o:tracking-opens'] = $enabled; @@ -394,17 +417,18 @@ class MessageBuilder } /** - * @param boolean $enabled + * @param bool $enabled + * * @return string */ public function setClickTracking($enabled) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { - $enabled = "yes"; - } elseif ($enabled == "html") { - $enabled = "html"; + $enabled = 'yes'; + } elseif ($enabled == 'html') { + $enabled = 'html'; } else { - $enabled = "no"; + $enabled = 'no'; } $this->message['o:tracking-clicks'] = $enabled; @@ -414,6 +438,7 @@ class MessageBuilder /** * @param string $timeDate * @param string|null $timeZone + * * @return string */ public function setDeliveryTime($timeDate, $timeZone = null) @@ -424,8 +449,8 @@ class MessageBuilder $timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE); } - $dateTimeObj = new \DateTime($timeDate, $timeZoneObj); - $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); + $dateTimeObj = new \DateTime($timeDate, $timeZoneObj); + $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); $this->message['o:deliverytime'] = $formattedTimeDate; return $this->message['o:deliverytime']; @@ -437,12 +462,13 @@ class MessageBuilder */ public function addCustomData($customName, $data) { - $this->message['v:' . $customName] = json_encode($data); + $this->message['v:'.$customName] = json_encode($data); } /** * @param string $parameterName * @param mixed $data + * * @return mixed */ public function addCustomParameter($parameterName, $data) From 817dad1f900798f35393c0f09d4c6b2b831cddb2 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Sun, 24 Jul 2016 07:42:47 -0400 Subject: [PATCH 3/3] Applied fixes from StyleCI --- src/Mailgun/Connection/RestClient.php | 36 +-- src/Mailgun/Lists/OptInHandler.php | 6 +- src/Mailgun/Mailgun.php | 18 +- src/Mailgun/Messages/BatchMessage.php | 12 +- src/Mailgun/Messages/MessageBuilder.php | 90 +++---- tests/Bootstrap.php | 3 +- .../Mailgun/Tests/Lists/OptInHandlerTest.php | 10 +- tests/Mailgun/Tests/MailgunTest.php | 29 +-- .../Tests/Messages/BatchMessageTest.php | 94 ++++---- .../Tests/Messages/MessageBuilderTest.php | 221 +++++++++--------- .../Tests/Messages/StandardMessageTest.php | 29 ++- .../Tests/Mock/Connection/TestBroker.php | 17 +- tests/Mailgun/Tests/Mock/Mailgun.php | 2 +- 13 files changed, 284 insertions(+), 283 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index a10cbe7..c93d663 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -8,8 +8,8 @@ use Http\Discovery\MessageFactoryDiscovery; use Http\Message\MultipartStream\MultipartStreamBuilder; use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\InvalidCredentials; -use Mailgun\Connection\Exceptions\MissingRequiredParameters; use Mailgun\Connection\Exceptions\MissingEndpoint; +use Mailgun\Connection\Exceptions\MissingRequiredParameters; use Mailgun\Constants\Api; use Mailgun\Constants\ExceptionMessages; use Psr\Http\Message\ResponseInterface; @@ -69,12 +69,12 @@ class RestClient * @param array $files * @param array $headers * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ protected function send($method, $uri, $body = null, $files = [], array $headers = []) { @@ -104,14 +104,14 @@ class RestClient * @param array $postData * @param array $files * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ - public function post($endpointUrl, array $postData = array(), $files = array()) + public function post($endpointUrl, array $postData = [], $files = []) { $postFiles = []; @@ -133,13 +133,13 @@ class RestClient if (is_array($value)) { foreach ($value as $subValue) { $postDataMultipart[] = [ - 'name' => $key, + 'name' => $key, 'contents' => $subValue, ]; } } else { $postDataMultipart[] = [ - 'name' => $key, + 'name' => $key, 'contents' => $value, ]; } @@ -152,14 +152,14 @@ class RestClient * @param string $endpointUrl * @param array $queryString * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ - public function get($endpointUrl, $queryString = array()) + public function get($endpointUrl, $queryString = []) { return $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); } @@ -167,12 +167,12 @@ class RestClient /** * @param string $endpointUrl * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ public function delete($endpointUrl) { @@ -183,12 +183,12 @@ class RestClient * @param string $endpointUrl * @param mixed $putData * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ public function put($endpointUrl, $putData) { @@ -198,12 +198,12 @@ class RestClient /** * @param ResponseInterface $responseObj * - * @return \stdClass - * * @throws GenericHTTPError * @throws InvalidCredentials * @throws MissingEndpoint * @throws MissingRequiredParameters + * + * @return \stdClass */ public function responseHandler(ResponseInterface $responseObj) { @@ -269,7 +269,7 @@ class RestClient } return [ - 'name' => $fieldName, + 'name' => $fieldName, 'contents' => fopen($filePath, 'r'), 'filename' => $filename, ]; diff --git a/src/Mailgun/Lists/OptInHandler.php b/src/Mailgun/Lists/OptInHandler.php index 4aeccdd..cc2ef2d 100644 --- a/src/Mailgun/Lists/OptInHandler.php +++ b/src/Mailgun/Lists/OptInHandler.php @@ -19,11 +19,11 @@ class OptInHandler */ public function generateHash($mailingList, $secretAppId, $recipientAddress) { - $innerPayload = array('r' => $recipientAddress, 'l' => $mailingList); + $innerPayload = ['r' => $recipientAddress, 'l' => $mailingList]; $encodedInnerPayload = base64_encode(json_encode($innerPayload)); $innerHash = hash_hmac('sha1', $encodedInnerPayload, $secretAppId); - $outerPayload = array('h' => $innerHash, 'p' => $encodedInnerPayload); + $outerPayload = ['h' => $innerHash, 'p' => $encodedInnerPayload]; return urlencode(base64_encode(json_encode($outerPayload))); } @@ -45,7 +45,7 @@ class OptInHandler $computedInnerHash = hash_hmac('sha1', $innerPayload, $secretAppId); if ($computedInnerHash == $decodedHash) { - return array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']); + return ['recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']]; } return false; diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 6fed34e..a0e3ead 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -3,11 +3,11 @@ namespace Mailgun; use Http\Client\HttpClient; -use Mailgun\Constants\ExceptionMessages; -use Mailgun\Messages\Exceptions; use Mailgun\Connection\RestClient; -use Mailgun\Messages\BatchMessage; +use Mailgun\Constants\ExceptionMessages; use Mailgun\Lists\OptInHandler; +use Mailgun\Messages\BatchMessage; +use Mailgun\Messages\Exceptions; use Mailgun\Messages\MessageBuilder; /** @@ -51,11 +51,11 @@ class Mailgun * @param array $postData * @param array $postFiles * - * @return \stdClass - * * @throws Exceptions\MissingRequiredMIMEParameters + * + * @return \stdClass */ - public function sendMessage($workingDomain, $postData, $postFiles = array()) + public function sendMessage($workingDomain, $postData, $postFiles = []) { if (is_array($postFiles)) { return $this->post("$workingDomain/messages", $postData, $postFiles); @@ -64,7 +64,7 @@ class Mailgun $fileHandle = fopen($tempFile, 'w'); fwrite($fileHandle, $postFiles); - $result = $this->post("$workingDomain/messages.mime", $postData, array('message' => $tempFile)); + $result = $this->post("$workingDomain/messages.mime", $postData, ['message' => $tempFile]); fclose($fileHandle); unlink($tempFile); @@ -113,7 +113,7 @@ class Mailgun * * @return \stdClass */ - public function post($endpointUrl, $postData = array(), $files = array()) + public function post($endpointUrl, $postData = [], $files = []) { return $this->restClient->post($endpointUrl, $postData, $files); } @@ -124,7 +124,7 @@ class Mailgun * * @return \stdClass */ - public function get($endpointUrl, $queryString = array()) + public function get($endpointUrl, $queryString = []) { return $this->restClient->get($endpointUrl, $queryString); } diff --git a/src/Mailgun/Messages/BatchMessage.php b/src/Mailgun/Messages/BatchMessage.php index 9bfbb22..7085400 100644 --- a/src/Mailgun/Messages/BatchMessage.php +++ b/src/Mailgun/Messages/BatchMessage.php @@ -4,8 +4,8 @@ namespace Mailgun\Messages; use Mailgun\Constants\Api; use Mailgun\Constants\ExceptionMessages; -use Mailgun\Messages\Exceptions\TooManyParameters; use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters; +use Mailgun\Messages\Exceptions\TooManyParameters; /** * This class is used for batch sending. See the official documentation (link below) @@ -38,7 +38,7 @@ class BatchMessage extends MessageBuilder /** * @var array */ - private $messageIds = array(); + private $messageIds = []; /** * @var string @@ -52,7 +52,7 @@ class BatchMessage extends MessageBuilder */ public function __construct($restClient, $workingDomain, $autoSend) { - $this->batchRecipientAttributes = array(); + $this->batchRecipientAttributes = []; $this->autoSend = $autoSend; $this->restClient = $restClient; $this->workingDomain = $workingDomain; @@ -85,7 +85,7 @@ class BatchMessage extends MessageBuilder } elseif ($headerName == 'h:reply-to') { $this->message[$headerName] = $compiledAddress; } else { - $this->message[$headerName] = array($compiledAddress); + $this->message[$headerName] = [$compiledAddress]; } if (array_key_exists($headerName, $this->counters['recipients'])) { @@ -103,7 +103,7 @@ class BatchMessage extends MessageBuilder * * @throws MissingRequiredMIMEParameters */ - public function sendMessage($message = array(), $files = array()) + public function sendMessage($message = [], $files = []) { if (count($message) < 1) { $message = $this->message; @@ -120,7 +120,7 @@ class BatchMessage extends MessageBuilder } else { $message['recipient-variables'] = json_encode($this->batchRecipientAttributes); $response = $this->restClient->post($this->endpointUrl, $message, $files); - $this->batchRecipientAttributes = array(); + $this->batchRecipientAttributes = []; $this->counters['recipients']['to'] = 0; $this->counters['recipients']['cc'] = 0; $this->counters['recipients']['bcc'] = 0; diff --git a/src/Mailgun/Messages/MessageBuilder.php b/src/Mailgun/Messages/MessageBuilder.php index b348cf0..b7f243f 100644 --- a/src/Mailgun/Messages/MessageBuilder.php +++ b/src/Mailgun/Messages/MessageBuilder.php @@ -20,34 +20,34 @@ class MessageBuilder /** * @var array */ - protected $message = array(); + protected $message = []; /** * @var array */ - protected $variables = array(); + protected $variables = []; /** * @var array */ - protected $files = array(); + protected $files = []; /** * @var array */ - protected $counters = array( - 'recipients' => array( - 'to' => 0, - 'cc' => 0, + protected $counters = [ + 'recipients' => [ + 'to' => 0, + 'cc' => 0, 'bcc' => 0, - ), - 'attributes' => array( - 'attachment' => 0, - 'campaign_id' => 0, + ], + 'attributes' => [ + 'attachment' => 0, + 'campaign_id' => 0, 'custom_option' => 0, - 'tag' => 0, - ), - ); + 'tag' => 0, + ], + ]; /** * @param array $params @@ -115,7 +115,7 @@ class MessageBuilder } elseif ($headerName == 'h:reply-to') { $this->message[$headerName] = $compiledAddress; } else { - $this->message[$headerName] = array($compiledAddress); + $this->message[$headerName] = [$compiledAddress]; } if (array_key_exists($headerName, $this->counters['recipients'])) { $this->counters['recipients'][$headerName] += 1; @@ -126,9 +126,9 @@ class MessageBuilder * @param string $address * @param array|null $variables * - * @return mixed - * * @throws TooManyParameters + * + * @return mixed */ public function addToRecipient($address, $variables = null) { @@ -144,9 +144,9 @@ class MessageBuilder * @param string $address * @param array|null $variables * - * @return mixed - * * @throws TooManyParameters + * + * @return mixed */ public function addCcRecipient($address, $variables = null) { @@ -162,9 +162,9 @@ class MessageBuilder * @param string $address * @param array|null $variables * - * @return mixed - * * @throws TooManyParameters + * + * @return mixed */ public function addBccRecipient($address, $variables = null) { @@ -228,7 +228,7 @@ class MessageBuilder if (!preg_match('/^h:/i', $headerName)) { $headerName = 'h:'.$headerName; } - $this->message[$headerName] = array($headerData); + $this->message[$headerName] = [$headerData]; return $this->message[$headerName]; } @@ -272,18 +272,18 @@ class MessageBuilder public function addAttachment($attachmentPath, $attachmentName = null) { if (isset($this->files['attachment'])) { - $attachment = array( - 'filePath' => $attachmentPath, + $attachment = [ + 'filePath' => $attachmentPath, 'remoteName' => $attachmentName, - ); + ]; array_push($this->files['attachment'], $attachment); } else { - $this->files['attachment'] = array( - array( - 'filePath' => $attachmentPath, + $this->files['attachment'] = [ + [ + 'filePath' => $attachmentPath, 'remoteName' => $attachmentName, - ), - ); + ], + ]; } return true; @@ -293,26 +293,26 @@ class MessageBuilder * @param string $inlineImagePath * @param string|null $inlineImageName * - * @return bool|true - * * @throws InvalidParameter + * + * @return bool|true */ public function addInlineImage($inlineImagePath, $inlineImageName = null) { if (strpos($inlineImagePath, '@') === 0) { if (isset($this->files['inline'])) { - $inlineAttachment = array( - 'filePath' => $inlineImagePath, + $inlineAttachment = [ + 'filePath' => $inlineImagePath, 'remoteName' => $inlineImageName, - ); + ]; array_push($this->files['inline'], $inlineAttachment); } else { - $this->files['inline'] = array( - array( - 'filePath' => $inlineImagePath, + $this->files['inline'] = [ + [ + 'filePath' => $inlineImagePath, 'remoteName' => $inlineImageName, - ), - ); + ], + ]; } return true; @@ -341,9 +341,9 @@ class MessageBuilder /** * @param string|int $campaignId * - * @return string|int - * * @throws TooManyParameters + * + * @return string|int */ public function addCampaignId($campaignId) { @@ -351,7 +351,7 @@ class MessageBuilder if (isset($this->message['o:campaign'])) { array_push($this->message['o:campaign'], $campaignId); } else { - $this->message['o:campaign'] = array($campaignId); + $this->message['o:campaign'] = [$campaignId]; } $this->counters['attributes']['campaign_id'] += 1; @@ -372,7 +372,7 @@ class MessageBuilder if (isset($this->message['o:tag'])) { array_push($this->message['o:tag'], $tag); } else { - $this->message['o:tag'] = array($tag); + $this->message['o:tag'] = [$tag]; } $this->counters['attributes']['tag'] += 1; @@ -478,7 +478,7 @@ class MessageBuilder return $this->message[$parameterName]; } else { - $this->message[$parameterName] = array($data); + $this->message[$parameterName] = [$data]; return $this->message[$parameterName]; } diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index da835f1..53a3686 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -1,5 +1,4 @@ client = new Mailgun("My-Super-Awesome-API-Key"); + $this->client = new Mailgun('My-Super-Awesome-API-Key'); $this->optInHandler = $this->client->OptInHandler(); } @@ -23,7 +23,7 @@ class OptInHandler extends \Mailgun\Tests\MailgunTestCase 'mysupersecretappid', 'testrecipient@example.com' ); - $knownHash = "eyJoIjoiMTllODc2YWNkMWRmNzk4NTc0ZTU0YzhjMzIzOTNiYTNjNzdhNGMxOCIsInAiOiJleUp5SWpvaWRHVnpkSEpsWTJsd2FXVnVkRUJsZUdGdGNHeGxMbU52YlNJc0ltd2lPaUp0ZVhSbGMzUnNhWE4wUUdWNFlXMXdiR1V1WTI5dEluMD0ifQ%3D%3D"; + $knownHash = 'eyJoIjoiMTllODc2YWNkMWRmNzk4NTc0ZTU0YzhjMzIzOTNiYTNjNzdhNGMxOCIsInAiOiJleUp5SWpvaWRHVnpkSEpsWTJsd2FXVnVkRUJsZUdGdGNHeGxMbU52YlNJc0ltd2lPaUp0ZVhSbGMzUnNhWE4wUUdWNFlXMXdiR1V1WTI5dEluMD0ifQ%3D%3D'; $this->assertEquals($generatedHash, $knownHash); } diff --git a/tests/Mailgun/Tests/MailgunTest.php b/tests/Mailgun/Tests/MailgunTest.php index 38a6905..204d2f5 100644 --- a/tests/Mailgun/Tests/MailgunTest.php +++ b/tests/Mailgun/Tests/MailgunTest.php @@ -1,42 +1,45 @@ setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $client = new Mailgun(); - $client->sendMessage("test.mailgun.com", "etss", 1); + $client->sendMessage('test.mailgun.com', 'etss', 1); } - public function testVerifyWebhookGood() { + public function testVerifyWebhookGood() + { $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); - $postData = array( + $postData = [ 'timestamp' => '1403645220', - 'token' => '5egbgr1vjgqxtrnp65xfznchgdccwh5d6i09vijqi3whgowmn6', + 'token' => '5egbgr1vjgqxtrnp65xfznchgdccwh5d6i09vijqi3whgowmn6', 'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33', - ); + ]; assert($client->verifyWebhookSignature($postData)); } - public function testVerifyWebhookBad() { + public function testVerifyWebhookBad() + { $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); - $postData = array( + $postData = [ 'timestamp' => '1403645220', - 'token' => 'owyldpe6nxhmrn78epljl6bj0orrki1u3d2v5e6cnlmmuox8jr', + 'token' => 'owyldpe6nxhmrn78epljl6bj0orrki1u3d2v5e6cnlmmuox8jr', 'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33', - ); + ]; assert(!$client->verifyWebhookSignature($postData)); } - public function testVerifyWebhookEmptyRequest() { + public function testVerifyWebhookEmptyRequest() + { $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); - $postData = array(); + $postData = []; assert(!$client->verifyWebhookSignature($postData)); } } diff --git a/tests/Mailgun/Tests/Messages/BatchMessageTest.php b/tests/Mailgun/Tests/Messages/BatchMessageTest.php index 05a39a1..a1072fb 100644 --- a/tests/Mailgun/Tests/Messages/BatchMessageTest.php +++ b/tests/Mailgun/Tests/Messages/BatchMessageTest.php @@ -1,17 +1,17 @@ client = new Mailgun("My-Super-Awesome-API-Key"); + $this->client = new Mailgun('My-Super-Awesome-API-Key'); } public function testBlankInstantiation() @@ -23,12 +23,12 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testAddRecipient() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + $this->assertEquals(['to' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['to']); @@ -37,78 +37,78 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testRecipientVariablesOnTo() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + $this->assertEquals(['to' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); - $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); - $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']); } public function testRecipientVariablesOnCc() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addCcRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("cc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['cc' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); - $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); - $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']); } public function testRecipientVariablesOnBcc() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addBccRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("bcc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['bcc' => ["'Test User' "]], $messageObj); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); - $this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); - $this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); + $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']); + $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']); } public function testAddMultipleBatchRecipients() { $message = $this->client->BatchMessage($this->sampleDomain); for ($i = 0; $i < 100; $i++) { - $message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i")); + $message->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]); } $messageObj = $message->getMessage(); - $this->assertEquals(100, count($messageObj["to"])); + $this->assertEquals(100, count($messageObj['to'])); } public function testMaximumBatchSize() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); - $message->setSubject("This is the subject of the message!"); - $message->setTextBody("This is the text body of the message!"); + $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setSubject('This is the subject of the message!'); + $message->setTextBody('This is the text body of the message!'); for ($i = 0; $i < 1001; $i++) { - $message->addToRecipient("$i@samples.mailgun.org", array("first" => "Test", "last" => "User $i")); + $message->addToRecipient("$i@samples.mailgun.org", ['first' => 'Test', 'last' => "User $i"]); } $messageObj = $message->getMessage(); - $this->assertEquals(1, count($messageObj["to"])); + $this->assertEquals(1, count($messageObj['to'])); } public function testAttributeResetOnEndBatchMessage() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); - $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); - $message->setSubject("This is the subject of the message!"); - $message->setTextBody("This is the text body of the message!"); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setSubject('This is the subject of the message!'); + $message->setTextBody('This is the text body of the message!'); $message->finalize(); $messageObj = $message->getMessage(); $this->assertTrue(true, empty($messageObj)); @@ -117,10 +117,10 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testDefaultIDInVariables() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('batchRecipientAttributes'); + $property = $reflectionClass->getProperty('batchRecipientAttributes'); $property->setAccessible(true); $propertyValue = $property->getValue($message); $this->assertEquals(1, $propertyValue['test-user@samples.mailgun.org']['id']); @@ -129,44 +129,44 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase public function testgetMessageIds() { $message = $this->client->BatchMessage($this->sampleDomain); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); - $message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); - $message->setSubject("This is the subject of the message!"); - $message->setTextBody("This is the text body of the message!"); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']); + $message->setSubject('This is the subject of the message!'); + $message->setTextBody('This is the text body of the message!'); $message->finalize(); - $this->assertEquals(array("1234"), $message->getMessageIds()); + $this->assertEquals(['1234'], $message->getMessageIds()); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array(1, 2, 3)); + $message->sendMessage([1, 2, 3]); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array("from" => 1, 2, 3)); + $message->sendMessage(['from' => 1, 2, 3]); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array("from" => 1, "to" => 2, 3)); + $message->sendMessage(['from' => 1, 'to' => 2, 3]); } public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml() { - $this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); + $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters'); $message = $this->client->BatchMessage($this->sampleDomain); - $message->sendMessage(array("from" => 1, "to" => 2, "subject" => 3)); + $message->sendMessage(['from' => 1, 'to' => 2, 'subject' => 3]); } } diff --git a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php index 8daf2e3..d32bf78 100644 --- a/tests/Mailgun/Tests/Messages/MessageBuilderTest.php +++ b/tests/Mailgun/Tests/Messages/MessageBuilderTest.php @@ -1,4 +1,5 @@ client->MessageBuilder(); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $propertyValue = $property->getValue($message); $this->assertEquals(0, $propertyValue['recipients']['to']); @@ -38,34 +39,34 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testAddToRecipient() { $message = $this->client->MessageBuilder(); - $message->addToRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("to" => array("'Test User' ")), $messageObj); + $this->assertEquals(['to' => ["'Test User' "]], $messageObj); } public function testAddCcRecipient() { $message = $this->client->MessageBuilder(); - $message->addCcRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addCcRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("cc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['cc' => ["'Test User' "]], $messageObj); } public function testAddBccRecipient() { $message = $this->client->MessageBuilder(); - $message->addBccRecipient("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addBccRecipient('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("bcc" => array("'Test User' ")), $messageObj); + $this->assertEquals(['bcc' => ["'Test User' "]], $messageObj); } public function testToRecipientCount() { $message = $this->client->MessageBuilder(); - $message->addToRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addToRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['to']); @@ -74,10 +75,10 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testCcRecipientCount() { $message = $this->client->MessageBuilder(); - $message->addCcRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addCcRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['cc']); @@ -86,10 +87,10 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testBccRecipientCount() { $message = $this->client->MessageBuilder(); - $message->addBccRecipient("test-user@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->addBccRecipient('test-user@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $reflectionClass = new \ReflectionClass(get_class($message)); - $property = $reflectionClass->getProperty('counters'); + $property = $reflectionClass->getProperty('counters'); $property->setAccessible(true); $array = $property->getValue($message); $this->assertEquals(1, $array['recipients']['bcc']); @@ -98,89 +99,89 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testSetFromAddress() { $message = $this->client->MessageBuilder(); - $message->setFromAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->setFromAddress('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("from" => array("'Test User' ")), $messageObj); + $this->assertEquals(['from' => ["'Test User' "]], $messageObj); } public function testSetReplyTo() { $message = $this->client->MessageBuilder(); - $message->setReplyToAddress("test@samples.mailgun.org", array("first" => "Test", "last" => "User")); + $message->setReplyToAddress('test@samples.mailgun.org', ['first' => 'Test', 'last' => 'User']); $messageObj = $message->getMessage(); - $this->assertEquals(array("h:reply-to" => "'Test User' "), $messageObj); + $this->assertEquals(['h:reply-to' => "'Test User' "], $messageObj); } public function testSetSubject() { $message = $this->client->MessageBuilder(); - $message->setSubject("Test Subject"); + $message->setSubject('Test Subject'); $messageObj = $message->getMessage(); - $this->assertEquals(array("subject" => "Test Subject"), $messageObj); + $this->assertEquals(['subject' => 'Test Subject'], $messageObj); } public function testAddCustomHeader() { $message = $this->client->MessageBuilder(); - $message->addCustomHeader("My-Header", "123"); + $message->addCustomHeader('My-Header', '123'); $messageObj = $message->getMessage(); - $this->assertEquals(array("h:My-Header" => array("123")), $messageObj); + $this->assertEquals(['h:My-Header' => ['123']], $messageObj); } public function testSetTextBody() { $message = $this->client->MessageBuilder(); - $message->setTextBody("This is the text body!"); + $message->setTextBody('This is the text body!'); $messageObj = $message->getMessage(); - $this->assertEquals(array("text" => "This is the text body!"), $messageObj); + $this->assertEquals(['text' => 'This is the text body!'], $messageObj); } public function testSetHtmlBody() { $message = $this->client->MessageBuilder(); - $message->setHtmlBody("This is an awesome email"); + $message->setHtmlBody('This is an awesome email'); $messageObj = $message->getMessage(); - $this->assertEquals(array("html" => "This is an awesome email"), $messageObj); + $this->assertEquals(['html' => 'This is an awesome email'], $messageObj); } public function testAddAttachments() { $message = $this->client->MessageBuilder(); - $message->addAttachment("@../TestAssets/mailgun_icon.png"); - $message->addAttachment("@../TestAssets/rackspace_logo.png"); + $message->addAttachment('@../TestAssets/mailgun_icon.png'); + $message->addAttachment('@../TestAssets/rackspace_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( - 'filePath' => "@../TestAssets/mailgun_icon.png", - 'remoteName' => null - ), - array( - 'filePath' => "@../TestAssets/rackspace_logo.png", - 'remoteName' => null - ) - ), - $messageObj["attachment"] + [ + [ + 'filePath' => '@../TestAssets/mailgun_icon.png', + 'remoteName' => null, + ], + [ + 'filePath' => '@../TestAssets/rackspace_logo.png', + 'remoteName' => null, + ], + ], + $messageObj['attachment'] ); } public function testAddInlineImages() { $message = $this->client->MessageBuilder(); - $message->addInlineImage("@../TestAssets/mailgun_icon.png"); - $message->addInlineImage("@../TestAssets/rackspace_logo.png"); + $message->addInlineImage('@../TestAssets/mailgun_icon.png'); + $message->addInlineImage('@../TestAssets/rackspace_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( - 'filePath' => "@../TestAssets/mailgun_icon.png", - 'remoteName' => null - ), - array( - 'filePath' => "@../TestAssets/rackspace_logo.png", - 'remoteName' => null - ) - ), + [ + [ + 'filePath' => '@../TestAssets/mailgun_icon.png', + 'remoteName' => null, + ], + [ + 'filePath' => '@../TestAssets/rackspace_logo.png', + 'remoteName' => null, + ], + ], $messageObj['inline'] ); } @@ -192,17 +193,17 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message->addAttachment('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( + [ + [ 'filePath' => '@../TestAssets/mailgun_icon.png', - 'remoteName' => 'mg_icon.png' - ), - array( + 'remoteName' => 'mg_icon.png', + ], + [ 'filePath' => '@../TestAssets/rackspace_logo.png', - 'remoteName' => 'rs_logo.png' - ) - ), - $messageObj["attachment"] + 'remoteName' => 'rs_logo.png', + ], + ], + $messageObj['attachment'] ); } @@ -213,16 +214,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message->addInlineImage('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); $messageObj = $message->getFiles(); $this->assertEquals( - array( - array( + [ + [ 'filePath' => '@../TestAssets/mailgun_icon.png', - 'remoteName' => 'mg_icon.png' - ), - array( + 'remoteName' => 'mg_icon.png', + ], + [ 'filePath' => '@../TestAssets/rackspace_logo.png', - 'remoteName' => 'rs_logo.png' - ) - ), + 'remoteName' => 'rs_logo.png', + ], + ], $messageObj['inline'] ); } @@ -232,27 +233,27 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setTestMode(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "yes"), $messageObj); + $this->assertEquals(['o:testmode' => 'yes'], $messageObj); $message->setTestMode(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "no"), $messageObj); - $message->setTestMode("yes"); + $this->assertEquals(['o:testmode' => 'no'], $messageObj); + $message->setTestMode('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "yes"), $messageObj); - $message->setTestMode("no"); + $this->assertEquals(['o:testmode' => 'yes'], $messageObj); + $message->setTestMode('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:testmode" => "no"), $messageObj); + $this->assertEquals(['o:testmode' => 'no'], $messageObj); } public function addCampaignId() { $message = $this->client->MessageBuilder(); - $message->addCampaignId("ABC123"); - $message->addCampaignId("XYZ987"); - $message->addCampaignId("TUV456"); - $message->addCampaignId("NONO123"); + $message->addCampaignId('ABC123'); + $message->addCampaignId('XYZ987'); + $message->addCampaignId('TUV456'); + $message->addCampaignId('NONO123'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:campaign" => array("ABC123", "XYZ987", "TUV456")), $messageObj); + $this->assertEquals(['o:campaign' => ['ABC123', 'XYZ987', 'TUV456']], $messageObj); } public function testSetDkim() @@ -260,16 +261,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setDkim(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "yes"), $messageObj); + $this->assertEquals(['o:dkim' => 'yes'], $messageObj); $message->setDkim(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "no"), $messageObj); - $message->setDkim("yes"); + $this->assertEquals(['o:dkim' => 'no'], $messageObj); + $message->setDkim('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "yes"), $messageObj); - $message->setDkim("no"); + $this->assertEquals(['o:dkim' => 'yes'], $messageObj); + $message->setDkim('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:dkim" => "no"), $messageObj); + $this->assertEquals(['o:dkim' => 'no'], $messageObj); } public function testSetClickTracking() @@ -277,16 +278,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setClickTracking(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj); + $this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj); $message->setClickTracking(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj); - $message->setClickTracking("yes"); + $this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj); + $message->setClickTracking('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj); - $message->setClickTracking("no"); + $this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj); + $message->setClickTracking('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj); + $this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj); } public function testSetOpenTracking() @@ -294,33 +295,33 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase $message = $this->client->MessageBuilder(); $message->setOpenTracking(true); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj); + $this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj); $message->setOpenTracking(false); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "no"), $messageObj); - $message->setOpenTracking("yes"); + $this->assertEquals(['o:tracking-opens' => 'no'], $messageObj); + $message->setOpenTracking('yes'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj); - $message->setOpenTracking("no"); + $this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj); + $message->setOpenTracking('no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:tracking-opens" => "no"), $messageObj); + $this->assertEquals(['o:tracking-opens' => 'no'], $messageObj); } public function testSetDeliveryTime() { $message = $this->client->MessageBuilder(); - $message->setDeliveryTime("January 15, 2014 8:00AM", "CST"); + $message->setDeliveryTime('January 15, 2014 8:00AM', 'CST'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 -0600"), $messageObj); - $message->setDeliveryTime("January 15, 2014 8:00AM", "UTC"); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 -0600'], $messageObj); + $message->setDeliveryTime('January 15, 2014 8:00AM', 'UTC'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj); - $message->setDeliveryTime("January 15, 2014 8:00AM"); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj); + $message->setDeliveryTime('January 15, 2014 8:00AM'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj); - $message->setDeliveryTime("1/15/2014 13:50:01", "CDT"); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj); + $message->setDeliveryTime('1/15/2014 13:50:01', 'CDT'); $messageObj = $message->getMessage(); - $this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 13:50:01 -0600"), $messageObj); + $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 13:50:01 -0600'], $messageObj); // https://github.com/mailgun/mailgun-php/pull/42 // https://github.com/mailgun/mailgun-php/issues/43 //$message->setDeliveryTime("first saturday of July 2013 8:00AM", "CDT"); @@ -331,23 +332,23 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase public function testAddCustomData() { $message = $this->client->MessageBuilder(); - $message->addCustomData("My-Super-Awesome-Data", array("What" => "Mailgun Rocks!")); + $message->addCustomData('My-Super-Awesome-Data', ['What' => 'Mailgun Rocks!']); $messageObj = $message->getMessage(); - $this->assertEquals(array("v:My-Super-Awesome-Data" => "{\"What\":\"Mailgun Rocks!\"}"), $messageObj); + $this->assertEquals(['v:My-Super-Awesome-Data' => '{"What":"Mailgun Rocks!"}'], $messageObj); } public function testAddCustomParameter() { $message = $this->client->MessageBuilder(); - $message->addCustomParameter("my-option", "yes"); - $message->addCustomParameter("o:my-other-option", "no"); + $message->addCustomParameter('my-option', 'yes'); + $message->addCustomParameter('o:my-other-option', 'no'); $messageObj = $message->getMessage(); - $this->assertEquals(array("my-option" => array("yes"), "o:my-other-option" => array("no")), $messageObj); + $this->assertEquals(['my-option' => ['yes'], 'o:my-other-option' => ['no']], $messageObj); } public function testSetMessage() { - $message = array(1, 2, 3, 4, 5); + $message = [1, 2, 3, 4, 5]; $messageBuilder = $this->client->MessageBuilder(); $messageBuilder->setMessage($message); diff --git a/tests/Mailgun/Tests/Messages/StandardMessageTest.php b/tests/Mailgun/Tests/Messages/StandardMessageTest.php index b1f885d..b3f95c3 100644 --- a/tests/Mailgun/Tests/Messages/StandardMessageTest.php +++ b/tests/Mailgun/Tests/Messages/StandardMessageTest.php @@ -1,24 +1,24 @@ client = new Mailgun("My-Super-Awesome-API-Key"); + $this->client = new Mailgun('My-Super-Awesome-API-Key'); } public function testSendMIMEMessage() { - $customMime = "Received: by luna.mailgun.net with SMTP mgrt 8728174999085; Mon, 10 Jun 2013 09:50:58 +0000 + $customMime = 'Received: by luna.mailgun.net with SMTP mgrt 8728174999085; Mon, 10 Jun 2013 09:50:58 +0000 Mime-Version: 1.0 - Content-Type: text/plain; charset=\"ascii\" + Content-Type: text/plain; charset="ascii" Subject: This is the Subject! From: Mailgun Testing To: test@test.mailgun.com @@ -28,21 +28,20 @@ class StandardMessageTest extends \Mailgun\Tests\MailgunTestCase Date: Mon, 10 Jun 2013 09:50:58 +0000 Sender: test@test.mailgun.com - Mailgun is testing!"; - $envelopeFields = array('to' => 'test@test.mailgun.org'); - $result = $this->client->sendMessage("test.mailgun.org", $envelopeFields, $customMime); - $this->assertEquals("test.mailgun.org/messages.mime", $result->http_endpoint_url); + Mailgun is testing!'; + $envelopeFields = ['to' => 'test@test.mailgun.org']; + $result = $this->client->sendMessage('test.mailgun.org', $envelopeFields, $customMime); + $this->assertEquals('test.mailgun.org/messages.mime', $result->http_endpoint_url); } public function testSendMessage() { - $message = array('to' => 'test@test.mailgun.org', + $message = ['to' => 'test@test.mailgun.org', 'from' => 'sender@test.mailgun.org', 'subject' => 'This is my test subject', - 'text' => 'Testing!' - ); - $result = $this->client->sendMessage("test.mailgun.org", $message); - $this->assertEquals("test.mailgun.org/messages", $result->http_endpoint_url); - + 'text' => 'Testing!', + ]; + $result = $this->client->sendMessage('test.mailgun.org', $message); + $this->assertEquals('test.mailgun.org/messages', $result->http_endpoint_url); } } diff --git a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php index b1dee7e..01d860e 100644 --- a/tests/Mailgun/Tests/Mock/Connection/TestBroker.php +++ b/tests/Mailgun/Tests/Mock/Connection/TestBroker.php @@ -1,4 +1,5 @@ apiKey = $apiKey; + $this->apiKey = $apiKey; $this->apiEndpoint = $apiHost; } - public function post($endpointUrl, array $postData = array(), $files = array()) + public function post($endpointUrl, array $postData = [], $files = []) { return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200); } - public function get($endpointUrl, $queryString = array()) + public function get($endpointUrl, $queryString = []) { return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200); } @@ -42,9 +43,9 @@ class TestBroker extends RestClient public function testResponseHandler($endpointUrl, $httpResponseCode = 200) { if ($httpResponseCode === 200) { - $result = new \stdClass(); + $result = new \stdClass(); $result->http_response_body = new \stdClass(); - $jsonResponseData = json_decode('{"message": "Some JSON Response Data", "id": "1234"}'); + $jsonResponseData = json_decode('{"message": "Some JSON Response Data", "id": "1234"}'); foreach ($jsonResponseData as $key => $value) { $result->http_response_body->$key = $value; } @@ -62,10 +63,8 @@ class TestBroker extends RestClient return false; } $result->http_response_code = $httpResponseCode; - $result->http_endpoint_url = $endpointUrl; + $result->http_endpoint_url = $endpointUrl; return $result; } - - } diff --git a/tests/Mailgun/Tests/Mock/Mailgun.php b/tests/Mailgun/Tests/Mock/Mailgun.php index ad57bcc..38e8651 100644 --- a/tests/Mailgun/Tests/Mock/Mailgun.php +++ b/tests/Mailgun/Tests/Mock/Mailgun.php @@ -10,7 +10,7 @@ class Mailgun extends Base protected $debug; protected $restClient; - public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v3") + public function __construct($apiKey = null, $apiEndpoint = 'api.mailgun.net', $apiVersion = 'v3') { $this->restClient = new TestBroker($apiKey, $apiEndpoint, $apiVersion); }