|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license https://github.com/f500/swiftmailer-sparkpost/blob/master/LICENSE MIT |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace SwiftSparkPost; |
|
8
|
|
|
|
|
9
|
|
|
use Exception as AnyException; |
|
10
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
11
|
|
|
use Http\Adapter\Guzzle6\Client as GuzzleAdapter; |
|
12
|
|
|
use SparkPost\SparkPost; |
|
13
|
|
|
use SparkPost\SparkPostResponse; |
|
14
|
|
|
use Swift_DependencyContainer; |
|
15
|
|
|
use Swift_Events_EventDispatcher; |
|
16
|
|
|
use Swift_Events_EventListener; |
|
17
|
|
|
use Swift_Events_SendEvent; |
|
18
|
|
|
use Swift_Mime_Message; |
|
19
|
|
|
use Swift_Transport; |
|
20
|
|
|
use Swift_TransportException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @copyright Future500 B.V. |
|
24
|
|
|
* @author Jasper N. Brouwer <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class Transport implements Swift_Transport |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var Swift_Events_EventDispatcher |
|
30
|
|
|
*/ |
|
31
|
|
|
private $eventDispatcher; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var SparkPost |
|
35
|
|
|
*/ |
|
36
|
|
|
private $sparkpost; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var PayloadBuilder |
|
40
|
|
|
*/ |
|
41
|
|
|
private $payloadBuilder; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $apiKey |
|
45
|
|
|
* @param Configuration|null $config |
|
46
|
|
|
* |
|
47
|
|
|
* @return Transport |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public static function newInstance($apiKey, Configuration $config = null) |
|
50
|
|
|
{ |
|
51
|
3 |
|
if ($config === null) { |
|
52
|
3 |
|
$config = new Configuration(); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
$eventDispatcher = Swift_DependencyContainer::getInstance()->lookup('transport.eventdispatcher'); |
|
56
|
3 |
|
$guzzle = new GuzzleAdapter(new GuzzleClient(['http_errors' => false, 'timeout' => 300])); |
|
57
|
3 |
|
$sparkpost = new SparkPost($guzzle, ['key' => $apiKey]); |
|
58
|
3 |
|
$randomNumberGenerator = new MtRandomNumberGenerator(); |
|
59
|
3 |
|
$payloadBuilder = new StandardPayloadBuilder($config, $randomNumberGenerator); |
|
60
|
|
|
|
|
61
|
3 |
|
return new self($eventDispatcher, $sparkpost, $payloadBuilder); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param Swift_Events_EventDispatcher $eventDispatcher |
|
66
|
|
|
* @param SparkPost $sparkpost |
|
67
|
|
|
* @param PayloadBuilder $payloadBuilder |
|
68
|
|
|
*/ |
|
69
|
39 |
|
public function __construct( |
|
70
|
|
|
Swift_Events_EventDispatcher $eventDispatcher, |
|
71
|
|
|
SparkPost $sparkpost, |
|
72
|
|
|
PayloadBuilder $payloadBuilder |
|
73
|
|
|
) { |
|
74
|
39 |
|
$this->eventDispatcher = $eventDispatcher; |
|
75
|
39 |
|
$this->sparkpost = $sparkpost; |
|
76
|
39 |
|
$this->payloadBuilder = $payloadBuilder; |
|
77
|
39 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public function isStarted() |
|
83
|
|
|
{ |
|
84
|
3 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function start() |
|
91
|
|
|
{ |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function stop() |
|
98
|
|
|
{ |
|
99
|
3 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
30 |
|
public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
105
|
|
|
{ |
|
106
|
30 |
|
$failedRecipients = (array) $failedRecipients; |
|
107
|
|
|
|
|
108
|
30 |
|
if ($event = $this->eventDispatcher->createSendEvent($this, $message)) { |
|
109
|
30 |
|
$this->eventDispatcher->dispatchEvent($event, 'beforeSendPerformed'); |
|
110
|
|
|
|
|
111
|
30 |
|
if ($event->bubbleCancelled()) { |
|
112
|
3 |
|
return 0; |
|
113
|
|
|
} |
|
114
|
9 |
|
} |
|
115
|
|
|
|
|
116
|
27 |
|
$payload = $this->buildPayload($message); |
|
117
|
24 |
|
$response = $this->sendTransmission($payload); |
|
118
|
18 |
|
$body = $response->getBody(); |
|
119
|
|
|
|
|
120
|
18 |
|
$sent = isset($body['results']['total_accepted_recipients']) |
|
121
|
18 |
|
? (int) $body['results']['total_accepted_recipients'] |
|
122
|
18 |
|
: 0; |
|
123
|
|
|
|
|
124
|
18 |
|
$unsent = isset($body['results']['total_rejected_recipients']) |
|
125
|
18 |
|
? (int) $body['results']['total_rejected_recipients'] |
|
126
|
18 |
|
: 0; |
|
127
|
|
|
|
|
128
|
18 |
|
if ($event) { |
|
129
|
18 |
|
if ($sent === 0) { |
|
130
|
3 |
|
$event->setResult(Swift_Events_SendEvent::RESULT_FAILED); |
|
131
|
16 |
|
} elseif ($unsent > 0) { |
|
132
|
3 |
|
$event->setResult(Swift_Events_SendEvent::RESULT_TENTATIVE); |
|
133
|
1 |
|
} else { |
|
134
|
12 |
|
$event->setResult(Swift_Events_SendEvent::RESULT_SUCCESS); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
18 |
|
$event->setFailedRecipients($failedRecipients); |
|
138
|
18 |
|
$this->eventDispatcher->dispatchEvent($event, 'sendPerformed'); |
|
139
|
6 |
|
} |
|
140
|
|
|
|
|
141
|
18 |
|
return (int) $sent; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function registerPlugin(Swift_Events_EventListener $plugin) |
|
148
|
|
|
{ |
|
149
|
3 |
|
$this->eventDispatcher->bindEventListener($plugin); |
|
150
|
3 |
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param Swift_Mime_Message $message |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
* @throws Swift_TransportException |
|
157
|
|
|
*/ |
|
158
|
27 |
|
private function buildPayload(Swift_Mime_Message $message) |
|
159
|
|
|
{ |
|
160
|
|
|
try { |
|
161
|
27 |
|
return $this->payloadBuilder->buildPayload($message); |
|
162
|
3 |
|
} catch (AnyException $exception) { |
|
163
|
3 |
|
throw $this->createAndDispatchTransportException( |
|
164
|
3 |
|
'Failed to build payload for a SparkPost transmission', |
|
165
|
2 |
|
$exception |
|
166
|
1 |
|
); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param array $payload |
|
172
|
|
|
* |
|
173
|
|
|
* @return SparkPostResponse |
|
174
|
|
|
* @throws Swift_TransportException |
|
175
|
|
|
*/ |
|
176
|
24 |
|
private function sendTransmission(array $payload) |
|
177
|
|
|
{ |
|
178
|
|
|
try { |
|
179
|
|
|
/** @noinspection PhpUndefinedVariableInspection */ |
|
180
|
24 |
|
$promise = $this->sparkpost->transmissions->post($payload); |
|
181
|
|
|
|
|
182
|
21 |
|
if ($promise instanceof SparkPostResponse) { |
|
183
|
18 |
|
$response = $promise; |
|
184
|
6 |
|
} else { |
|
185
|
15 |
|
$response = $promise->wait(); |
|
186
|
|
|
} |
|
187
|
10 |
|
} catch (AnyException $exception) { |
|
188
|
3 |
|
throw $this->createAndDispatchTransportException( |
|
189
|
3 |
|
'Failed to send transmission to SparkPost', |
|
190
|
2 |
|
$exception |
|
191
|
1 |
|
); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
21 |
|
if (!isset($response->getBody()['results'])) { |
|
195
|
3 |
|
throw $this->createAndDispatchTransportException( |
|
196
|
3 |
|
'Failed to send transmission to SparkPost', |
|
197
|
3 |
|
new Exception(json_encode($response->getBody())) |
|
198
|
1 |
|
); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
18 |
|
return $response; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param string $message |
|
206
|
|
|
* @param AnyException $exception |
|
207
|
|
|
* |
|
208
|
|
|
* @return Swift_TransportException |
|
209
|
|
|
*/ |
|
210
|
9 |
|
private function createAndDispatchTransportException($message, AnyException $exception) |
|
211
|
|
|
{ |
|
212
|
9 |
|
$transportException = new Swift_TransportException($message, 0, $exception); |
|
213
|
|
|
|
|
214
|
9 |
|
if ($event = $this->eventDispatcher->createTransportExceptionEvent($this, $transportException)) { |
|
215
|
9 |
|
$this->eventDispatcher->dispatchEvent($event, 'exceptionThrown'); |
|
216
|
3 |
|
} |
|
217
|
|
|
|
|
218
|
9 |
|
return $transportException; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|