1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the theroadbunch/mandrill-sdk package. |
5
|
|
|
* |
6
|
|
|
* (c) Dan McAdams <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace RoadBunch\Mandrill\Message; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Dispatcher |
16
|
|
|
* |
17
|
|
|
* @author Dan McAdams |
18
|
|
|
* @package RoadBunch\Mandrill\Message |
19
|
|
|
*/ |
20
|
|
|
class Dispatcher implements MessageDispatcherInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var \Mandrill_Messages $service |
24
|
|
|
*/ |
25
|
|
|
protected $service; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* the name of the dedicated ip pool that should be used to send the message. |
29
|
|
|
* If you do not have any dedicated IPs, this parameter has no effect. |
30
|
|
|
* If you specify a pool that does not exist, your default pool will be used instead. |
31
|
|
|
* |
32
|
|
|
* @var string $ipPool |
33
|
|
|
*/ |
34
|
|
|
protected $ipPool; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* enable a background sending mode that is optimized for bulk sending. |
38
|
|
|
* In async mode, messages/send will immediately return a status of "queued" for every recipient. |
39
|
|
|
* To handle rejections when sending in async mode, set up a webhook for the 'reject' event. |
40
|
|
|
* Defaults to false for messages with no more than 10 recipients; |
41
|
|
|
* messages with more than 10 recipients are always sent asynchronously, regardless of the value of async. |
42
|
|
|
* |
43
|
|
|
* @var bool $async |
44
|
|
|
*/ |
45
|
|
|
protected $async; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Dispatcher constructor. |
49
|
|
|
* |
50
|
|
|
* @param \Mandrill_Messages $service |
51
|
|
|
*/ |
52
|
|
|
public function __construct(\Mandrill_Messages $service) |
53
|
|
|
{ |
54
|
|
|
$this->service = $service; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $ipPool |
59
|
|
|
*/ |
60
|
|
|
public function setIpPool(string $ipPool) |
61
|
|
|
{ |
62
|
|
|
$this->ipPool = $ipPool; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
*/ |
68
|
|
|
public function clearIpPool() |
69
|
|
|
{ |
70
|
|
|
$this->ipPool = null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Message $message |
75
|
|
|
* |
76
|
|
|
* @return SendResponse[] |
77
|
|
|
*/ |
78
|
|
|
public function send(Message $message): array |
79
|
|
|
{ |
80
|
|
|
return $this->sendMessage($message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Message $message |
85
|
|
|
* @param \DateTime $sendAt |
86
|
|
|
* when this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. |
87
|
|
|
* If you specify a time in the past, the message will be sent immediately. |
88
|
|
|
* An additional fee applies for scheduled email, and this feature is only available to accounts with a |
89
|
|
|
* positive balance. |
90
|
|
|
* |
91
|
|
|
* @return SendResponse[] |
92
|
|
|
*/ |
93
|
|
|
public function sendAt(Message $message, \DateTime $sendAt): array |
94
|
|
|
{ |
95
|
|
|
return $this->sendMessage($message, $this->formatDate($sendAt)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Message $message |
100
|
|
|
* @param string|null $sendAt |
101
|
|
|
* |
102
|
|
|
* @return SendResponse[] |
103
|
|
|
*/ |
104
|
|
|
private function sendMessage(Message $message, string $sendAt = null): array |
105
|
|
|
{ |
106
|
|
|
/** @noinspection PhpParamsInspection ignore error warning because Mandrill used \struct in their docblock */ |
107
|
|
|
return $this->buildResponse($this->service->send($message->toArray(), $this->async, $this->ipPool, $sendAt)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param TemplateMessage $message |
112
|
|
|
* |
113
|
|
|
* @return SendResponse[] |
114
|
|
|
*/ |
115
|
|
|
public function sendTemplate(TemplateMessage $message): array |
116
|
|
|
{ |
117
|
|
|
return $this->sendTemplateMessage($message); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param TemplateMessage $message |
122
|
|
|
* @param \DateTime $sendAt |
123
|
|
|
* when this message should be sent as a UTC timestamp in YYYY-MM-DD HH:MM:SS format. |
124
|
|
|
* If you specify a time in the past, the message will be sent immediately. |
125
|
|
|
* An additional fee applies for scheduled email, and this feature is only available to accounts with a |
126
|
|
|
* positive balance. |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function sendTemplateAt(TemplateMessage $message, \DateTime $sendAt): array |
131
|
|
|
{ |
132
|
|
|
return $this->sendTemplateMessage($message, $this->formatDate($sendAt)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param TemplateMessage $message |
137
|
|
|
* @param string|null $sendAt |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
private function sendTemplateMessage(TemplateMessage $message, string $sendAt = null) |
142
|
|
|
{ |
143
|
|
|
/** @noinspection PhpParamsInspection ignore error warning because Mandrill used \struct in their docblock */ |
144
|
|
|
return $this->buildResponse( |
145
|
|
|
$this->service->sendTemplate( |
146
|
|
|
$message->getName(), |
147
|
|
|
$message->getContent(), |
148
|
|
|
$message->toArray(), |
149
|
|
|
$this->async, |
150
|
|
|
$this->ipPool, |
151
|
|
|
$sendAt |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Mandrill's API returns an array of response array when you |
158
|
|
|
* send a message, here we'll convert them into objects before we return them to the user |
159
|
|
|
* |
160
|
|
|
* @param $messagesResponse |
161
|
|
|
* |
162
|
|
|
* @return SendResponse[] |
163
|
|
|
*/ |
164
|
|
|
private function buildResponse(array $messagesResponse): array |
165
|
|
|
{ |
166
|
|
|
$resArray = []; |
167
|
|
|
foreach ($messagesResponse as $response) { |
168
|
|
|
$resArray[] = new SendResponse( |
169
|
|
|
$response['_id'], |
170
|
|
|
$response['email'], |
171
|
|
|
$response['status'], |
172
|
|
|
isset($response['reject_reason']) ? $response['reject_reason'] : null |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
return $resArray; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param \DateTime $sendAt |
180
|
|
|
* |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
private function formatDate(\DateTime $sendAt): string |
184
|
|
|
{ |
185
|
|
|
return $sendAt->format('Y-m-d H:i:s'); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|