Merge pull request #144 from DavidGarciaCat/master

Apply PSR Standards
This commit is contained in:
Tobias Nyholm 2016-07-24 14:00:38 +02:00 committed by GitHub
commit 8a6d22198c
24 changed files with 597 additions and 533 deletions

View File

@ -1,25 +1,27 @@
<?php <?php
namespace Mailgun\Connection\Exceptions; namespace Mailgun\Connection\Exceptions;
class GenericHTTPError extends \Exception class GenericHTTPError extends \Exception
{ {
protected $httpResponseCode; protected $httpResponseCode;
protected $httpResponseBody; 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;
}
}
?> 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;
}
}

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Connection\Exceptions; namespace Mailgun\Connection\Exceptions;
class InvalidCredentials extends \Exception{} class InvalidCredentials extends \Exception
{
}

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Connection\Exceptions; namespace Mailgun\Connection\Exceptions;
class MissingEndpoint extends \Exception{} class MissingEndpoint extends \Exception
{
}

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Connection\Exceptions; namespace Mailgun\Connection\Exceptions;
class MissingRequiredParameters extends \Exception{} class MissingRequiredParameters extends \Exception
{
}

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Connection\Exceptions; namespace Mailgun\Connection\Exceptions;
class NoDomainsConfigured extends \Exception{} class NoDomainsConfigured extends \Exception
{
}

View File

@ -8,8 +8,8 @@ use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\MultipartStream\MultipartStreamBuilder; use Http\Message\MultipartStream\MultipartStreamBuilder;
use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\GenericHTTPError;
use Mailgun\Connection\Exceptions\InvalidCredentials; use Mailgun\Connection\Exceptions\InvalidCredentials;
use Mailgun\Connection\Exceptions\MissingRequiredParameters;
use Mailgun\Connection\Exceptions\MissingEndpoint; use Mailgun\Connection\Exceptions\MissingEndpoint;
use Mailgun\Connection\Exceptions\MissingRequiredParameters;
use Mailgun\Constants\Api; use Mailgun\Constants\Api;
use Mailgun\Constants\ExceptionMessages; use Mailgun\Constants\ExceptionMessages;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
@ -20,7 +20,8 @@ use Psr\Http\Message\ResponseInterface;
class RestClient class RestClient
{ {
/** /**
* Your API key * Your API key.
*
* @var string * @var string
*/ */
private $apiKey; private $apiKey;
@ -36,21 +37,23 @@ class RestClient
protected $apiHost; protected $apiHost;
/** /**
* The version of the API to use * The version of the API to use.
*
* @var string * @var string
*/ */
protected $apiVersion = 'v2'; protected $apiVersion = 'v2';
/** /**
* If we should use SSL or not * If we should use SSL or not.
*
* @var bool * @var bool
*/ */
protected $sslEnabled = true; protected $sslEnabled = true;
/** /**
* @param string $apiKey * @param string $apiKey
* @param string $apiHost * @param string $apiHost
* @param HttpClient $httpClient * @param HttpClient $httpClient
*/ */
public function __construct($apiKey, $apiHost, HttpClient $httpClient = null) public function __construct($apiKey, $apiHost, HttpClient $httpClient = null)
{ {
@ -66,12 +69,12 @@ class RestClient
* @param array $files * @param array $files
* @param array $headers * @param array $headers
* *
* @return \stdClass
*
* @throws GenericHTTPError * @throws GenericHTTPError
* @throws InvalidCredentials * @throws InvalidCredentials
* @throws MissingEndpoint * @throws MissingEndpoint
* @throws MissingRequiredParameters * @throws MissingRequiredParameters
*
* @return \stdClass
*/ */
protected function send($method, $uri, $body = null, $files = [], array $headers = []) protected function send($method, $uri, $body = null, $files = [], array $headers = [])
{ {
@ -101,14 +104,14 @@ class RestClient
* @param array $postData * @param array $postData
* @param array $files * @param array $files
* *
* @return \stdClass
*
* @throws GenericHTTPError * @throws GenericHTTPError
* @throws InvalidCredentials * @throws InvalidCredentials
* @throws MissingEndpoint * @throws MissingEndpoint
* @throws MissingRequiredParameters * @throws MissingRequiredParameters
*
* @return \stdClass
*/ */
public function post($endpointUrl, array $postData = array(), $files = array()) public function post($endpointUrl, array $postData = [], $files = [])
{ {
$postFiles = []; $postFiles = [];
@ -130,13 +133,13 @@ class RestClient
if (is_array($value)) { if (is_array($value)) {
foreach ($value as $subValue) { foreach ($value as $subValue) {
$postDataMultipart[] = [ $postDataMultipart[] = [
'name' => $key, 'name' => $key,
'contents' => $subValue, 'contents' => $subValue,
]; ];
} }
} else { } else {
$postDataMultipart[] = [ $postDataMultipart[] = [
'name' => $key, 'name' => $key,
'contents' => $value, 'contents' => $value,
]; ];
} }
@ -149,14 +152,14 @@ class RestClient
* @param string $endpointUrl * @param string $endpointUrl
* @param array $queryString * @param array $queryString
* *
* @return \stdClass
*
* @throws GenericHTTPError * @throws GenericHTTPError
* @throws InvalidCredentials * @throws InvalidCredentials
* @throws MissingEndpoint * @throws MissingEndpoint
* @throws MissingRequiredParameters * @throws MissingRequiredParameters
*
* @return \stdClass
*/ */
public function get($endpointUrl, $queryString = array()) public function get($endpointUrl, $queryString = [])
{ {
return $this->send('GET', $endpointUrl.'?'.http_build_query($queryString)); return $this->send('GET', $endpointUrl.'?'.http_build_query($queryString));
} }
@ -164,12 +167,12 @@ class RestClient
/** /**
* @param string $endpointUrl * @param string $endpointUrl
* *
* @return \stdClass
*
* @throws GenericHTTPError * @throws GenericHTTPError
* @throws InvalidCredentials * @throws InvalidCredentials
* @throws MissingEndpoint * @throws MissingEndpoint
* @throws MissingRequiredParameters * @throws MissingRequiredParameters
*
* @return \stdClass
*/ */
public function delete($endpointUrl) public function delete($endpointUrl)
{ {
@ -180,12 +183,12 @@ class RestClient
* @param string $endpointUrl * @param string $endpointUrl
* @param mixed $putData * @param mixed $putData
* *
* @return \stdClass
*
* @throws GenericHTTPError * @throws GenericHTTPError
* @throws InvalidCredentials * @throws InvalidCredentials
* @throws MissingEndpoint * @throws MissingEndpoint
* @throws MissingRequiredParameters * @throws MissingRequiredParameters
*
* @return \stdClass
*/ */
public function put($endpointUrl, $putData) public function put($endpointUrl, $putData)
{ {
@ -195,35 +198,35 @@ class RestClient
/** /**
* @param ResponseInterface $responseObj * @param ResponseInterface $responseObj
* *
* @return \stdClass
*
* @throws GenericHTTPError * @throws GenericHTTPError
* @throws InvalidCredentials * @throws InvalidCredentials
* @throws MissingEndpoint * @throws MissingEndpoint
* @throws MissingRequiredParameters * @throws MissingRequiredParameters
*
* @return \stdClass
*/ */
public function responseHandler(ResponseInterface $responseObj) public function responseHandler(ResponseInterface $responseObj)
{ {
$httpResponseCode = (int)$responseObj->getStatusCode(); $httpResponseCode = (int) $responseObj->getStatusCode();
switch ($httpResponseCode) { switch ($httpResponseCode) {
case 200: case 200:
$data = (string)$responseObj->getBody(); $data = (string) $responseObj->getBody();
$jsonResponseData = json_decode($data, false); $jsonResponseData = json_decode($data, false);
$result = new \stdClass(); $result = new \stdClass();
// return response data as json if possible, raw if not // return response data as json if possible, raw if not
$result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
$result->http_response_code = $httpResponseCode; $result->http_response_code = $httpResponseCode;
return $result; return $result;
case 400: 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: case 401:
throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS); throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS);
case 404: case 404:
throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT . $this->getResponseExceptionMessage($responseObj)); throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT.$this->getResponseExceptionMessage($responseObj));
default: default:
throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
} }
} }
@ -266,15 +269,13 @@ class RestClient
} }
return [ return [
'name' => $fieldName, 'name' => $fieldName,
'contents' => fopen($filePath, 'r'), 'contents' => fopen($filePath, 'r'),
'filename' => $filename, 'filename' => $filename,
]; ];
} }
/** /**
*
* @return HttpClient * @return HttpClient
*/ */
protected function getHttpClient() protected function getHttpClient()
@ -296,7 +297,6 @@ class RestClient
return $this->generateEndpoint($this->apiHost, $this->apiVersion, $this->sslEnabled).$uri; return $this->generateEndpoint($this->apiHost, $this->apiVersion, $this->sslEnabled).$uri;
} }
/** /**
* @param string $apiEndpoint * @param string $apiEndpoint
* @param string $apiVersion * @param string $apiVersion
@ -306,7 +306,7 @@ class RestClient
*/ */
private function generateEndpoint($apiEndpoint, $apiVersion, $ssl) private function generateEndpoint($apiEndpoint, $apiVersion, $ssl)
{ {
return ($ssl ? 'https://' : 'http://') . $apiEndpoint . '/' . $apiVersion . '/'; return ($ssl ? 'https://' : 'http://').$apiEndpoint.'/'.$apiVersion.'/';
} }
/** /**
@ -322,7 +322,7 @@ class RestClient
} }
/** /**
* @param boolean $sslEnabled * @param bool $sslEnabled
* *
* @return RestClient * @return RestClient
*/ */

View File

@ -1,16 +1,14 @@
<?php <?php
namespace Mailgun\Constants; namespace Mailgun\Constants;
class Api
class Api { {
const API_USER = "api"; const API_USER = 'api';
const SDK_VERSION = "1.7"; const SDK_VERSION = '1.7';
const SDK_USER_AGENT = "mailgun-sdk-php"; const SDK_USER_AGENT = 'mailgun-sdk-php';
const RECIPIENT_COUNT_LIMIT = 1000; const RECIPIENT_COUNT_LIMIT = 1000;
const CAMPAIGN_ID_LIMIT = 3; const CAMPAIGN_ID_LIMIT = 3;
const TAG_LIMIT = 3; const TAG_LIMIT = 3;
const DEFAULT_TIME_ZONE = "UTC"; const DEFAULT_TIME_ZONE = 'UTC';
} }

View File

@ -1,21 +1,19 @@
<?php <?php
namespace Mailgun\Constants; namespace Mailgun\Constants;
class ExceptionMessages
class ExceptionMessages { {
const EXCEPTION_INVALID_CREDENTIALS = "Your credentials are incorrect."; 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_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!"; const EXCEPTION_MISSING_REQUIRED_PARAMETERS = 'The parameters passed to the API were invalid. Check your inputs!';
const EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS = "The parameters passed to the API were invalid. Check your inputs!"; const EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS = 'The parameters passed to the API were invalid. Check your inputs!';
const EXCEPTION_MISSING_ENDPOINT = "The endpoint you've tried to access does not exist. Check your URL."; const EXCEPTION_MISSING_ENDPOINT = "The endpoint you've tried to access does not exist. Check your URL.";
const TOO_MANY_RECIPIENTS = "You've exceeded the maximum recipient count (1,000) on the to field with autosend disabled."; const TOO_MANY_RECIPIENTS = "You've exceeded the maximum recipient count (1,000) on the to field with autosend disabled.";
const INVALID_PARAMETER_NON_ARRAY = "The parameter you've passed in position 2 must be an array."; const INVALID_PARAMETER_NON_ARRAY = "The parameter you've passed in position 2 must be an array.";
const INVALID_PARAMETER_ATTACHMENT = "Attachments must be passed with an \"@\" preceding the file path. Web resources not supported."; const INVALID_PARAMETER_ATTACHMENT = 'Attachments must be passed with an "@" preceding the file path. Web resources not supported.';
const INVALID_PARAMETER_INLINE = "Inline images must be passed with an \"@\" preceding the file path. Web resources not supported."; const INVALID_PARAMETER_INLINE = 'Inline images must be passed with an "@" preceding the file path. Web resources not supported.';
const TOO_MANY_PARAMETERS_CAMPAIGNS = "You've exceeded the maximum (3) campaigns for a single message."; const TOO_MANY_PARAMETERS_CAMPAIGNS = "You've exceeded the maximum (3) campaigns for a single message.";
const TOO_MANY_PARAMETERS_TAGS = "You've exceeded the maximum (3) tags for a single message."; const TOO_MANY_PARAMETERS_TAGS = "You've exceeded the maximum (3) tags for a single message.";
const TOO_MANY_PARAMETERS_RECIPIENT = "You've exceeded the maximum recipient count (1,000) on the to field with autosend disabled."; const TOO_MANY_PARAMETERS_RECIPIENT = "You've exceeded the maximum recipient count (1,000) on the to field with autosend disabled.";
} }

View File

@ -2,52 +2,52 @@
namespace Mailgun\Lists; namespace Mailgun\Lists;
use Mailgun\Messages\Exceptions\InvalidParameter;
use Mailgun\Messages\Exceptions\TooManyParameters;
use Mailgun\Messages\Expcetions\InvalidParameterType;
/** /**
* This class is used for creating a unique hash for * This class is used for creating a unique hash for
* mailing list subscription double-opt in requests. * mailing list subscription double-opt in requests.
* *
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Lists/README.md * @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Lists/README.md
*/ */
class OptInHandler{ class OptInHandler
{
/** /**
* @param string $mailingList * @param string $mailingList
* @param string $secretAppId * @param string $secretAppId
* @param string $recipientAddress * @param string $recipientAddress
*
* @return string * @return string
*/ */
public function generateHash($mailingList, $secretAppId, $recipientAddress){ public function generateHash($mailingList, $secretAppId, $recipientAddress)
$innerPayload = array('r' => $recipientAddress, 'l' => $mailingList); {
$encodedInnerPayload = base64_encode(json_encode($innerPayload)); $innerPayload = ['r' => $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); $outerPayload = ['h' => $innerHash, 'p' => $encodedInnerPayload];
return urlencode(base64_encode(json_encode($outerPayload))); return urlencode(base64_encode(json_encode($outerPayload)));
} }
/** /**
* @param string $secretAppId * @param string $secretAppId
* @param string $uniqueHash * @param string $uniqueHash
*
* @return array|bool * @return array|bool
*/ */
public function validateHash($secretAppId, $uniqueHash){ public function validateHash($secretAppId, $uniqueHash)
$decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true); {
$decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true);
$decodedHash = $decodedOuterPayload['h']; $decodedHash = $decodedOuterPayload['h'];
$innerPayload = $decodedOuterPayload['p']; $innerPayload = $decodedOuterPayload['p'];
$decodedInnerPayload = json_decode(base64_decode($innerPayload), true); $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']); return ['recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']];
} }
return false; return false;
} }
} }

View File

@ -3,11 +3,11 @@
namespace Mailgun; namespace Mailgun;
use Http\Client\HttpClient; use Http\Client\HttpClient;
use Mailgun\Constants\ExceptionMessages;
use Mailgun\Messages\Exceptions;
use Mailgun\Connection\RestClient; use Mailgun\Connection\RestClient;
use Mailgun\Messages\BatchMessage; use Mailgun\Constants\ExceptionMessages;
use Mailgun\Lists\OptInHandler; use Mailgun\Lists\OptInHandler;
use Mailgun\Messages\BatchMessage;
use Mailgun\Messages\Exceptions;
use Mailgun\Messages\MessageBuilder; use Mailgun\Messages\MessageBuilder;
/** /**
@ -16,8 +16,8 @@ use Mailgun\Messages\MessageBuilder;
* *
* @link https://github.com/mailgun/mailgun-php/blob/master/README.md * @link https://github.com/mailgun/mailgun-php/blob/master/README.md
*/ */
class Mailgun{ class Mailgun
{
/** /**
* @var RestClient * @var RestClient
*/ */
@ -30,8 +30,8 @@ class Mailgun{
/** /**
* @param string|null $apiKey * @param string|null $apiKey
* @param HttpClient $httpClient * @param HttpClient $httpClient
* @param string $apiEndpoint * @param string $apiEndpoint
*/ */
public function __construct( public function __construct(
$apiKey = null, $apiKey = null,
@ -48,27 +48,28 @@ class Mailgun{
* position of the function call. * position of the function call.
* *
* @param string $workingDomain * @param string $workingDomain
* @param array $postData * @param array $postData
* @param array $postFiles * @param array $postFiles
* @return \stdClass *
* @throws Exceptions\MissingRequiredMIMEParameters * @throws Exceptions\MissingRequiredMIMEParameters
*
* @return \stdClass
*/ */
public function sendMessage($workingDomain, $postData, $postFiles = array()){ public function sendMessage($workingDomain, $postData, $postFiles = [])
if(is_array($postFiles)){ {
if (is_array($postFiles)) {
return $this->post("$workingDomain/messages", $postData, $postFiles); return $this->post("$workingDomain/messages", $postData, $postFiles);
} } elseif (is_string($postFiles)) {
else if(is_string($postFiles)){ $tempFile = tempnam(sys_get_temp_dir(), 'MG_TMP_MIME');
$fileHandle = fopen($tempFile, 'w');
$tempFile = tempnam(sys_get_temp_dir(), "MG_TMP_MIME");
$fileHandle = fopen($tempFile, "w");
fwrite($fileHandle, $postFiles); fwrite($fileHandle, $postFiles);
$result = $this->post("$workingDomain/messages.mime", $postData, array("message" => $tempFile)); $result = $this->post("$workingDomain/messages.mime", $postData, ['message' => $tempFile]);
fclose($fileHandle); fclose($fileHandle);
unlink($tempFile); unlink($tempFile);
return $result; return $result;
} } else {
else{
throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
} }
} }
@ -84,59 +85,68 @@ class Mailgun{
* You should reject the request with status code 403 Forbidden. * You should reject the request with status code 403 Forbidden.
* *
* @param array|null $postData * @param array|null $postData
*
* @return bool * @return bool
*/ */
public function verifyWebhookSignature($postData = NULL) { public function verifyWebhookSignature($postData = null)
if($postData === null) { {
if ($postData === null) {
$postData = $_POST; $postData = $_POST;
} }
if(!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) { if (!isset($postData['timestamp']) || !isset($postData['token']) || !isset($postData['signature'])) {
return false; 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']; $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 // hash_equals is constant time, but will not be introduced until PHP 5.6
return hash_equals($hmac, $sig); return hash_equals($hmac, $sig);
} } else {
else { return $hmac === $sig;
return ($hmac === $sig);
} }
} }
/** /**
* @param string $endpointUrl * @param string $endpointUrl
* @param array $postData * @param array $postData
* @param array $files * @param array $files
*
* @return \stdClass * @return \stdClass
*/ */
public function post($endpointUrl, $postData = array(), $files = array()){ public function post($endpointUrl, $postData = [], $files = [])
{
return $this->restClient->post($endpointUrl, $postData, $files); return $this->restClient->post($endpointUrl, $postData, $files);
} }
/** /**
* @param string $endpointUrl * @param string $endpointUrl
* @param array $queryString * @param array $queryString
*
* @return \stdClass * @return \stdClass
*/ */
public function get($endpointUrl, $queryString = array()){ public function get($endpointUrl, $queryString = [])
{
return $this->restClient->get($endpointUrl, $queryString); return $this->restClient->get($endpointUrl, $queryString);
} }
/** /**
* @param string $endpointUrl * @param string $endpointUrl
*
* @return \stdClass * @return \stdClass
*/ */
public function delete($endpointUrl){ public function delete($endpointUrl)
{
return $this->restClient->delete($endpointUrl); return $this->restClient->delete($endpointUrl);
} }
/** /**
* @param string $endpointUrl * @param string $endpointUrl
* @param array $putData * @param array $putData
*
* @return \stdClass * @return \stdClass
*/ */
public function put($endpointUrl, $putData){ public function put($endpointUrl, $putData)
{
return $this->restClient->put($endpointUrl, $putData); return $this->restClient->put($endpointUrl, $putData);
} }
@ -153,7 +163,7 @@ class Mailgun{
} }
/** /**
* @param boolean $sslEnabled * @param bool $sslEnabled
* *
* @return Mailgun * @return Mailgun
*/ */
@ -167,23 +177,27 @@ class Mailgun{
/** /**
* @return MessageBuilder * @return MessageBuilder
*/ */
public function MessageBuilder(){ public function MessageBuilder()
{
return new MessageBuilder(); return new MessageBuilder();
} }
/** /**
* @return OptInHandler * @return OptInHandler
*/ */
public function OptInHandler(){ public function OptInHandler()
{
return new OptInHandler(); return new OptInHandler();
} }
/** /**
* @param string $workingDomain * @param string $workingDomain
* @param bool $autoSend * @param bool $autoSend
*
* @return BatchMessage * @return BatchMessage
*/ */
public function BatchMessage($workingDomain, $autoSend = true){ public function BatchMessage($workingDomain, $autoSend = true)
{
return new BatchMessage($this->restClient, $workingDomain, $autoSend); return new BatchMessage($this->restClient, $workingDomain, $autoSend);
} }
} }

View File

@ -4,8 +4,8 @@ namespace Mailgun\Messages;
use Mailgun\Constants\Api; use Mailgun\Constants\Api;
use Mailgun\Constants\ExceptionMessages; use Mailgun\Constants\ExceptionMessages;
use Mailgun\Messages\Exceptions\TooManyParameters;
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters; use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
use Mailgun\Messages\Exceptions\TooManyParameters;
/** /**
* This class is used for batch sending. See the official documentation (link below) * This class is used for batch sending. See the official documentation (link below)
@ -13,134 +13,135 @@ use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
* *
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md * @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
*/ */
class BatchMessage extends MessageBuilder{ class BatchMessage extends MessageBuilder
{
/** /**
* @var array * @var array
*/ */
private $batchRecipientAttributes; private $batchRecipientAttributes;
/** /**
* @var boolean * @var bool
*/ */
private $autoSend; private $autoSend;
/** /**
* @var \Mailgun\Connection\RestClient * @var \Mailgun\Connection\RestClient
*/ */
private $restClient; private $restClient;
/** /**
* @var string * @var string
*/ */
private $workingDomain; private $workingDomain;
/** /**
* @var array * @var array
*/ */
private $messageIds = array(); private $messageIds = [];
/** /**
* @var string * @var string
*/ */
private $endpointUrl; private $endpointUrl;
/** /**
* @param \Mailgun\Connection\RestClient $restClient * @param \Mailgun\Connection\RestClient $restClient
* @param string $workingDomain * @param string $workingDomain
* @param boolean $autoSend * @param bool $autoSend
*/ */
public function __construct($restClient, $workingDomain, $autoSend){ public function __construct($restClient, $workingDomain, $autoSend)
$this->batchRecipientAttributes = array(); {
$this->autoSend = $autoSend; $this->batchRecipientAttributes = [];
$this->restClient = $restClient; $this->autoSend = $autoSend;
$this->workingDomain = $workingDomain; $this->restClient = $restClient;
$this->endpointUrl = $workingDomain . "/messages"; $this->workingDomain = $workingDomain;
} $this->endpointUrl = $workingDomain.'/messages';
}
/** /**
* @param string $headerName * @param string $headerName
* @param string $address * @param string $address
* @param array $variables * @param array $variables
*
* @throws MissingRequiredMIMEParameters * @throws MissingRequiredMIMEParameters
* @throws TooManyParameters * @throws TooManyParameters
*/ */
protected function addRecipient($headerName, $address, $variables){ protected function addRecipient($headerName, $address, $variables)
if(array_key_exists($headerName, $this->counters['recipients'])){ {
if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT){ if (array_key_exists($headerName, $this->counters['recipients'])) {
if($this->autoSend == false){ if ($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS); if ($this->autoSend == false) {
} throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS);
$this->sendMessage(); }
} $this->sendMessage();
} }
}
$compiledAddress = $this->parseAddress($address, $variables); $compiledAddress = $this->parseAddress($address, $variables);
if(isset($this->message[$headerName])){ if (isset($this->message[$headerName])) {
array_push($this->message[$headerName], $compiledAddress); array_push($this->message[$headerName], $compiledAddress);
} } elseif ($headerName == 'h:reply-to') {
elseif($headerName == "h:reply-to"){ $this->message[$headerName] = $compiledAddress;
$this->message[$headerName] = $compiledAddress; } else {
} $this->message[$headerName] = [$compiledAddress];
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; $this->counters['recipients'][$headerName] += 1;
if(!array_key_exists("id", $variables)){ if (!array_key_exists('id', $variables)) {
$variables['id'] = $this->counters['recipients'][$headerName]; $variables['id'] = $this->counters['recipients'][$headerName];
} }
} }
$this->batchRecipientAttributes["$address"] = $variables; $this->batchRecipientAttributes["$address"] = $variables;
} }
/** /**
* @param array $message * @param array $message
* @param array $files * @param array $files
*
* @throws MissingRequiredMIMEParameters * @throws MissingRequiredMIMEParameters
*/ */
public function sendMessage($message = array(), $files = array()){ public function sendMessage($message = [], $files = [])
if(count($message) < 1){ {
$message = $this->message; if (count($message) < 1) {
$files = $this->files; $message = $this->message;
} $files = $this->files;
if(!array_key_exists("from", $message)){ }
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); 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); 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);
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);
elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))){ } else {
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); $message['recipient-variables'] = json_encode($this->batchRecipientAttributes);
} $response = $this->restClient->post($this->endpointUrl, $message, $files);
else{ $this->batchRecipientAttributes = [];
$message["recipient-variables"] = json_encode($this->batchRecipientAttributes); $this->counters['recipients']['to'] = 0;
$response = $this->restClient->post($this->endpointUrl, $message, $files); $this->counters['recipients']['cc'] = 0;
$this->batchRecipientAttributes = array(); $this->counters['recipients']['bcc'] = 0;
$this->counters['recipients']['to'] = 0; unset($this->message['to']);
$this->counters['recipients']['cc'] = 0; array_push($this->messageIds, $response->http_response_body->id);
$this->counters['recipients']['bcc'] = 0; }
unset($this->message["to"]); }
array_push($this->messageIds, $response->http_response_body->id);
}
}
/** /**
* @throws MissingRequiredMIMEParameters * @throws MissingRequiredMIMEParameters
*/ */
public function finalize(){ public function finalize()
$this->sendMessage(); {
} $this->sendMessage();
}
/** /**
* @return string[] * @return string[]
*/ */
public function getMessageIds(){ public function getMessageIds()
return $this->messageIds; {
} return $this->messageIds;
}
} }

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Messages\Exceptions; namespace Mailgun\Messages\Exceptions;
class InvalidParameter extends \Exception{} class InvalidParameter extends \Exception
{
}

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Messages\Exceptions; namespace Mailgun\Messages\Exceptions;
class InvalidParameterType extends \Exception{} class InvalidParameterType extends \Exception
{
}

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Messages\Exceptions; namespace Mailgun\Messages\Exceptions;
class MissingRequiredMIMEParameters extends \Exception{} class MissingRequiredMIMEParameters extends \Exception
{
}

View File

@ -1,4 +1,7 @@
<?php <?php
namespace Mailgun\Messages\Exceptions; namespace Mailgun\Messages\Exceptions;
class TooManyParameters extends \Exception{} class TooManyParameters extends \Exception
{
}

View File

@ -20,39 +20,40 @@ class MessageBuilder
/** /**
* @var array * @var array
*/ */
protected $message = array(); protected $message = [];
/** /**
* @var array * @var array
*/ */
protected $variables = array(); protected $variables = [];
/** /**
* @var array * @var array
*/ */
protected $files = array(); protected $files = [];
/** /**
* @var array * @var array
*/ */
protected $counters = array( protected $counters = [
'recipients' => array( 'recipients' => [
'to' => 0, 'to' => 0,
'cc' => 0, 'cc' => 0,
'bcc' => 0 'bcc' => 0,
), ],
'attributes' => array( 'attributes' => [
'attachment' => 0, 'attachment' => 0,
'campaign_id' => 0, 'campaign_id' => 0,
'custom_option' => 0, 'custom_option' => 0,
'tag' => 0 'tag' => 0,
) ],
); ];
/** /**
* @param array $params * @param array $params
* @param string $key * @param string $key
* @param mixed $default * @param mixed $default
*
* @return mixed * @return mixed
*/ */
protected function safeGet($params, $key, $default) protected function safeGet($params, $key, $default)
@ -66,23 +67,25 @@ class MessageBuilder
/** /**
* @param array $params * @param array $params
*
* @return mixed|string * @return mixed|string
*/ */
protected function getFullName($params) protected function getFullName($params)
{ {
if (array_key_exists("first", $params)) { if (array_key_exists('first', $params)) {
$first = $this->safeGet($params, "first", ""); $first = $this->safeGet($params, 'first', '');
$last = $this->safeGet($params, "last", ""); $last = $this->safeGet($params, 'last', '');
return trim("$first $last"); return trim("$first $last");
} }
return $this->safeGet($params, "full_name", ""); return $this->safeGet($params, 'full_name', '');
} }
/** /**
* @param string $address * @param string $address
* @param array $variables * @param array $variables
*
* @return string * @return string
*/ */
protected function parseAddress($address, $variables) protected function parseAddress($address, $variables)
@ -101,7 +104,7 @@ class MessageBuilder
/** /**
* @param string $headerName * @param string $headerName
* @param string $address * @param string $address
* @param array $variables * @param array $variables
*/ */
protected function addRecipient($headerName, $address, $variables) protected function addRecipient($headerName, $address, $variables)
{ {
@ -109,10 +112,10 @@ class MessageBuilder
if (isset($this->message[$headerName])) { if (isset($this->message[$headerName])) {
array_push($this->message[$headerName], $compiledAddress); array_push($this->message[$headerName], $compiledAddress);
} elseif ($headerName == "h:reply-to") { } elseif ($headerName == 'h:reply-to') {
$this->message[$headerName] = $compiledAddress; $this->message[$headerName] = $compiledAddress;
} else { } else {
$this->message[$headerName] = array($compiledAddress); $this->message[$headerName] = [$compiledAddress];
} }
if (array_key_exists($headerName, $this->counters['recipients'])) { if (array_key_exists($headerName, $this->counters['recipients'])) {
$this->counters['recipients'][$headerName] += 1; $this->counters['recipients'][$headerName] += 1;
@ -120,85 +123,94 @@ class MessageBuilder
} }
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
* @return mixed *
* @throws TooManyParameters * @throws TooManyParameters
*
* @return mixed
*/ */
public function addToRecipient($address, $variables = null) public function addToRecipient($address, $variables = null)
{ {
if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) { if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
} }
$this->addRecipient("to", $address, $variables); $this->addRecipient('to', $address, $variables);
return end($this->message['to']); return end($this->message['to']);
} }
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
* @return mixed *
* @throws TooManyParameters * @throws TooManyParameters
*
* @return mixed
*/ */
public function addCcRecipient($address, $variables = null) public function addCcRecipient($address, $variables = null)
{ {
if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) { if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
} }
$this->addRecipient("cc", $address, $variables); $this->addRecipient('cc', $address, $variables);
return end($this->message['cc']); return end($this->message['cc']);
} }
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
* @return mixed *
* @throws TooManyParameters * @throws TooManyParameters
*
* @return mixed
*/ */
public function addBccRecipient($address, $variables = null) public function addBccRecipient($address, $variables = null)
{ {
if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) { if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT); throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
} }
$this->addRecipient("bcc", $address, $variables); $this->addRecipient('bcc', $address, $variables);
return end($this->message['bcc']); return end($this->message['bcc']);
} }
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
*
* @return mixed * @return mixed
*/ */
public function setFromAddress($address, $variables = null) public function setFromAddress($address, $variables = null)
{ {
$this->addRecipient("from", $address, $variables); $this->addRecipient('from', $address, $variables);
return $this->message['from']; return $this->message['from'];
} }
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
*
* @return mixed * @return mixed
*/ */
public function setReplyToAddress($address, $variables = null) 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']; return $this->message['h:reply-to'];
} }
/** /**
* @param string $subject * @param string $subject
*
* @return mixed * @return mixed
*/ */
public function setSubject($subject = "") public function setSubject($subject = '')
{ {
if ($subject == null || $subject == "") { if ($subject == null || $subject == '') {
$subject = " "; $subject = ' ';
} }
$this->message['subject'] = $subject; $this->message['subject'] = $subject;
@ -207,27 +219,29 @@ class MessageBuilder
/** /**
* @param string $headerName * @param string $headerName
* @param mixed $headerData * @param mixed $headerData
*
* @return mixed * @return mixed
*/ */
public function addCustomHeader($headerName, $headerData) public function addCustomHeader($headerName, $headerData)
{ {
if (!preg_match("/^h:/i", $headerName)) { if (!preg_match('/^h:/i', $headerName)) {
$headerName = "h:" . $headerName; $headerName = 'h:'.$headerName;
} }
$this->message[$headerName] = array($headerData); $this->message[$headerName] = [$headerData];
return $this->message[$headerName]; return $this->message[$headerName];
} }
/** /**
* @param string $textBody * @param string $textBody
*
* @return string * @return string
*/ */
public function setTextBody($textBody) public function setTextBody($textBody)
{ {
if ($textBody == null || $textBody == "") { if ($textBody == null || $textBody == '') {
$textBody = " "; $textBody = ' ';
} }
$this->message['text'] = $textBody; $this->message['text'] = $textBody;
@ -236,12 +250,13 @@ class MessageBuilder
/** /**
* @param string $htmlBody * @param string $htmlBody
*
* @return string * @return string
*/ */
public function setHtmlBody($htmlBody) public function setHtmlBody($htmlBody)
{ {
if ($htmlBody == null || $htmlBody == "") { if ($htmlBody == null || $htmlBody == '') {
$htmlBody = " "; $htmlBody = ' ';
} }
$this->message['html'] = $htmlBody; $this->message['html'] = $htmlBody;
@ -249,53 +264,55 @@ class MessageBuilder
} }
/** /**
* @param string $attachmentPath * @param string $attachmentPath
* @param string|null $attachmentName * @param string|null $attachmentName
*
* @return bool * @return bool
*/ */
public function addAttachment($attachmentPath, $attachmentName = null) public function addAttachment($attachmentPath, $attachmentName = null)
{ {
if (isset($this->files["attachment"])) { if (isset($this->files['attachment'])) {
$attachment = array( $attachment = [
'filePath' => $attachmentPath, 'filePath' => $attachmentPath,
'remoteName' => $attachmentName 'remoteName' => $attachmentName,
); ];
array_push($this->files["attachment"], $attachment); array_push($this->files['attachment'], $attachment);
} else { } else {
$this->files["attachment"] = array( $this->files['attachment'] = [
array( [
'filePath' => $attachmentPath, 'filePath' => $attachmentPath,
'remoteName' => $attachmentName 'remoteName' => $attachmentName,
) ],
); ];
} }
return true; return true;
} }
/** /**
* @param string $inlineImagePath * @param string $inlineImagePath
* @param string|null $inlineImageName * @param string|null $inlineImageName
* *
* @return bool|true
* @throws InvalidParameter * @throws InvalidParameter
*
* @return bool|true
*/ */
public function addInlineImage($inlineImagePath, $inlineImageName = null) public function addInlineImage($inlineImagePath, $inlineImageName = null)
{ {
if (strpos($inlineImagePath, '@') === 0) { if (strpos($inlineImagePath, '@') === 0) {
if (isset($this->files['inline'])) { if (isset($this->files['inline'])) {
$inlineAttachment = array( $inlineAttachment = [
'filePath' => $inlineImagePath, 'filePath' => $inlineImagePath,
'remoteName' => $inlineImageName 'remoteName' => $inlineImageName,
); ];
array_push($this->files['inline'], $inlineAttachment); array_push($this->files['inline'], $inlineAttachment);
} else { } else {
$this->files['inline'] = array( $this->files['inline'] = [
array( [
'filePath' => $inlineImagePath, 'filePath' => $inlineImagePath,
'remoteName' => $inlineImageName 'remoteName' => $inlineImageName,
) ],
); ];
} }
return true; return true;
@ -305,15 +322,16 @@ class MessageBuilder
} }
/** /**
* @param boolean $testMode * @param bool $testMode
*
* @return string * @return string
*/ */
public function setTestMode($testMode) public function setTestMode($testMode)
{ {
if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) { if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
$testMode = "yes"; $testMode = 'yes';
} else { } else {
$testMode = "no"; $testMode = 'no';
} }
$this->message['o:testmode'] = $testMode; $this->message['o:testmode'] = $testMode;
@ -322,8 +340,10 @@ class MessageBuilder
/** /**
* @param string|int $campaignId * @param string|int $campaignId
* @return string|int *
* @throws TooManyParameters * @throws TooManyParameters
*
* @return string|int
*/ */
public function addCampaignId($campaignId) public function addCampaignId($campaignId)
{ {
@ -331,7 +351,7 @@ class MessageBuilder
if (isset($this->message['o:campaign'])) { if (isset($this->message['o:campaign'])) {
array_push($this->message['o:campaign'], $campaignId); array_push($this->message['o:campaign'], $campaignId);
} else { } else {
$this->message['o:campaign'] = array($campaignId); $this->message['o:campaign'] = [$campaignId];
} }
$this->counters['attributes']['campaign_id'] += 1; $this->counters['attributes']['campaign_id'] += 1;
@ -343,6 +363,7 @@ class MessageBuilder
/** /**
* @param string $tag * @param string $tag
*
* @throws TooManyParameters * @throws TooManyParameters
*/ */
public function addTag($tag) public function addTag($tag)
@ -351,7 +372,7 @@ class MessageBuilder
if (isset($this->message['o:tag'])) { if (isset($this->message['o:tag'])) {
array_push($this->message['o:tag'], $tag); array_push($this->message['o:tag'], $tag);
} else { } else {
$this->message['o:tag'] = array($tag); $this->message['o:tag'] = [$tag];
} }
$this->counters['attributes']['tag'] += 1; $this->counters['attributes']['tag'] += 1;
@ -362,31 +383,33 @@ class MessageBuilder
} }
/** /**
* @param boolean $enabled * @param bool $enabled
*
* @return mixed * @return mixed
*/ */
public function setDkim($enabled) public function setDkim($enabled)
{ {
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
$enabled = "yes"; $enabled = 'yes';
} else { } 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 * @return string
*/ */
public function setOpenTracking($enabled) public function setOpenTracking($enabled)
{ {
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
$enabled = "yes"; $enabled = 'yes';
} else { } else {
$enabled = "no"; $enabled = 'no';
} }
$this->message['o:tracking-opens'] = $enabled; $this->message['o:tracking-opens'] = $enabled;
@ -394,17 +417,18 @@ class MessageBuilder
} }
/** /**
* @param boolean $enabled * @param bool $enabled
*
* @return string * @return string
*/ */
public function setClickTracking($enabled) public function setClickTracking($enabled)
{ {
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) { if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
$enabled = "yes"; $enabled = 'yes';
} elseif ($enabled == "html") { } elseif ($enabled == 'html') {
$enabled = "html"; $enabled = 'html';
} else { } else {
$enabled = "no"; $enabled = 'no';
} }
$this->message['o:tracking-clicks'] = $enabled; $this->message['o:tracking-clicks'] = $enabled;
@ -412,8 +436,9 @@ class MessageBuilder
} }
/** /**
* @param string $timeDate * @param string $timeDate
* @param string|null $timeZone * @param string|null $timeZone
*
* @return string * @return string
*/ */
public function setDeliveryTime($timeDate, $timeZone = null) public function setDeliveryTime($timeDate, $timeZone = null)
@ -424,8 +449,8 @@ class MessageBuilder
$timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE); $timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE);
} }
$dateTimeObj = new \DateTime($timeDate, $timeZoneObj); $dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
$formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822); $formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822);
$this->message['o:deliverytime'] = $formattedTimeDate; $this->message['o:deliverytime'] = $formattedTimeDate;
return $this->message['o:deliverytime']; return $this->message['o:deliverytime'];
@ -433,16 +458,17 @@ class MessageBuilder
/** /**
* @param string $customName * @param string $customName
* @param mixed $data * @param mixed $data
*/ */
public function addCustomData($customName, $data) public function addCustomData($customName, $data)
{ {
$this->message['v:' . $customName] = json_encode($data); $this->message['v:'.$customName] = json_encode($data);
} }
/** /**
* @param string $parameterName * @param string $parameterName
* @param mixed $data * @param mixed $data
*
* @return mixed * @return mixed
*/ */
public function addCustomParameter($parameterName, $data) public function addCustomParameter($parameterName, $data)
@ -452,7 +478,7 @@ class MessageBuilder
return $this->message[$parameterName]; return $this->message[$parameterName];
} else { } else {
$this->message[$parameterName] = array($data); $this->message[$parameterName] = [$data];
return $this->message[$parameterName]; return $this->message[$parameterName];
} }

View File

@ -1,5 +1,4 @@
<?php <?php
//Grab the composer Autoloader! //Grab the composer Autoloader!
$autoloader = require dirname(__DIR__) . '/vendor/autoload.php'; $autoloader = require dirname(__DIR__).'/vendor/autoload.php';

View File

@ -1,18 +1,18 @@
<?PHP <?PHP
namespace Mailgun\Tests\Lists; namespace Mailgun\Tests\Lists;
use Mailgun\Tests\Mock\Mailgun; use Mailgun\Tests\Mock\Mailgun;
class OptInHandler extends \Mailgun\Tests\MailgunTestCase class OptInHandlerTest extends \Mailgun\Tests\MailgunTestCase
{ {
private $client; private $client;
private $sampleDomain = "samples.mailgun.org"; private $sampleDomain = 'samples.mailgun.org';
private $optInHandler; private $optInHandler;
public function setUp() public function setUp()
{ {
$this->client = new Mailgun("My-Super-Awesome-API-Key"); $this->client = new Mailgun('My-Super-Awesome-API-Key');
$this->optInHandler = $this->client->OptInHandler(); $this->optInHandler = $this->client->OptInHandler();
} }
@ -23,7 +23,7 @@ class OptInHandler extends \Mailgun\Tests\MailgunTestCase
'mysupersecretappid', 'mysupersecretappid',
'testrecipient@example.com' 'testrecipient@example.com'
); );
$knownHash = "eyJoIjoiMTllODc2YWNkMWRmNzk4NTc0ZTU0YzhjMzIzOTNiYTNjNzdhNGMxOCIsInAiOiJleUp5SWpvaWRHVnpkSEpsWTJsd2FXVnVkRUJsZUdGdGNHeGxMbU52YlNJc0ltd2lPaUp0ZVhSbGMzUnNhWE4wUUdWNFlXMXdiR1V1WTI5dEluMD0ifQ%3D%3D"; $knownHash = 'eyJoIjoiMTllODc2YWNkMWRmNzk4NTc0ZTU0YzhjMzIzOTNiYTNjNzdhNGMxOCIsInAiOiJleUp5SWpvaWRHVnpkSEpsWTJsd2FXVnVkRUJsZUdGdGNHeGxMbU52YlNJc0ltd2lPaUp0ZVhSbGMzUnNhWE4wUUdWNFlXMXdiR1V1WTI5dEluMD0ifQ%3D%3D';
$this->assertEquals($generatedHash, $knownHash); $this->assertEquals($generatedHash, $knownHash);
} }

View File

@ -1,42 +1,45 @@
<?PHP <?PHP
namespace Mailgun\Tests\Lists; namespace Mailgun\Tests\Lists;
use Mailgun\Mailgun; use Mailgun\Mailgun;
class MailgunTest extends \Mailgun\Tests\MailgunTestCase class MailgunTest extends \Mailgun\Tests\MailgunTestCase
{ {
public function testSendMessageMissingRequiredMIMEParametersExceptionGetsFlung() public function testSendMessageMissingRequiredMIMEParametersExceptionGetsFlung()
{ {
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$client = new Mailgun(); $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'); $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$postData = array( $postData = [
'timestamp' => '1403645220', 'timestamp' => '1403645220',
'token' => '5egbgr1vjgqxtrnp65xfznchgdccwh5d6i09vijqi3whgowmn6', 'token' => '5egbgr1vjgqxtrnp65xfznchgdccwh5d6i09vijqi3whgowmn6',
'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33', 'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33',
); ];
assert($client->verifyWebhookSignature($postData)); assert($client->verifyWebhookSignature($postData));
} }
public function testVerifyWebhookBad() { public function testVerifyWebhookBad()
{
$client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$postData = array( $postData = [
'timestamp' => '1403645220', 'timestamp' => '1403645220',
'token' => 'owyldpe6nxhmrn78epljl6bj0orrki1u3d2v5e6cnlmmuox8jr', 'token' => 'owyldpe6nxhmrn78epljl6bj0orrki1u3d2v5e6cnlmmuox8jr',
'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33', 'signature' => '9cfc5c41582e51246e73c88d34db3af0a3a2692a76fbab81492842f000256d33',
); ];
assert(!$client->verifyWebhookSignature($postData)); assert(!$client->verifyWebhookSignature($postData));
} }
public function testVerifyWebhookEmptyRequest() { public function testVerifyWebhookEmptyRequest()
{
$client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0'); $client = new Mailgun('key-3ax6xnjp29jd6fds4gc373sgvjxteol0');
$postData = array(); $postData = [];
assert(!$client->verifyWebhookSignature($postData)); assert(!$client->verifyWebhookSignature($postData));
} }
} }

View File

@ -1,17 +1,17 @@
<?PHP <?PHP
namespace Mailgun\Tests\Messages; namespace Mailgun\Tests\Messages;
use Mailgun\Tests\Mock\Mailgun; use Mailgun\Tests\Mock\Mailgun;
class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase
{ {
private $client; private $client;
private $sampleDomain = "samples.mailgun.org"; private $sampleDomain = 'samples.mailgun.org';
public function setUp() public function setUp()
{ {
$this->client = new Mailgun("My-Super-Awesome-API-Key"); $this->client = new Mailgun('My-Super-Awesome-API-Key');
} }
public function testBlankInstantiation() public function testBlankInstantiation()
@ -23,12 +23,12 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase
public function testAddRecipient() public function testAddRecipient()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['to' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
$reflectionClass = new \ReflectionClass(get_class($message)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('counters'); $property = $reflectionClass->getProperty('counters');
$property->setAccessible(true); $property->setAccessible(true);
$array = $property->getValue($message); $array = $property->getValue($message);
$this->assertEquals(1, $array['recipients']['to']); $this->assertEquals(1, $array['recipients']['to']);
@ -37,78 +37,78 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase
public function testRecipientVariablesOnTo() public function testRecipientVariablesOnTo()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['to' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
$reflectionClass = new \ReflectionClass(get_class($message)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes'); $property = $reflectionClass->getProperty('batchRecipientAttributes');
$property->setAccessible(true); $property->setAccessible(true);
$propertyValue = $property->getValue($message); $propertyValue = $property->getValue($message);
$this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']);
$this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']);
} }
public function testRecipientVariablesOnCc() public function testRecipientVariablesOnCc()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("cc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['cc' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
$reflectionClass = new \ReflectionClass(get_class($message)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes'); $property = $reflectionClass->getProperty('batchRecipientAttributes');
$property->setAccessible(true); $property->setAccessible(true);
$propertyValue = $property->getValue($message); $propertyValue = $property->getValue($message);
$this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']);
$this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']);
} }
public function testRecipientVariablesOnBcc() public function testRecipientVariablesOnBcc()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("bcc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['bcc' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
$reflectionClass = new \ReflectionClass(get_class($message)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes'); $property = $reflectionClass->getProperty('batchRecipientAttributes');
$property->setAccessible(true); $property->setAccessible(true);
$propertyValue = $property->getValue($message); $propertyValue = $property->getValue($message);
$this->assertEquals("Test", $propertyValue['test@samples.mailgun.org']['first']); $this->assertEquals('Test', $propertyValue['test@samples.mailgun.org']['first']);
$this->assertEquals("User", $propertyValue['test@samples.mailgun.org']['last']); $this->assertEquals('User', $propertyValue['test@samples.mailgun.org']['last']);
} }
public function testAddMultipleBatchRecipients() public function testAddMultipleBatchRecipients()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
for ($i = 0; $i < 100; $i++) { 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(); $messageObj = $message->getMessage();
$this->assertEquals(100, count($messageObj["to"])); $this->assertEquals(100, count($messageObj['to']));
} }
public function testMaximumBatchSize() public function testMaximumBatchSize()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']);
$message->setSubject("This is the subject of the message!"); $message->setSubject('This is the subject of the message!');
$message->setTextBody("This is the text body of the message!"); $message->setTextBody('This is the text body of the message!');
for ($i = 0; $i < 1001; $i++) { 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(); $messageObj = $message->getMessage();
$this->assertEquals(1, count($messageObj["to"])); $this->assertEquals(1, count($messageObj['to']));
} }
public function testAttributeResetOnEndBatchMessage() public function testAttributeResetOnEndBatchMessage()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $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']);
$message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']);
$message->setSubject("This is the subject of the message!"); $message->setSubject('This is the subject of the message!');
$message->setTextBody("This is the text body of the message!"); $message->setTextBody('This is the text body of the message!');
$message->finalize(); $message->finalize();
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertTrue(true, empty($messageObj)); $this->assertTrue(true, empty($messageObj));
@ -117,10 +117,10 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase
public function testDefaultIDInVariables() public function testDefaultIDInVariables()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $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)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('batchRecipientAttributes'); $property = $reflectionClass->getProperty('batchRecipientAttributes');
$property->setAccessible(true); $property->setAccessible(true);
$propertyValue = $property->getValue($message); $propertyValue = $property->getValue($message);
$this->assertEquals(1, $propertyValue['test-user@samples.mailgun.org']['id']); $this->assertEquals(1, $propertyValue['test-user@samples.mailgun.org']['id']);
@ -129,44 +129,44 @@ class BatchMessageTest extends \Mailgun\Tests\MailgunTestCase
public function testgetMessageIds() public function testgetMessageIds()
{ {
$message = $this->client->BatchMessage($this->sampleDomain); $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']);
$message->setFromAddress("samples@mailgun.org", array("first" => "Test", "last" => "User")); $message->setFromAddress('samples@mailgun.org', ['first' => 'Test', 'last' => 'User']);
$message->setSubject("This is the subject of the message!"); $message->setSubject('This is the subject of the message!');
$message->setTextBody("This is the text body of the message!"); $message->setTextBody('This is the text body of the message!');
$message->finalize(); $message->finalize();
$this->assertEquals(array("1234"), $message->getMessageIds()); $this->assertEquals(['1234'], $message->getMessageIds());
} }
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom() public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoFrom()
{ {
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage(array(1, 2, 3)); $message->sendMessage([1, 2, 3]);
} }
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo() public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTo()
{ {
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage(array("from" => 1, 2, 3)); $message->sendMessage(['from' => 1, 2, 3]);
} }
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject() public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoSubject()
{ {
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage(array("from" => 1, "to" => 2, 3)); $message->sendMessage(['from' => 1, 'to' => 2, 3]);
} }
public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml() public function testInvalidMissingRequiredMIMEParametersExceptionGetsFlungNoTextOrHtml()
{ {
$this->setExpectedException("\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters"); $this->setExpectedException('\\Mailgun\\Messages\\Exceptions\\MissingRequiredMIMEParameters');
$message = $this->client->BatchMessage($this->sampleDomain); $message = $this->client->BatchMessage($this->sampleDomain);
$message->sendMessage(array("from" => 1, "to" => 2, "subject" => 3)); $message->sendMessage(['from' => 1, 'to' => 2, 'subject' => 3]);
} }
} }

View File

@ -1,4 +1,5 @@
<?PHP <?PHP
namespace Mailgun\Tests\Messages; namespace Mailgun\Tests\Messages;
use Mailgun\Tests\Mock\Mailgun; use Mailgun\Tests\Mock\Mailgun;
@ -23,7 +24,7 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$reflectionClass = new \ReflectionClass(get_class($message)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('counters'); $property = $reflectionClass->getProperty('counters');
$property->setAccessible(true); $property->setAccessible(true);
$propertyValue = $property->getValue($message); $propertyValue = $property->getValue($message);
$this->assertEquals(0, $propertyValue['recipients']['to']); $this->assertEquals(0, $propertyValue['recipients']['to']);
@ -38,34 +39,34 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
public function testAddToRecipient() public function testAddToRecipient()
{ {
$message = $this->client->MessageBuilder(); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("to" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['to' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
} }
public function testAddCcRecipient() public function testAddCcRecipient()
{ {
$message = $this->client->MessageBuilder(); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("cc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['cc' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
} }
public function testAddBccRecipient() public function testAddBccRecipient()
{ {
$message = $this->client->MessageBuilder(); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("bcc" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['bcc' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
} }
public function testToRecipientCount() public function testToRecipientCount()
{ {
$message = $this->client->MessageBuilder(); $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)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('counters'); $property = $reflectionClass->getProperty('counters');
$property->setAccessible(true); $property->setAccessible(true);
$array = $property->getValue($message); $array = $property->getValue($message);
$this->assertEquals(1, $array['recipients']['to']); $this->assertEquals(1, $array['recipients']['to']);
@ -74,10 +75,10 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
public function testCcRecipientCount() public function testCcRecipientCount()
{ {
$message = $this->client->MessageBuilder(); $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)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('counters'); $property = $reflectionClass->getProperty('counters');
$property->setAccessible(true); $property->setAccessible(true);
$array = $property->getValue($message); $array = $property->getValue($message);
$this->assertEquals(1, $array['recipients']['cc']); $this->assertEquals(1, $array['recipients']['cc']);
@ -86,10 +87,10 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
public function testBccRecipientCount() public function testBccRecipientCount()
{ {
$message = $this->client->MessageBuilder(); $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)); $reflectionClass = new \ReflectionClass(get_class($message));
$property = $reflectionClass->getProperty('counters'); $property = $reflectionClass->getProperty('counters');
$property->setAccessible(true); $property->setAccessible(true);
$array = $property->getValue($message); $array = $property->getValue($message);
$this->assertEquals(1, $array['recipients']['bcc']); $this->assertEquals(1, $array['recipients']['bcc']);
@ -98,89 +99,89 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
public function testSetFromAddress() public function testSetFromAddress()
{ {
$message = $this->client->MessageBuilder(); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("from" => array("'Test User' <test@samples.mailgun.org>")), $messageObj); $this->assertEquals(['from' => ["'Test User' <test@samples.mailgun.org>"]], $messageObj);
} }
public function testSetReplyTo() public function testSetReplyTo()
{ {
$message = $this->client->MessageBuilder(); $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(); $messageObj = $message->getMessage();
$this->assertEquals(array("h:reply-to" => "'Test User' <test@samples.mailgun.org>"), $messageObj); $this->assertEquals(['h:reply-to' => "'Test User' <test@samples.mailgun.org>"], $messageObj);
} }
public function testSetSubject() public function testSetSubject()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setSubject("Test Subject"); $message->setSubject('Test Subject');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("subject" => "Test Subject"), $messageObj); $this->assertEquals(['subject' => 'Test Subject'], $messageObj);
} }
public function testAddCustomHeader() public function testAddCustomHeader()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->addCustomHeader("My-Header", "123"); $message->addCustomHeader('My-Header', '123');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("h:My-Header" => array("123")), $messageObj); $this->assertEquals(['h:My-Header' => ['123']], $messageObj);
} }
public function testSetTextBody() public function testSetTextBody()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setTextBody("This is the text body!"); $message->setTextBody('This is the text body!');
$messageObj = $message->getMessage(); $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() public function testSetHtmlBody()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setHtmlBody("<html><body>This is an awesome email</body></html>"); $message->setHtmlBody('<html><body>This is an awesome email</body></html>');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("html" => "<html><body>This is an awesome email</body></html>"), $messageObj); $this->assertEquals(['html' => '<html><body>This is an awesome email</body></html>'], $messageObj);
} }
public function testAddAttachments() public function testAddAttachments()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->addAttachment("@../TestAssets/mailgun_icon.png"); $message->addAttachment('@../TestAssets/mailgun_icon.png');
$message->addAttachment("@../TestAssets/rackspace_logo.png"); $message->addAttachment('@../TestAssets/rackspace_logo.png');
$messageObj = $message->getFiles(); $messageObj = $message->getFiles();
$this->assertEquals( $this->assertEquals(
array( [
array( [
'filePath' => "@../TestAssets/mailgun_icon.png", 'filePath' => '@../TestAssets/mailgun_icon.png',
'remoteName' => null 'remoteName' => null,
), ],
array( [
'filePath' => "@../TestAssets/rackspace_logo.png", 'filePath' => '@../TestAssets/rackspace_logo.png',
'remoteName' => null 'remoteName' => null,
) ],
), ],
$messageObj["attachment"] $messageObj['attachment']
); );
} }
public function testAddInlineImages() public function testAddInlineImages()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->addInlineImage("@../TestAssets/mailgun_icon.png"); $message->addInlineImage('@../TestAssets/mailgun_icon.png');
$message->addInlineImage("@../TestAssets/rackspace_logo.png"); $message->addInlineImage('@../TestAssets/rackspace_logo.png');
$messageObj = $message->getFiles(); $messageObj = $message->getFiles();
$this->assertEquals( $this->assertEquals(
array( [
array( [
'filePath' => "@../TestAssets/mailgun_icon.png", 'filePath' => '@../TestAssets/mailgun_icon.png',
'remoteName' => null 'remoteName' => null,
), ],
array( [
'filePath' => "@../TestAssets/rackspace_logo.png", 'filePath' => '@../TestAssets/rackspace_logo.png',
'remoteName' => null 'remoteName' => null,
) ],
), ],
$messageObj['inline'] $messageObj['inline']
); );
} }
@ -192,17 +193,17 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
$message->addAttachment('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); $message->addAttachment('@../TestAssets/rackspace_logo.png', 'rs_logo.png');
$messageObj = $message->getFiles(); $messageObj = $message->getFiles();
$this->assertEquals( $this->assertEquals(
array( [
array( [
'filePath' => '@../TestAssets/mailgun_icon.png', 'filePath' => '@../TestAssets/mailgun_icon.png',
'remoteName' => 'mg_icon.png' 'remoteName' => 'mg_icon.png',
), ],
array( [
'filePath' => '@../TestAssets/rackspace_logo.png', 'filePath' => '@../TestAssets/rackspace_logo.png',
'remoteName' => 'rs_logo.png' 'remoteName' => 'rs_logo.png',
) ],
), ],
$messageObj["attachment"] $messageObj['attachment']
); );
} }
@ -213,16 +214,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
$message->addInlineImage('@../TestAssets/rackspace_logo.png', 'rs_logo.png'); $message->addInlineImage('@../TestAssets/rackspace_logo.png', 'rs_logo.png');
$messageObj = $message->getFiles(); $messageObj = $message->getFiles();
$this->assertEquals( $this->assertEquals(
array( [
array( [
'filePath' => '@../TestAssets/mailgun_icon.png', 'filePath' => '@../TestAssets/mailgun_icon.png',
'remoteName' => 'mg_icon.png' 'remoteName' => 'mg_icon.png',
), ],
array( [
'filePath' => '@../TestAssets/rackspace_logo.png', 'filePath' => '@../TestAssets/rackspace_logo.png',
'remoteName' => 'rs_logo.png' 'remoteName' => 'rs_logo.png',
) ],
), ],
$messageObj['inline'] $messageObj['inline']
); );
} }
@ -232,27 +233,27 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setTestMode(true); $message->setTestMode(true);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:testmode" => "yes"), $messageObj); $this->assertEquals(['o:testmode' => 'yes'], $messageObj);
$message->setTestMode(false); $message->setTestMode(false);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:testmode" => "no"), $messageObj); $this->assertEquals(['o:testmode' => 'no'], $messageObj);
$message->setTestMode("yes"); $message->setTestMode('yes');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:testmode" => "yes"), $messageObj); $this->assertEquals(['o:testmode' => 'yes'], $messageObj);
$message->setTestMode("no"); $message->setTestMode('no');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:testmode" => "no"), $messageObj); $this->assertEquals(['o:testmode' => 'no'], $messageObj);
} }
public function addCampaignId() public function addCampaignId()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->addCampaignId("ABC123"); $message->addCampaignId('ABC123');
$message->addCampaignId("XYZ987"); $message->addCampaignId('XYZ987');
$message->addCampaignId("TUV456"); $message->addCampaignId('TUV456');
$message->addCampaignId("NONO123"); $message->addCampaignId('NONO123');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:campaign" => array("ABC123", "XYZ987", "TUV456")), $messageObj); $this->assertEquals(['o:campaign' => ['ABC123', 'XYZ987', 'TUV456']], $messageObj);
} }
public function testSetDkim() public function testSetDkim()
@ -260,16 +261,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setDkim(true); $message->setDkim(true);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:dkim" => "yes"), $messageObj); $this->assertEquals(['o:dkim' => 'yes'], $messageObj);
$message->setDkim(false); $message->setDkim(false);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:dkim" => "no"), $messageObj); $this->assertEquals(['o:dkim' => 'no'], $messageObj);
$message->setDkim("yes"); $message->setDkim('yes');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:dkim" => "yes"), $messageObj); $this->assertEquals(['o:dkim' => 'yes'], $messageObj);
$message->setDkim("no"); $message->setDkim('no');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:dkim" => "no"), $messageObj); $this->assertEquals(['o:dkim' => 'no'], $messageObj);
} }
public function testSetClickTracking() public function testSetClickTracking()
@ -277,16 +278,16 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setClickTracking(true); $message->setClickTracking(true);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj); $this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj);
$message->setClickTracking(false); $message->setClickTracking(false);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj); $this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj);
$message->setClickTracking("yes"); $message->setClickTracking('yes');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-clicks" => "yes"), $messageObj); $this->assertEquals(['o:tracking-clicks' => 'yes'], $messageObj);
$message->setClickTracking("no"); $message->setClickTracking('no');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-clicks" => "no"), $messageObj); $this->assertEquals(['o:tracking-clicks' => 'no'], $messageObj);
} }
public function testSetOpenTracking() public function testSetOpenTracking()
@ -294,33 +295,33 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setOpenTracking(true); $message->setOpenTracking(true);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj); $this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj);
$message->setOpenTracking(false); $message->setOpenTracking(false);
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-opens" => "no"), $messageObj); $this->assertEquals(['o:tracking-opens' => 'no'], $messageObj);
$message->setOpenTracking("yes"); $message->setOpenTracking('yes');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-opens" => "yes"), $messageObj); $this->assertEquals(['o:tracking-opens' => 'yes'], $messageObj);
$message->setOpenTracking("no"); $message->setOpenTracking('no');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:tracking-opens" => "no"), $messageObj); $this->assertEquals(['o:tracking-opens' => 'no'], $messageObj);
} }
public function testSetDeliveryTime() public function testSetDeliveryTime()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->setDeliveryTime("January 15, 2014 8:00AM", "CST"); $message->setDeliveryTime('January 15, 2014 8:00AM', 'CST');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 -0600"), $messageObj); $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 -0600'], $messageObj);
$message->setDeliveryTime("January 15, 2014 8:00AM", "UTC"); $message->setDeliveryTime('January 15, 2014 8:00AM', 'UTC');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj); $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj);
$message->setDeliveryTime("January 15, 2014 8:00AM"); $message->setDeliveryTime('January 15, 2014 8:00AM');
$messageObj = $message->getMessage(); $messageObj = $message->getMessage();
$this->assertEquals(array("o:deliverytime" => "Wed, 15 Jan 2014 08:00:00 +0000"), $messageObj); $this->assertEquals(['o:deliverytime' => 'Wed, 15 Jan 2014 08:00:00 +0000'], $messageObj);
$message->setDeliveryTime("1/15/2014 13:50:01", "CDT"); $message->setDeliveryTime('1/15/2014 13:50:01', 'CDT');
$messageObj = $message->getMessage(); $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/pull/42
// https://github.com/mailgun/mailgun-php/issues/43 // https://github.com/mailgun/mailgun-php/issues/43
//$message->setDeliveryTime("first saturday of July 2013 8:00AM", "CDT"); //$message->setDeliveryTime("first saturday of July 2013 8:00AM", "CDT");
@ -331,23 +332,23 @@ class MessageBuilderTest extends \Mailgun\Tests\MailgunTestCase
public function testAddCustomData() public function testAddCustomData()
{ {
$message = $this->client->MessageBuilder(); $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(); $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() public function testAddCustomParameter()
{ {
$message = $this->client->MessageBuilder(); $message = $this->client->MessageBuilder();
$message->addCustomParameter("my-option", "yes"); $message->addCustomParameter('my-option', 'yes');
$message->addCustomParameter("o:my-other-option", "no"); $message->addCustomParameter('o:my-other-option', 'no');
$messageObj = $message->getMessage(); $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() public function testSetMessage()
{ {
$message = array(1, 2, 3, 4, 5); $message = [1, 2, 3, 4, 5];
$messageBuilder = $this->client->MessageBuilder(); $messageBuilder = $this->client->MessageBuilder();
$messageBuilder->setMessage($message); $messageBuilder->setMessage($message);

View File

@ -1,24 +1,24 @@
<?PHP <?PHP
namespace Mailgun\Tests\Messages; namespace Mailgun\Tests\Messages;
use Mailgun\Tests\Mock\Mailgun; use Mailgun\Tests\Mock\Mailgun;
class StandardMessageTest extends \Mailgun\Tests\MailgunTestCase class StandardMessageTest extends \Mailgun\Tests\MailgunTestCase
{ {
private $client; private $client;
private $sampleDomain = "samples.mailgun.org"; private $sampleDomain = 'samples.mailgun.org';
public function setUp() public function setUp()
{ {
$this->client = new Mailgun("My-Super-Awesome-API-Key"); $this->client = new Mailgun('My-Super-Awesome-API-Key');
} }
public function testSendMIMEMessage() 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 Mime-Version: 1.0
Content-Type: text/plain; charset=\"ascii\" Content-Type: text/plain; charset="ascii"
Subject: This is the Subject! Subject: This is the Subject!
From: Mailgun Testing <test@test.mailgun.com> From: Mailgun Testing <test@test.mailgun.com>
To: test@test.mailgun.com To: test@test.mailgun.com
@ -28,21 +28,20 @@ class StandardMessageTest extends \Mailgun\Tests\MailgunTestCase
Date: Mon, 10 Jun 2013 09:50:58 +0000 Date: Mon, 10 Jun 2013 09:50:58 +0000
Sender: test@test.mailgun.com Sender: test@test.mailgun.com
Mailgun is testing!"; Mailgun is testing!';
$envelopeFields = array('to' => 'test@test.mailgun.org'); $envelopeFields = ['to' => 'test@test.mailgun.org'];
$result = $this->client->sendMessage("test.mailgun.org", $envelopeFields, $customMime); $result = $this->client->sendMessage('test.mailgun.org', $envelopeFields, $customMime);
$this->assertEquals("test.mailgun.org/messages.mime", $result->http_endpoint_url); $this->assertEquals('test.mailgun.org/messages.mime', $result->http_endpoint_url);
} }
public function testSendMessage() public function testSendMessage()
{ {
$message = array('to' => 'test@test.mailgun.org', $message = ['to' => 'test@test.mailgun.org',
'from' => 'sender@test.mailgun.org', 'from' => 'sender@test.mailgun.org',
'subject' => 'This is my test subject', 'subject' => 'This is my test subject',
'text' => 'Testing!' 'text' => 'Testing!',
); ];
$result = $this->client->sendMessage("test.mailgun.org", $message); $result = $this->client->sendMessage('test.mailgun.org', $message);
$this->assertEquals("test.mailgun.org/messages", $result->http_endpoint_url); $this->assertEquals('test.mailgun.org/messages', $result->http_endpoint_url);
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Mailgun\Tests\Mock\Connection; namespace Mailgun\Tests\Mock\Connection;
use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\GenericHTTPError;
@ -13,18 +14,18 @@ class TestBroker extends RestClient
protected $apiEndpoint; protected $apiEndpoint;
public function __construct($apiKey = null, $apiHost = "api.mailgun.net", $apiVersion = "v3") public function __construct($apiKey = null, $apiHost = 'api.mailgun.net', $apiVersion = 'v3')
{ {
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
$this->apiEndpoint = $apiHost; $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); return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
} }
public function get($endpointUrl, $queryString = array()) public function get($endpointUrl, $queryString = [])
{ {
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200); return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
} }
@ -42,9 +43,9 @@ class TestBroker extends RestClient
public function testResponseHandler($endpointUrl, $httpResponseCode = 200) public function testResponseHandler($endpointUrl, $httpResponseCode = 200)
{ {
if ($httpResponseCode === 200) { if ($httpResponseCode === 200) {
$result = new \stdClass(); $result = new \stdClass();
$result->http_response_body = 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) { foreach ($jsonResponseData as $key => $value) {
$result->http_response_body->$key = $value; $result->http_response_body->$key = $value;
} }
@ -62,10 +63,8 @@ class TestBroker extends RestClient
return false; return false;
} }
$result->http_response_code = $httpResponseCode; $result->http_response_code = $httpResponseCode;
$result->http_endpoint_url = $endpointUrl; $result->http_endpoint_url = $endpointUrl;
return $result; return $result;
} }
} }

View File

@ -10,7 +10,7 @@ class Mailgun extends Base
protected $debug; protected $debug;
protected $restClient; 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); $this->restClient = new TestBroker($apiKey, $apiEndpoint, $apiVersion);
} }