Merge pull request #66 from cerealean/master

PHP DocBlocks, Constants Changes, and Minor Refactors
This commit is contained in:
Travis Swientek 2014-12-02 15:58:50 -08:00
commit fbe34ce59a
8 changed files with 429 additions and 107 deletions

View File

@ -3,35 +3,61 @@
namespace Mailgun\Connection;
use Guzzle\Http\Client as Guzzle;
use Mailgun\MailgunClient;
use Mailgun\Connection\Exceptions\GenericHTTPError;
use Guzzle\Http\QueryAggregator\DuplicateAggregator;
use Guzzle\Http\QueryAggregator\PhpAggregator;
use Mailgun\Connection\Exceptions\InvalidCredentials;
use Mailgun\Connection\Exceptions\NoDomainsConfigured;
use Mailgun\Connection\Exceptions\MissingRequiredParameters;
use Mailgun\Connection\Exceptions\MissingEndpoint;
use Mailgun\Constants\Api;
use Mailgun\Constants\ExceptionMessages;
/*
This class is a wrapper for the Guzzle (HTTP Client Library).
*/
class RestClient{
/**
* This class is a wrapper for the Guzzle (HTTP Client Library).
*/
class RestClient {
/**
* @var string
*/
private $apiKey;
/**
* @var Guzzle
*/
protected $mgClient;
/**
* @var bool
*/
protected $hasFiles = False;
/**
* @param string $apiKey
* @param string $apiEndpoint
* @param string $apiVersion
* @param bool $ssl
*/
public function __construct($apiKey, $apiEndpoint, $apiVersion, $ssl){
$this->apiKey = $apiKey;
$this->mgClient = new Guzzle($this->generateEndpoint($apiEndpoint, $apiVersion, $ssl));
$this->mgClient->setDefaultOption('curl.options', array('CURLOPT_FORBID_REUSE' => true));
$this->mgClient->setDefaultOption('auth', array (API_USER, $this->apiKey));
$this->mgClient->setDefaultOption('auth', array (Api::API_USER, $this->apiKey));
$this->mgClient->setDefaultOption('exceptions', false);
$this->mgClient->setUserAgent(SDK_USER_AGENT . '/' . SDK_VERSION);
$this->mgClient->setUserAgent(Api::SDK_USER_AGENT . '/' . Api::SDK_VERSION);
}
/**
* @param string $endpointUrl
* @param array $postData
* @param array $files
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function post($endpointUrl, $postData = array(), $files = array()){
$request = $this->mgClient->post($endpointUrl, array(), $postData);
@ -90,6 +116,15 @@ class RestClient{
return $this->responseHandler($response);
}
/**
* @param string $endpointUrl
* @param array $queryString
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function get($endpointUrl, $queryString = array()){
$request = $this->mgClient->get($endpointUrl);
if(isset($queryString)){
@ -101,12 +136,29 @@ class RestClient{
return $this->responseHandler($response);
}
/**
* @param string $endpointUrl
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function delete($endpointUrl){
$request = $this->mgClient->delete($endpointUrl);
$response = $request->send();
return $this->responseHandler($response);
}
/**
* @param string $endpointUrl
* @param array $putData
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function put($endpointUrl, $putData){
$request = $this->mgClient->put($endpointUrl, array(), $putData);
$request->getPostFields()->setAggregator(new DuplicateAggregator());
@ -114,6 +166,14 @@ class RestClient{
return $this->responseHandler($response);
}
/**
* @param \Guzzle\Http\Message\Response $responseObj
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function responseHandler($responseObj){
$httpResponseCode = $responseObj->getStatusCode();
if($httpResponseCode === 200){
@ -124,21 +184,27 @@ class RestClient{
$result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
}
elseif($httpResponseCode == 400){
throw new MissingRequiredParameters(EXCEPTION_MISSING_REQUIRED_PARAMETERS);
throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS);
}
elseif($httpResponseCode == 401){
throw new InvalidCredentials(EXCEPTION_INVALID_CREDENTIALS);
throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS);
}
elseif($httpResponseCode == 404){
throw new MissingEndpoint(EXCEPTION_MISSING_ENDPOINT);
throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT);
}
else{
throw new GenericHTTPError(EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
}
$result->http_response_code = $httpResponseCode;
return $result;
}
/**
* @param string $apiEndpoint
* @param string $apiVersion
* @param bool $ssl
* @return string
*/
private function generateEndpoint($apiEndpoint, $apiVersion, $ssl){
if(!$ssl){
return "http://" . $apiEndpoint . "/" . $apiVersion . "/";

View File

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

View File

@ -1,23 +0,0 @@
<?PHP
const API_USER = "api";
const SDK_VERSION = "1.7";
const SDK_USER_AGENT = "mailgun-sdk-php";
const RECIPIENT_COUNT_LIMIT = 1000;
const CAMPAIGN_ID_LIMIT = 3;
const TAG_LIMIT = 3;
const DEFAULT_TIME_ZONE = "UTC";
//Common Exception Messages
const EXCEPTION_INVALID_CREDENTIALS = "Your credentials are incorrect.";
const EXCEPTION_GENERIC_HTTP_ERROR = "An HTTP Error has occurred! Check your network connection and try again.";
const EXCEPTION_MISSING_REQUIRED_PARAMETERS = "The parameters passed to the API were invalid. Check your inputs!";
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 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_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 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_RECIPIENT = "You've exceeded the maximum recipient count (1,000) on the to field with autosend disabled.";

View File

@ -0,0 +1,21 @@
<?php
namespace Mailgun\Constants;
class ExceptionMessages {
const EXCEPTION_INVALID_CREDENTIALS = "Your credentials are incorrect.";
const EXCEPTION_GENERIC_HTTP_ERROR = "An HTTP Error has occurred! Check your network connection and try again.";
const EXCEPTION_MISSING_REQUIRED_PARAMETERS = "The parameters passed to the API were invalid. Check your inputs!";
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 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_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 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_RECIPIENT = "You've exceeded the maximum recipient count (1,000) on the to field with autosend disabled.";
}

View File

@ -6,17 +6,20 @@ 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
mailing list subscription double-opt in requests.
*/
/**
* This class is used for creating a unique hash for
* mailing list subscription double-opt in requests.
*
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Lists/README.md
*/
class OptInHandler{
function __construct(){
}
/**
* @param string $mailingList
* @param string $secretAppId
* @param string $recipientAddress
* @return string
*/
public function generateHash($mailingList, $secretAppId, $recipientAddress){
$innerPayload = array('r' => $recipientAddress, 'l' => $mailingList);
$encodedInnerPayload = base64_encode(json_encode($innerPayload));
@ -27,6 +30,11 @@ class OptInHandler{
return urlencode(base64_encode(json_encode($outerPayload)));
}
/**
* @param string $secretAppId
* @param string $uniqueHash
* @return array|bool
*/
public function validateHash($secretAppId, $uniqueHash){
$decodedOuterPayload = json_decode(base64_decode(urldecode($uniqueHash)), true);

View File

@ -2,36 +2,53 @@
namespace Mailgun;
require_once 'Constants/Constants.php';
use Mailgun\Messages\Messages;
use Mailgun\Constants\ExceptionMessages;
use Mailgun\Messages\Exceptions;
use Mailgun\Connection\RestClient;
use Mailgun\Messages\BatchMessage;
use Mailgun\Lists\OptInHandler;
use Mailgun\Messages\MessageBuilder;
/*
This class is the base class for the Mailgun SDK.
See the official documentation for usage instructions.
*/
/**
* This class is the base class for the Mailgun SDK.
* See the official documentation (link below) for usage instructions.
*
* @link https://github.com/mailgun/mailgun-php/blob/master/README.md
*/
class Mailgun{
protected $workingDomain;
/**
* @var RestClient
*/
protected $restClient;
/**
* @var null|string
*/
protected $apiKey;
/**
* @param string|null $apiKey
* @param string $apiEndpoint
* @param string $apiVersion
* @param bool $ssl
*/
public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v2", $ssl = true){
$this->apiKey = $apiKey;
$this->restClient = new RestClient($apiKey, $apiEndpoint, $apiVersion, $ssl);
}
/**
* This function allows the sending of a fully formed message OR a custom
* MIME string. If sending MIME, the string must be passed in to the 3rd
* position of the function call.
*
* @param string $workingDomain
* @param array $postData
* @param array $postFiles
* @throws Exceptions\MissingRequiredMIMEParameters
*/
public function sendMessage($workingDomain, $postData, $postFiles = array()){
/*
* This function allows the sending of a fully formed message OR a custom
* MIME string. If sending MIME, the string must be passed in to the 3rd
* position of the function call.
*/
if(is_array($postFiles)){
return $this->post("$workingDomain/messages", $postData, $postFiles);
}
@ -47,21 +64,24 @@ class Mailgun{
return $result;
}
else{
throw new Exceptions\MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
throw new Exceptions\MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
}
/**
* This function checks the signature in a POST request to see if it is
* authentic.
*
* Pass an array of parameters. If you pass nothing, $_POST will be
* used instead.
*
* If this function returns FALSE, you must not process the request.
* You should reject the request with status code 403 Forbidden.
*
* @param array|null $postData
* @return bool
*/
public function verifyWebhookSignature($postData = NULL) {
/*
* This function checks the signature in a POST request to see if it is
* authentic.
*
* Pass an array of parameters. If you pass nothing, $_POST will be
* used instead.
*
* If this function returns FALSE, you must not process the request.
* You should reject the request with status code 403 Forbidden.
*/
if(is_null($postData)) {
$postData = $_POST;
}
@ -76,30 +96,61 @@ class Mailgun{
}
}
/**
* @param string $endpointUrl
* @param array $postData
* @param array $files
* @return \stdClass
*/
public function post($endpointUrl, $postData = array(), $files = array()){
return $this->restClient->post($endpointUrl, $postData, $files);
}
/**
* @param string $endpointUrl
* @param array $queryString
* @return \stdClass
*/
public function get($endpointUrl, $queryString = array()){
return $this->restClient->get($endpointUrl, $queryString);
}
/**
* @param string $endpointUrl
* @return \stdClass
*/
public function delete($endpointUrl){
return $this->restClient->delete($endpointUrl);
}
/**
* @param string $endpointUrl
* @param array $putData
* @return \stdClass
*/
public function put($endpointUrl, $putData){
return $this->restClient->put($endpointUrl, $putData);
}
/**
* @return MessageBuilder
*/
public function MessageBuilder(){
return new MessageBuilder();
}
/**
* @return OptInHandler
*/
public function OptInHandler(){
return new OptInHandler();
}
/**
* @param string $workingDomain
* @param bool $autoSend
* @return BatchMessage
*/
public function BatchMessage($workingDomain, $autoSend = true){
return new BatchMessage($this->restClient, $workingDomain, $autoSend);
}

View File

@ -2,23 +2,49 @@
namespace Mailgun\Messages;
use Mailgun\Messages\MessageBuilder;
use Mailgun\Constants\Api;
use Mailgun\Constants\ExceptionMessages;
use Mailgun\Messages\Exceptions\TooManyParameters;
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
/*
This class is used for batch sending. See the official documentation
for usage instructions.
*/
/**
* This class is used for batch sending. See the official documentation (link below)
* for usage instructions.
*
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
*/
class BatchMessage extends MessageBuilder{
/**
* @var array
*/
private $batchRecipientAttributes;
/**
* @var boolean
*/
private $autoSend;
/**
* @var \Mailgun\Connection\RestClient
*/
private $restClient;
/**
* @var string
*/
private $workingDomain;
/**
* @var array
*/
private $messageIds = array();
/**
* @param \Mailgun\Connection\RestClient $restClient
* @param string $workingDomain
* @param boolean $autoSend
*/
public function __construct($restClient, $workingDomain, $autoSend){
$this->batchRecipientAttributes = array();
$this->autoSend = $autoSend;
@ -27,11 +53,18 @@ class BatchMessage extends MessageBuilder{
$this->endpointUrl = $workingDomain . "/messages";
}
/**
* @param string $headerName
* @param string $address
* @param array $variables
* @throws MissingRequiredMIMEParameters
* @throws TooManyParameters
*/
protected function addRecipient($headerName, $address, $variables){
if(array_key_exists($headerName, $this->counters['recipients'])){
if($this->counters['recipients'][$headerName] == RECIPIENT_COUNT_LIMIT){
if($this->counters['recipients'][$headerName] == Api::RECIPIENT_COUNT_LIMIT){
if($this->autoSend == false){
throw new TooManyParameters(TOO_MANY_RECIPIENTS);
throw new TooManyParameters(ExceptionMessages::TOO_MANY_RECIPIENTS);
}
$this->sendMessage();
}
@ -58,22 +91,27 @@ class BatchMessage extends MessageBuilder{
$this->batchRecipientAttributes["$address"] = $variables;
}
/**
* @param array $message
* @param array $files
* @throws MissingRequiredMIMEParameters
*/
public function sendMessage($message = array(), $files = array()){
if(count($message) < 1){
$message = $this->message;
$files = $this->files;
}
if(!array_key_exists("from", $message)){
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif(!array_key_exists("to", $message)){
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
elseif(!array_key_exists("subject", $message)){
throw new MissingRequiredMIMEParameters(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(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
throw new MissingRequiredMIMEParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
}
else{
$message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
@ -87,10 +125,16 @@ class BatchMessage extends MessageBuilder{
}
}
/**
* @throws MissingRequiredMIMEParameters
*/
public function finalize(){
return $this->sendMessage();
$this->sendMessage();
}
/**
* @return string[]
*/
public function getMessageIds(){
return $this->messageIds;
}

View File

@ -2,23 +2,39 @@
namespace Mailgun\Messages;
use Mailgun\Constants\Api;
use Mailgun\Constants\ExceptionMessages;
use Mailgun\Messages\Exceptions\InvalidParameter;
use Mailgun\Messages\Exceptions\TooManyParameters;
use Mailgun\Messages\Exceptions\InvalidParameterType;
/*
This class is used for composing a properly formed
message object. Dealing with arrays can be cumbersome,
this class makes the process easier. See the official
documentation for usage instructions.
*/
/**
* This class is used for composing a properly formed
* message object. Dealing with arrays can be cumbersome,
* this class makes the process easier. See the official
* documentation (link below) for usage instructions.
*
* @link https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Messages/README.md
*/
class MessageBuilder
{
/**
* @var array
*/
protected $message = array();
/**
* @var array
*/
protected $variables = array();
/**
* @var array
*/
protected $files = array();
/**
* @var array
*/
protected $counters = array(
'recipients' => array(
'to' => 0,
@ -33,6 +49,12 @@ class MessageBuilder
)
);
/**
* @param array $params
* @param string $key
* @param mixed $default
* @return mixed
*/
protected function safeGet($params, $key, $default)
{
if (array_key_exists($key, $params)) {
@ -42,6 +64,10 @@ class MessageBuilder
return $default;
}
/**
* @param array $params
* @return mixed|string
*/
protected function getFullName($params)
{
if (array_key_exists("first", $params)) {
@ -54,6 +80,11 @@ class MessageBuilder
return $this->safeGet($params, "full_name", "");
}
/**
* @param string $address
* @param array $variables
* @return string
*/
protected function parseAddress($address, $variables)
{
if (!is_array($variables)) {
@ -67,6 +98,11 @@ class MessageBuilder
return $address;
}
/**
* @param string $headerName
* @param string $address
* @param array $variables
*/
protected function addRecipient($headerName, $address, $variables)
{
$compiledAddress = $this->parseAddress($address, $variables);
@ -83,36 +119,59 @@ class MessageBuilder
}
}
/**
* @param string $address
* @param array|null $variables
* @return mixed
* @throws TooManyParameters
*/
public function addToRecipient($address, $variables = null)
{
if ($this->counters['recipients']['to'] > RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
if ($this->counters['recipients']['to'] > Api::RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
}
$this->addRecipient("to", $address, $variables);
return end($this->message['to']);
}
/**
* @param string $address
* @param array|null $variables
* @return mixed
* @throws TooManyParameters
*/
public function addCcRecipient($address, $variables = null)
{
if ($this->counters['recipients']['cc'] > RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
if ($this->counters['recipients']['cc'] > Api::RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
}
$this->addRecipient("cc", $address, $variables);
return end($this->message['cc']);
}
/**
* @param string $address
* @param array|null $variables
* @return mixed
* @throws TooManyParameters
*/
public function addBccRecipient($address, $variables = null)
{
if ($this->counters['recipients']['bcc'] > RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(TOO_MANY_PARAMETERS_RECIPIENT);
if ($this->counters['recipients']['bcc'] > Api::RECIPIENT_COUNT_LIMIT) {
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_RECIPIENT);
}
$this->addRecipient("bcc", $address, $variables);
return end($this->message['bcc']);
}
/**
* @param string $address
* @param array|null $variables
* @return mixed
*/
public function setFromAddress($address, $variables = null)
{
$this->addRecipient("from", $address, $variables);
@ -120,6 +179,11 @@ class MessageBuilder
return $this->message['from'];
}
/**
* @param string $address
* @param array|null $variables
* @return mixed
*/
public function setReplyToAddress($address, $variables = null)
{
$this->addRecipient("h:reply-to", $address, $variables);
@ -127,7 +191,11 @@ class MessageBuilder
return $this->message['h:reply-to'];
}
public function setSubject($subject = null)
/**
* @param string $subject
* @return mixed
*/
public function setSubject($subject = "")
{
if ($subject == null || $subject == "") {
$subject = " ";
@ -137,6 +205,11 @@ class MessageBuilder
return $this->message['subject'];
}
/**
* @param string $headerName
* @param mixed $headerData
* @return mixed
*/
public function addCustomHeader($headerName, $headerData)
{
if (!preg_match("/^h:/i", $headerName)) {
@ -147,6 +220,10 @@ class MessageBuilder
return $this->message[$headerName];
}
/**
* @param string $textBody
* @return string
*/
public function setTextBody($textBody)
{
if ($textBody == null || $textBody == "") {
@ -157,6 +234,10 @@ class MessageBuilder
return $this->message['text'];
}
/**
* @param string $htmlBody
* @return string
*/
public function setHtmlBody($htmlBody)
{
if ($htmlBody == null || $htmlBody == "") {
@ -167,6 +248,11 @@ class MessageBuilder
return $this->message['html'];
}
/**
* @param string $attachmentPath
* @param string|null $attachmentName
* @return bool
*/
public function addAttachment($attachmentPath, $attachmentName = null)
{
if (isset($this->files["attachment"])) {
@ -187,6 +273,11 @@ class MessageBuilder
return true;
}
/**
* @param string $inlineImagePath
* @param string|null $inlineImageName
* @throws InvalidParameter
*/
public function addInlineImage($inlineImagePath, $inlineImageName = null)
{
if (preg_match("/^@/", $inlineImagePath)) {
@ -207,10 +298,14 @@ class MessageBuilder
return true;
} else {
throw new InvalidParameter(INVALID_PARAMETER_INLINE);
throw new InvalidParameter(ExceptionMessages::INVALID_PARAMETER_INLINE);
}
}
/**
* @param boolean $testMode
* @return string
*/
public function setTestMode($testMode)
{
if (filter_var($testMode, FILTER_VALIDATE_BOOLEAN)) {
@ -223,9 +318,14 @@ class MessageBuilder
return $this->message['o:testmode'];
}
/**
* @param string|int $campaignId
* @return string|int
* @throws TooManyParameters
*/
public function addCampaignId($campaignId)
{
if ($this->counters['attributes']['campaign_id'] < CAMPAIGN_ID_LIMIT) {
if ($this->counters['attributes']['campaign_id'] < Api::CAMPAIGN_ID_LIMIT) {
if (isset($this->message['o:campaign'])) {
array_push($this->message['o:campaign'], $campaignId);
} else {
@ -235,13 +335,17 @@ class MessageBuilder
return $this->message['o:campaign'];
} else {
throw new TooManyParameters(TOO_MANY_PARAMETERS_CAMPAIGNS);
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_CAMPAIGNS);
}
}
/**
* @param string $tag
* @throws TooManyParameters
*/
public function addTag($tag)
{
if ($this->counters['attributes']['tag'] < TAG_LIMIT) {
if ($this->counters['attributes']['tag'] < Api::TAG_LIMIT) {
if (isset($this->message['o:tag'])) {
array_push($this->message['o:tag'], $tag);
} else {
@ -251,10 +355,14 @@ class MessageBuilder
return $this->message['o:tag'];
} else {
throw new TooManyParameters(TOO_MANY_PARAMETERS_TAGS);
throw new TooManyParameters(ExceptionMessages::TOO_MANY_PARAMETERS_TAGS);
}
}
/**
* @param boolean $enabled
* @return mixed
*/
public function setDkim($enabled)
{
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
@ -267,6 +375,10 @@ class MessageBuilder
return $this->message["o:dkim"];
}
/**
* @param boolean $enabled
* @return string
*/
public function setOpenTracking($enabled)
{
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
@ -279,6 +391,10 @@ class MessageBuilder
return $this->message['o:tracking-opens'];
}
/**
* @param boolean $enabled
* @return string
*/
public function setClickTracking($enabled)
{
if (filter_var($enabled, FILTER_VALIDATE_BOOLEAN)) {
@ -293,12 +409,17 @@ class MessageBuilder
return $this->message['o:tracking-clicks'];
}
/**
* @param string $timeDate
* @param string|null $timeZone
* @return string
*/
public function setDeliveryTime($timeDate, $timeZone = null)
{
if (isset($timeZone)) {
$timeZoneObj = new \DateTimeZone("$timeZone");
} else {
$timeZoneObj = new \DateTimeZone(\DEFAULT_TIME_ZONE);
$timeZoneObj = new \DateTimeZone(Api::DEFAULT_TIME_ZONE);
}
$dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
@ -308,11 +429,20 @@ class MessageBuilder
return $this->message['o:deliverytime'];
}
/**
* @param string $customName
* @param mixed $data
*/
public function addCustomData($customName, $data)
{
$this->message['v:' . $customName] = json_encode($data);
}
/**
* @param string $parameterName
* @param mixed $data
* @return mixed
*/
public function addCustomParameter($parameterName, $data)
{
if (isset($this->message[$parameterName])) {
@ -326,16 +456,25 @@ class MessageBuilder
}
}
/**
* @param array $message
*/
public function setMessage($message)
{
$this->message = $message;
}
/**
* @return array
*/
public function getMessage()
{
return $this->message;
}
/**
* @return array
*/
public function getFiles()
{
return $this->files;