Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
50 / 50 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| Queue | |
100.00% |
50 / 50 |
|
100.00% |
11 / 11 |
28 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| setRecipients | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| addRecipients | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| addRecipient | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| setMessages | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| addMessages | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| addMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getMessages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRecipients | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPreparedMessages | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| prepare | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
7 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <dev@noladev.com> |
| 8 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Mail; |
| 16 | |
| 17 | /** |
| 18 | * Mail queue class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Mail |
| 22 | * @author Nick Sagona, III <dev@noladev.com> |
| 23 | * @copyright Copyright (c) 2009-2027 NOLA Interactive, LLC. |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | class Queue |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Recipients |
| 32 | * @var array |
| 33 | */ |
| 34 | protected array $recipients = []; |
| 35 | |
| 36 | /** |
| 37 | * Messages |
| 38 | * @var array |
| 39 | */ |
| 40 | protected array $messages = []; |
| 41 | |
| 42 | /** |
| 43 | * Prepared messages |
| 44 | * @var array |
| 45 | */ |
| 46 | protected array $prepared = []; |
| 47 | |
| 48 | /** |
| 49 | * Constructor |
| 50 | * |
| 51 | * Instantiate the mail queue object |
| 52 | */ |
| 53 | public function __construct() |
| 54 | { |
| 55 | $args = func_get_args(); |
| 56 | |
| 57 | foreach ($args as $arg) { |
| 58 | if ($arg instanceof Message) { |
| 59 | $this->addMessage($arg); |
| 60 | } else if (is_array($arg)) { |
| 61 | $keys = array_keys($arg); |
| 62 | if (is_numeric($keys[0])) { |
| 63 | $this->addRecipients($arg); |
| 64 | } else { |
| 65 | $this->addRecipient($arg); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set (and clear) recipients in the queue |
| 73 | * |
| 74 | * @param array $recipients |
| 75 | * @return Queue |
| 76 | */ |
| 77 | public function setRecipients(array $recipients): Queue |
| 78 | { |
| 79 | $this->recipients = []; |
| 80 | foreach ($recipients as $recipient) { |
| 81 | $this->addRecipient($recipient); |
| 82 | } |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Add recipients to the queue |
| 88 | * |
| 89 | * @param array $recipients |
| 90 | * @return Queue |
| 91 | */ |
| 92 | public function addRecipients(array $recipients): Queue |
| 93 | { |
| 94 | foreach ($recipients as $recipient) { |
| 95 | $this->addRecipient($recipient); |
| 96 | } |
| 97 | return $this; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Add a recipient to the queue |
| 102 | * |
| 103 | * $recipient = [ |
| 104 | * 'email' => 'me@domain.com', // Required |
| 105 | * 'name' => 'My Name', // Everything else is optional for individual message customization |
| 106 | * 'company' => 'My Company' |
| 107 | * ] |
| 108 | * |
| 109 | * @param array $recipient |
| 110 | * @throws Exception |
| 111 | * @return Queue |
| 112 | */ |
| 113 | public function addRecipient(array $recipient): Queue |
| 114 | { |
| 115 | if (!isset($recipient['email'])) { |
| 116 | throw new Exception("Error: The recipient's array must contain at least an 'email' key."); |
| 117 | } |
| 118 | if (!in_array($recipient, $this->recipients, true)) { |
| 119 | $this->recipients[] = $recipient; |
| 120 | } |
| 121 | |
| 122 | return $this; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set (and clear) messages in the queue |
| 127 | * |
| 128 | * @param array $messages |
| 129 | * @return Queue |
| 130 | */ |
| 131 | public function setMessages(array $messages): Queue |
| 132 | { |
| 133 | $this->messages = []; |
| 134 | foreach ($messages as $message) { |
| 135 | $this->addMessage($message); |
| 136 | } |
| 137 | return $this; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Add messages to the queue |
| 142 | * |
| 143 | * @param array $messages |
| 144 | * @return Queue |
| 145 | */ |
| 146 | public function addMessages(array $messages): Queue |
| 147 | { |
| 148 | foreach ($messages as $message) { |
| 149 | $this->addMessage($message); |
| 150 | } |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Add a message to the queue |
| 156 | * |
| 157 | * @param Message $message |
| 158 | * @return Queue |
| 159 | */ |
| 160 | public function addMessage(Message $message): Queue |
| 161 | { |
| 162 | if (!in_array($message, $this->messages, true)) { |
| 163 | $this->messages[] = $message; |
| 164 | } |
| 165 | return $this; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Get messages |
| 170 | * |
| 171 | * @return array |
| 172 | */ |
| 173 | public function getMessages(): array |
| 174 | { |
| 175 | return $this->messages; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Get recipients |
| 180 | * |
| 181 | * @return array |
| 182 | */ |
| 183 | public function getRecipients(): array |
| 184 | { |
| 185 | return $this->recipients; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Get prepared messages |
| 190 | * |
| 191 | * @return array |
| 192 | */ |
| 193 | public function getPreparedMessages(): array |
| 194 | { |
| 195 | return $this->prepared; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Prepare queue for sending messages |
| 200 | * |
| 201 | * @return array |
| 202 | */ |
| 203 | public function prepare(): array |
| 204 | { |
| 205 | foreach ($this->recipients as $recipient) { |
| 206 | foreach ($this->messages as $message) { |
| 207 | $to = (isset($recipient['name'])) ? [$recipient['email'] => $recipient['name']] : $recipient['email']; |
| 208 | |
| 209 | $msg = clone $message; |
| 210 | $msg->setTo($to); |
| 211 | |
| 212 | foreach ($msg->getParts() as $i => $part) { |
| 213 | if (!($part instanceof Message\Attachment)) { |
| 214 | $subject = $msg->getSubject(); |
| 215 | $content = $part->getContent(); |
| 216 | foreach ($recipient as $key => $value) { |
| 217 | $subject = str_replace('[{' . $key . '}]', $value, $subject); |
| 218 | $content = str_replace('[{' . $key . '}]', $value, $content); |
| 219 | } |
| 220 | $msg->setSubject($subject); |
| 221 | $msg->getPart($i)->setContent($content); |
| 222 | } |
| 223 | } |
| 224 | $this->prepared[] = $msg; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return $this->prepared; |
| 229 | } |
| 230 | |
| 231 | } |