Exceptions
7 years ago
BatchMessage.php
7 years ago
MessageBuilder.php
7 years ago
README.md
7 years ago
BatchMessage.php
140 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2013 Mailgun |
| 5 | * |
| 6 | * This software may be modified and distributed under the terms |
| 7 | * of the MIT license. See the LICENSE file for details. |
| 8 | */ |
| 9 | |
| 10 | namespace Mailgun\Message; |
| 11 | |
| 12 | use Mailgun\Api\Message; |
| 13 | use Mailgun\Message\Exceptions\MissingRequiredParameter; |
| 14 | use Mailgun\Message\Exceptions\RuntimeException; |
| 15 | use Mailgun\Message\Exceptions\TooManyRecipients; |
| 16 | |
| 17 | /** |
| 18 | * This class is used for batch sending. See the official documentation (link below) |
| 19 | * for usage instructions. |
| 20 | * |
| 21 | * @see https://github.com/mailgun/mailgun-php/blob/master/src/Mailgun/Message/README.md |
| 22 | */ |
| 23 | class BatchMessage extends MessageBuilder |
| 24 | { |
| 25 | /** |
| 26 | * @var array |
| 27 | */ |
| 28 | private $batchRecipientAttributes = []; |
| 29 | |
| 30 | /** |
| 31 | * @var bool |
| 32 | */ |
| 33 | private $autoSend = true; |
| 34 | |
| 35 | /** |
| 36 | * @var array |
| 37 | */ |
| 38 | private $messageIds = []; |
| 39 | |
| 40 | /** |
| 41 | * @var string |
| 42 | */ |
| 43 | private $domain; |
| 44 | |
| 45 | /** |
| 46 | * @var Message |
| 47 | */ |
| 48 | private $api; |
| 49 | |
| 50 | /** |
| 51 | * @param Message $messageApi |
| 52 | * @param string $domain |
| 53 | * @param bool $autoSend |
| 54 | */ |
| 55 | public function __construct(Message $messageApi, $domain, $autoSend) |
| 56 | { |
| 57 | $this->api = $messageApi; |
| 58 | $this->domain = $domain; |
| 59 | $this->autoSend = $autoSend; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @param string $headerName |
| 64 | * @param string $address |
| 65 | * @param array $variables { |
| 66 | * |
| 67 | * @var string $id |
| 68 | * @var string $full_name |
| 69 | * @var string $first |
| 70 | * @var string $last |
| 71 | * } |
| 72 | * |
| 73 | * @throws MissingRequiredParameter |
| 74 | * @throws TooManyRecipients |
| 75 | * |
| 76 | * @return BatchMessage |
| 77 | */ |
| 78 | protected function addRecipient($headerName, $address, array $variables) |
| 79 | { |
| 80 | if (array_key_exists($headerName, $this->counters['recipients'])) { |
| 81 | if ($this->counters['recipients'][$headerName] === self::RECIPIENT_COUNT_LIMIT) { |
| 82 | if (false === $this->autoSend) { |
| 83 | throw TooManyRecipients::whenAutoSendDisabled(); |
| 84 | } |
| 85 | $this->finalize(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | parent::addRecipient($headerName, $address, $variables); |
| 90 | |
| 91 | if (array_key_exists($headerName, $this->counters['recipients']) && !array_key_exists('id', $variables)) { |
| 92 | $variables['id'] = $headerName.'_'.$this->counters['recipients'][$headerName]; |
| 93 | } |
| 94 | |
| 95 | $this->batchRecipientAttributes[(string) $address] = $variables; |
| 96 | |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @throws RuntimeException |
| 102 | * @throws MissingRequiredParameter |
| 103 | */ |
| 104 | public function finalize() |
| 105 | { |
| 106 | $message = $this->message; |
| 107 | |
| 108 | if (empty($this->domain)) { |
| 109 | throw new RuntimeException('You must call BatchMessage::setDomain before sending messages.'); |
| 110 | } elseif (empty($message['from'])) { |
| 111 | throw MissingRequiredParameter::create('from'); |
| 112 | } elseif (empty($message['to'])) { |
| 113 | throw MissingRequiredParameter::create('to'); |
| 114 | } elseif (empty($message['subject'])) { |
| 115 | throw MissingRequiredParameter::create('subject'); |
| 116 | } elseif (empty($message['text']) && empty($message['html'])) { |
| 117 | throw MissingRequiredParameter::create('text" or "html'); |
| 118 | } else { |
| 119 | $message['recipient-variables'] = json_encode($this->batchRecipientAttributes); |
| 120 | $response = $this->api->send($this->domain, $message); |
| 121 | |
| 122 | $this->batchRecipientAttributes = []; |
| 123 | $this->counters['recipients']['to'] = 0; |
| 124 | $this->counters['recipients']['cc'] = 0; |
| 125 | $this->counters['recipients']['bcc'] = 0; |
| 126 | unset($this->message['to']); |
| 127 | |
| 128 | $this->messageIds[] = $response->getId(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @return string[] |
| 134 | */ |
| 135 | public function getMessageIds() |
| 136 | { |
| 137 | return $this->messageIds; |
| 138 | } |
| 139 | } |
| 140 |