PHP CS Fixer

This commit is contained in:
David Garcia 2016-07-24 12:41:21 +01:00
parent 09eda3df13
commit 8da94ac190
16 changed files with 174 additions and 154 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Mailgun\Connection\Exceptions; namespace Mailgun\Connection\Exceptions;
class GenericHTTPError extends \Exception class GenericHTTPError extends \Exception
@ -6,7 +7,7 @@ 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) public function __construct($message = null, $response_code = null, $response_body = null, $code = 0, \Exception $previous = null)
{ {
parent::__construct($message, $code, $previous); parent::__construct($message, $code, $previous);
@ -24,5 +25,3 @@ class GenericHTTPError extends \Exception
return $this->httpResponseBody; return $this->httpResponseBody;
} }
} }
?>

View File

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

View File

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

View File

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

View File

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

View File

@ -20,7 +20,7 @@ use Psr\Http\Message\ResponseInterface;
class RestClient class RestClient
{ {
/** /**
* Your API key * Your API key.
* *
* @var string * @var string
*/ */
@ -37,14 +37,14 @@ 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
*/ */
@ -207,11 +207,11 @@ class RestClient
*/ */
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
@ -220,11 +220,11 @@ class RestClient
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());
} }
@ -275,9 +275,7 @@ class RestClient
]; ];
} }
/** /**
*
* @return HttpClient * @return HttpClient
*/ */
protected function getHttpClient() protected function getHttpClient()
@ -299,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
@ -309,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.'/';
} }
/** /**
@ -325,7 +322,7 @@ class RestClient
} }
/** /**
* @param boolean $sslEnabled * @param bool $sslEnabled
* *
* @return RestClient * @return RestClient
*/ */

View File

@ -1,17 +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,22 +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,10 +2,6 @@
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.
@ -14,11 +10,11 @@ use Mailgun\Messages\Expcetions\InvalidParameterType;
*/ */
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)
@ -26,7 +22,7 @@ class OptInHandler
$innerPayload = array('r' => $recipientAddress, 'l' => $mailingList); $innerPayload = array('r' => $recipientAddress, 'l' => $mailingList);
$encodedInnerPayload = base64_encode(json_encode($innerPayload)); $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 = array('h' => $innerHash, 'p' => $encodedInnerPayload);
return urlencode(base64_encode(json_encode($outerPayload))); return urlencode(base64_encode(json_encode($outerPayload)));
@ -35,6 +31,7 @@ class OptInHandler
/** /**
* @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)
@ -45,9 +42,9 @@ class OptInHandler
$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 array('recipientAddress' => $decodedInnerPayload['r'], 'mailingList' => $decodedInnerPayload['l']);
} }

View File

@ -18,7 +18,6 @@ use Mailgun\Messages\MessageBuilder;
*/ */
class Mailgun class Mailgun
{ {
/** /**
* @var RestClient * @var RestClient
*/ */
@ -51,26 +50,26 @@ class Mailgun
* @param string $workingDomain * @param string $workingDomain
* @param array $postData * @param array $postData
* @param array $postFiles * @param array $postFiles
*
* @return \stdClass * @return \stdClass
*
* @throws Exceptions\MissingRequiredMIMEParameters * @throws Exceptions\MissingRequiredMIMEParameters
*/ */
public function sendMessage($workingDomain, $postData, $postFiles = array()) public function sendMessage($workingDomain, $postData, $postFiles = array())
{ {
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, array('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);
} }
} }
@ -86,24 +85,24 @@ 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);
} }
} }
@ -111,6 +110,7 @@ class Mailgun
* @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 = array(), $files = array())
@ -121,6 +121,7 @@ class Mailgun
/** /**
* @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 = array())
@ -130,6 +131,7 @@ class Mailgun
/** /**
* @param string $endpointUrl * @param string $endpointUrl
*
* @return \stdClass * @return \stdClass
*/ */
public function delete($endpointUrl) public function delete($endpointUrl)
@ -140,6 +142,7 @@ class Mailgun
/** /**
* @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)
@ -160,7 +163,7 @@ class Mailgun
} }
/** /**
* @param boolean $sslEnabled * @param bool $sslEnabled
* *
* @return Mailgun * @return Mailgun
*/ */
@ -190,6 +193,7 @@ class Mailgun
/** /**
* @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)

View File

@ -15,14 +15,13 @@ use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
*/ */
class BatchMessage extends MessageBuilder class BatchMessage extends MessageBuilder
{ {
/** /**
* @var array * @var array
*/ */
private $batchRecipientAttributes; private $batchRecipientAttributes;
/** /**
* @var boolean * @var bool
*/ */
private $autoSend; private $autoSend;
@ -49,7 +48,7 @@ class BatchMessage extends MessageBuilder
/** /**
* @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)
{ {
@ -57,21 +56,22 @@ class BatchMessage extends MessageBuilder
$this->autoSend = $autoSend; $this->autoSend = $autoSend;
$this->restClient = $restClient; $this->restClient = $restClient;
$this->workingDomain = $workingDomain; $this->workingDomain = $workingDomain;
$this->endpointUrl = $workingDomain . "/messages"; $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 (array_key_exists($headerName, $this->counters['recipients'])) {
if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) { if ($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT) {
if($this->autoSend == false) { if ($this->autoSend == false) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS); throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS);
} }
$this->sendMessage(); $this->sendMessage();
@ -80,19 +80,17 @@ class BatchMessage extends MessageBuilder
$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 {
else{
$this->message[$headerName] = array($compiledAddress); $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];
} }
} }
@ -102,34 +100,31 @@ class BatchMessage extends MessageBuilder
/** /**
* @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 = array(), $files = array())
{ {
if(count($message) < 1) { if (count($message) < 1) {
$message = $this->message; $message = $this->message;
$files = $this->files; $files = $this->files;
} }
if(!array_key_exists("from", $message)) { if (!array_key_exists('from', $message)) {
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); 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))) {
elseif((!array_key_exists("text", $message) && !array_key_exists("html", $message))) {
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS); throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
} } else {
else{ $message['recipient-variables'] = json_encode($this->batchRecipientAttributes);
$message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
$response = $this->restClient->post($this->endpointUrl, $message, $files); $response = $this->restClient->post($this->endpointUrl, $message, $files);
$this->batchRecipientAttributes = array(); $this->batchRecipientAttributes = array();
$this->counters['recipients']['to'] = 0; $this->counters['recipients']['to'] = 0;
$this->counters['recipients']['cc'] = 0; $this->counters['recipients']['cc'] = 0;
$this->counters['recipients']['bcc'] = 0; $this->counters['recipients']['bcc'] = 0;
unset($this->message["to"]); unset($this->message['to']);
array_push($this->messageIds, $response->http_response_body->id); array_push($this->messageIds, $response->http_response_body->id);
} }
} }

View File

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

View File

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

View File

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

View File

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

View File

@ -39,20 +39,21 @@ class MessageBuilder
'recipients' => array( 'recipients' => array(
'to' => 0, 'to' => 0,
'cc' => 0, 'cc' => 0,
'bcc' => 0 'bcc' => 0,
), ),
'attributes' => array( 'attributes' => array(
'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)
@ -109,7 +112,7 @@ 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] = array($compiledAddress);
@ -122,7 +125,9 @@ class MessageBuilder
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
*
* @return mixed * @return mixed
*
* @throws TooManyParameters * @throws TooManyParameters
*/ */
public function addToRecipient($address, $variables = null) public function addToRecipient($address, $variables = null)
@ -130,7 +135,7 @@ class MessageBuilder
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']);
} }
@ -138,7 +143,9 @@ class MessageBuilder
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
*
* @return mixed * @return mixed
*
* @throws TooManyParameters * @throws TooManyParameters
*/ */
public function addCcRecipient($address, $variables = null) public function addCcRecipient($address, $variables = null)
@ -146,7 +153,7 @@ class MessageBuilder
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']);
} }
@ -154,7 +161,9 @@ class MessageBuilder
/** /**
* @param string $address * @param string $address
* @param array|null $variables * @param array|null $variables
*
* @return mixed * @return mixed
*
* @throws TooManyParameters * @throws TooManyParameters
*/ */
public function addBccRecipient($address, $variables = null) public function addBccRecipient($address, $variables = null)
@ -162,7 +171,7 @@ class MessageBuilder
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']);
} }
@ -170,11 +179,12 @@ class MessageBuilder
/** /**
* @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'];
} }
@ -182,23 +192,25 @@ class MessageBuilder
/** /**
* @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;
@ -208,12 +220,13 @@ 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] = array($headerData);
@ -222,12 +235,13 @@ class MessageBuilder
/** /**
* @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;
@ -251,22 +266,23 @@ 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 = array(
'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(
array( array(
'filePath' => $attachmentPath, 'filePath' => $attachmentPath,
'remoteName' => $attachmentName 'remoteName' => $attachmentName,
) ),
); );
} }
@ -278,6 +294,7 @@ class MessageBuilder
* @param string|null $inlineImageName * @param string|null $inlineImageName
* *
* @return bool|true * @return bool|true
*
* @throws InvalidParameter * @throws InvalidParameter
*/ */
public function addInlineImage($inlineImagePath, $inlineImageName = null) public function addInlineImage($inlineImagePath, $inlineImageName = null)
@ -286,15 +303,15 @@ class MessageBuilder
if (isset($this->files['inline'])) { if (isset($this->files['inline'])) {
$inlineAttachment = array( $inlineAttachment = array(
'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(
array( array(
'filePath' => $inlineImagePath, 'filePath' => $inlineImagePath,
'remoteName' => $inlineImageName 'remoteName' => $inlineImageName,
) ),
); );
} }
@ -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,7 +340,9 @@ class MessageBuilder
/** /**
* @param string|int $campaignId * @param string|int $campaignId
*
* @return string|int * @return string|int
*
* @throws TooManyParameters * @throws TooManyParameters
*/ */
public function addCampaignId($campaignId) public function addCampaignId($campaignId)
@ -343,6 +363,7 @@ class MessageBuilder
/** /**
* @param string $tag * @param string $tag
*
* @throws TooManyParameters * @throws TooManyParameters
*/ */
public function addTag($tag) public function addTag($tag)
@ -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;
@ -414,6 +438,7 @@ 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)
@ -437,12 +462,13 @@ class MessageBuilder
*/ */
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)