1 | <?php |
||
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) |
|
63 | |||
64 | /** |
||
65 | * @param Swift_Events_EventDispatcher $eventDispatcher |
||
66 | * @param SparkPost $sparkpost |
||
67 | * @param PayloadBuilder $payloadBuilder |
||
68 | */ |
||
69 | 39 | public function __construct( |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 3 | public function isStarted() |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 3 | public function start() |
|
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | 3 | public function stop() |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 30 | public function send(Swift_Mime_Message $message, &$failedRecipients = null) |
|
143 | |||
144 | /** |
||
145 | * {@inheritdoc} |
||
146 | */ |
||
147 | 3 | public function registerPlugin(Swift_Events_EventListener $plugin) |
|
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) |
|
220 | } |
||
221 |