mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2025-02-06 08:19:25 +03:00
* Changed class docblock to include link to readme for instructions.
* Added docblocks for functions and member variables. * Updated constants to use constants classes * Changed finalize() - removing the return (since the sendMessage() method already does not return anything)
This commit is contained in:
parent
3e55c2bcd2
commit
7fed60d881
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user