From 2d9cd57d2edab93a960fdd4ee2a36ed38adb0c6b Mon Sep 17 00:00:00 2001 From: temirkhan Date: Tue, 19 Apr 2016 15:27:12 +0300 Subject: [PATCH 1/2] PHPDoc typos fixed, minor performance changes --- src/Mailgun/Connection/RestClient.php | 48 ++++++++++++++----------- src/Mailgun/Mailgun.php | 4 ++- src/Mailgun/Messages/MessageBuilder.php | 4 ++- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 1cdc2ee..270c1bc 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -197,22 +197,30 @@ class RestClient */ public function responseHandler(ResponseInterface $responseObj) { - $httpResponseCode = $responseObj->getStatusCode(); - if ($httpResponseCode === 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; - } elseif ($httpResponseCode == 400) { - throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS.$this->getResponseExceptionMessage($responseObj)); - } elseif ($httpResponseCode == 401) { - throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); - } elseif ($httpResponseCode == 404) { - throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT.$this->getResponseExceptionMessage($responseObj)); - } else { - throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); + $httpResponseCode = (int)$responseObj->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; + break; + case 400: + throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj)); + break; + case 401: + throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); + break; + case 404: + throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj)); + break; + default: + throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); + break; } + $result->http_response_code = $httpResponseCode; return $result; @@ -230,6 +238,8 @@ class RestClient if (json_last_error() == JSON_ERROR_NONE && isset($response->message)) { return ' '.$response->message; } + + return ''; } /** @@ -237,6 +247,8 @@ class RestClient * * @param string $fieldName * @param string|array $filePath + * + * @return array */ protected function prepareFile($fieldName, $filePath) { @@ -293,11 +305,7 @@ class RestClient */ private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) { - if (!$ssl) { - return 'http://'.$apiEndpoint.'/'.$apiVersion.'/'; - } else { - return 'https://'.$apiEndpoint.'/'.$apiVersion.'/'; - } + return ($ssl ? 'https://' : 'http://') . $apiEndpoint . '/' . $apiVersion . '/'; } /** diff --git a/src/Mailgun/Mailgun.php b/src/Mailgun/Mailgun.php index 6777801..d31ed18 100644 --- a/src/Mailgun/Mailgun.php +++ b/src/Mailgun/Mailgun.php @@ -50,6 +50,8 @@ class Mailgun{ * @param string $workingDomain * @param array $postData * @param array $postFiles + * + * @return \stdClass * @throws Exceptions\MissingRequiredMIMEParameters */ public function sendMessage($workingDomain, $postData, $postFiles = array()){ @@ -86,7 +88,7 @@ class Mailgun{ * @return bool */ public function verifyWebhookSignature($postData = NULL) { - if(is_null($postData)) { + if($postData === null) { $postData = $_POST; } $hmac = hash_hmac('sha256', "{$postData["timestamp"]}{$postData["token"]}", $this->apiKey); diff --git a/src/Mailgun/Messages/MessageBuilder.php b/src/Mailgun/Messages/MessageBuilder.php index 1ee5d83..9a4a0d0 100644 --- a/src/Mailgun/Messages/MessageBuilder.php +++ b/src/Mailgun/Messages/MessageBuilder.php @@ -276,11 +276,13 @@ class MessageBuilder /** * @param string $inlineImagePath * @param string|null $inlineImageName + * + * @return bool|true * @throws InvalidParameter */ public function addInlineImage($inlineImagePath, $inlineImageName = null) { - if (preg_match("/^@/", $inlineImagePath)) { + if (strpos($inlineImagePath, '@') === 0) { if (isset($this->files['inline'])) { $inlineAttachment = array( 'filePath' => $inlineImagePath, From 30b4aa5e9a2526870e8d461ed00a92677aa5ac9d Mon Sep 17 00:00:00 2001 From: temirkhan Date: Wed, 20 Apr 2016 12:00:50 +0300 Subject: [PATCH 2/2] Little fix in responseHandler switch case: throwing exception prevents next code execution, so it was unnecessary to use breaks. And also use return in case that differs from 200 --- src/Mailgun/Connection/RestClient.php | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Mailgun/Connection/RestClient.php b/src/Mailgun/Connection/RestClient.php index 270c1bc..bb67f8e 100644 --- a/src/Mailgun/Connection/RestClient.php +++ b/src/Mailgun/Connection/RestClient.php @@ -206,24 +206,18 @@ class RestClient $result = new \stdClass(); // return response data as json if possible, raw if not $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; - break; + $result->http_response_code = $httpResponseCode; + + return $result; case 400: throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS . $this->getResponseExceptionMessage($responseObj)); - break; case 401: throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); - break; case 404: throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj)); - break; default: throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); - break; } - - $result->http_response_code = $httpResponseCode; - - return $result; } /**