Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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()
{
Expand All @@ -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
*/
Expand Down Expand Up @@ -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']
);
Expand Down
2 changes: 2 additions & 0 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
7 changes: 6 additions & 1 deletion src/Swift_PostmarkTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -35,6 +36,10 @@ public function __construct($token = null)
if ($token) {
$this->setApi(new Api($token));
}

if ($listener) {
$this->registerPlugin($listener);
}
}

/**
Expand Down
55 changes: 49 additions & 6 deletions src/Swift_Transport_PostmarkTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand All @@ -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}
*/
Expand All @@ -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)
{
Expand All @@ -108,6 +122,8 @@ protected function getMIMEPart(\Swift_Mime_Message $message, $mimeType)
return $part;
}
}

return null;
}

/**
Expand Down Expand Up @@ -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);
Expand Down