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 @@