diff --git a/src/Api.php b/src/Api.php index 161d48e..ac63e6d 100644 --- a/src/Api.php +++ b/src/Api.php @@ -2,6 +2,9 @@ namespace Openbuildings\Postmark; +// Make it obvious when we're throwing a custom exception +use Openbuildings\Postmark\Exception as PostmarkException; + /** * Class for manupulating a server * @@ -58,9 +61,8 @@ public function setToken($token) /** * Get the headers needed to be sent to the Postmark API. - * * @return array of header strings - * @throws Exception If the Postmark API token is not yet set. + * @throws \Exception */ public function getHeaders() { @@ -81,8 +83,8 @@ public function getHeaders() * * @param array $data * @return array Postmark API response. - * @throws Exception If API request failed or JSON returned was invalid. - * @throws Openbuildings\Postmark\Exception If Postmark API returned an error. + * @throws \Exception If API request failed or JSON returned was invalid. + * @throws PostmarkException If Postmark API returned an error. * @uses Openbuildings\Postmark\Api::getSendUri to determine the request URI * @uses Openbuildings\Postmark\Api::getHeaders for the request headers */ @@ -120,7 +122,7 @@ public function send(array $data) $responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($responseCode != 200) { - throw new Exception( + throw new PostmarkException( sprintf('Postmark delivery failed: %s', $response['Message']), (int) $response['ErrorCode'] ); diff --git a/src/Exception.php b/src/Exception.php index c83674f..fbe2f20 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -52,6 +52,8 @@ class Exception extends Swift_TransportException * Create a new Exception with $message and $code * * @param string $message + * @param int $code + * @param \Exception $previous */ public function __construct($message = '', $code = 0, \Exception $previous = null) { diff --git a/src/Swift_PostmarkTransport.php b/src/Swift_PostmarkTransport.php index 4d8da7f..5c85e26 100644 --- a/src/Swift_PostmarkTransport.php +++ b/src/Swift_PostmarkTransport.php @@ -18,8 +18,9 @@ class Swift_PostmarkTransport extends Swift_Transport_PostmarkTransport * Create a new PostmarkTransport. * * @param null|string $token + * @param \Swift_Events_EventListener $listener */ - public function __construct($token = null) + public function __construct($token = null, \Swift_Events_EventListener $listener = null) { Swift_DependencyContainer::getInstance() ->register('transport.postmark') @@ -35,6 +36,10 @@ public function __construct($token = null) if ($token) { $this->setApi(new Api($token)); } + + if ($listener) { + $this->registerPlugin($listener); + } } /** diff --git a/src/Swift_Transport_PostmarkTransport.php b/src/Swift_Transport_PostmarkTransport.php index 0113c72..93f7441 100644 --- a/src/Swift_Transport_PostmarkTransport.php +++ b/src/Swift_Transport_PostmarkTransport.php @@ -15,15 +15,20 @@ class Swift_Transport_PostmarkTransport implements \Swift_Transport /** * The Postmark API SDK instance. * - * @var Openbuildings\Postmark\Api + * @var \Openbuildings\Postmark\Api */ protected $api; /** - * @var Swift_Events_EventDispatcher + * @var \Swift_Events_EventDispatcher */ protected $eventDispatcher; + /** + * @var \Swift_Mime_Message + */ + protected $message; + public function __construct(\Swift_Events_EventDispatcher $eventDispatcher) { $this->eventDispatcher = $eventDispatcher; @@ -52,7 +57,7 @@ public static function convertEmailsArray(array $emails) /** * Get the Postmark API SDK instance * - * @return Openbuildings\Postmark\Api + * @return \Openbuildings\Postmark\Api */ public function getApi() { @@ -72,6 +77,15 @@ public function setApi(Api $api) return $this; } + /** + * Get the message currently being processed + * + * @return \Swift_Mime_Message|null + */ + public function getMessage() { + return $this->message; + } + /** * {@inheritdoc} */ @@ -97,9 +111,9 @@ public function stop() } /** - * @param Swift_Mime_Message $message + * @param \Swift_Mime_Message $message * @param string $mimeType - * @return Swift_Mime_MimePart + * @return \Swift_Mime_MimePart|null */ protected function getMIMEPart(\Swift_Mime_Message $message, $mimeType) { @@ -108,6 +122,8 @@ protected function getMIMEPart(\Swift_Mime_Message $message, $mimeType) return $part; } } + + return null; } /** @@ -173,7 +189,34 @@ public function send(\Swift_Mime_Message $message, &$failedRecipients = null) } } - $response = $this->getApi()->send($data); + try { + $response = $this->getApi()->send($data); + } catch (\Exception $e) { + // Something went wrong. Trigger Swift's exception event. + if ($e instanceof \Swift_TransportException) { + $exceptionToHandle = $e; + } else { + $exceptionToHandle = new \Swift_TransportException( + $e->getMessage(), + 0, + $e + ); + } + + $this->message = $message; + + $exceptionEvent = $this->eventDispatcher->createTransportExceptionEvent( + $this, + $exceptionToHandle + ); + $this->eventDispatcher->dispatchEvent($exceptionEvent, 'exceptionThrown'); + + // Clear the message because we don't want it hanging around in the class because we're done with it. + $this->message = null; + + // Pass along the original exception. + throw $e; + } if ($evt) { $evt->setResult(\Swift_Events_SendEvent::RESULT_SUCCESS);