Skip to content

Commit

Permalink
Merge pull request #22 from Werkspot/be_able_to_throw_exceptions
Browse files Browse the repository at this point in the history
Be able to throw exceptions
  • Loading branch information
Andrew Theken committed Jul 7, 2019
2 parents dc20462 + 91afdf3 commit f3932a6
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 9 deletions.
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -40,3 +40,15 @@ $mailer->send($message);

?>
```

##### 3. Throw exceptions on Postmark api errors

```php
$transport = new \Postmark\Transport('<SERVER_TOKEN>');
$transport->registerPlugin(new \Postmark\ThrowExceptionOnFailurePlugin());

$message = new Swift_Message('Hello from Postmark!');
$mailer->send($message); // Exception is throw when response !== 200

?>
```
13 changes: 13 additions & 0 deletions src/Postmark/ThrowExceptionOnFailurePlugin.php
@@ -0,0 +1,13 @@
<?php

namespace Postmark;

class ThrowExceptionOnFailurePlugin implements \Swift_Events_ResponseListener
{
public function responseReceived(\Swift_Events_ResponseEvent $event)
{
if (!$event->isValid()) {
throw new \Swift_TransportException($event->getResponse());
}
}
}
20 changes: 12 additions & 8 deletions src/Postmark/Transport.php
Expand Up @@ -74,9 +74,9 @@ public function ping() {
public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) {
$client = $this->getHttpClient();

if ($evt = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($evt, 'beforeSendPerformed');
if ($evt->bubbleCancelled()) {
if ($sendEvent = $this->_eventDispatcher->createSendEvent($this, $message)) {
$this->_eventDispatcher->dispatchEvent($sendEvent, 'beforeSendPerformed');
if ($sendEvent->bubbleCancelled()) {
return 0;
}
}
Expand All @@ -94,14 +94,18 @@ public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = nul
'http_errors' => false,
]);

$sendSuccessful = $response->getStatusCode() == 200;
$success = $response->getStatusCode() === 200;

if ($evt && $sendSuccessful) {
$evt->setResult(\Swift_Events_SendEvent::RESULT_SUCCESS);
$this->_eventDispatcher->dispatchEvent($evt, 'sendPerformed');
if ($responseEvent = $this->_eventDispatcher->createResponseEvent($this, $response->getBody()->getContents(), $success)) {
$this->_eventDispatcher->dispatchEvent($responseEvent, 'responseReceived');
}

if ($sendEvent) {
$sendEvent->setResult($success ? \Swift_Events_SendEvent::RESULT_SUCCESS : \Swift_Events_SendEvent::RESULT_FAILED);
$this->_eventDispatcher->dispatchEvent($sendEvent, 'sendPerformed');
}

return $sendSuccessful
return $success
? $this->getRecipientCount($message)
: 0;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/ThrowExceptionOnFailurePluginTest.php
@@ -0,0 +1,34 @@
<?php

use PHPUnit\Framework\TestCase;
use Postmark\ThrowExceptionOnFailurePlugin;

require_once __DIR__ . '/../vendor/autoload.php';

class ThrowExceptionOnFailurePluginTest extends TestCase {

/**
* @doesNotPerformAssertions
*/
public function testValidResponseThrowsNoException()
{
$valid = true;
$event = new \Swift_Events_ResponseEvent(new \Postmark\Transport('SERVER_TOKEN'), 'success', $valid);

$plugin = new ThrowExceptionOnFailurePlugin();
$plugin->responseReceived($event); // no exception
}

public function testInvalidResponseThrowsException()
{
$valid = false;
$event = new \Swift_Events_ResponseEvent(new \Postmark\Transport('SERVER_TOKEN'), 'invalid response', $valid);

$plugin = new ThrowExceptionOnFailurePlugin();

$this->expectException(\Swift_TransportException::class);
$this->expectExceptionMessage('invalid response');

$plugin->responseReceived($event);
}
}
13 changes: 12 additions & 1 deletion tests/TransportTest.php
Expand Up @@ -146,9 +146,20 @@ public function testToAndCcCanBeNullableEvents()

public function testServerTokenReturnedFromPublicMethod()
{
$transport = new PostmarkTransportStub([new Response(200)]);
$transport = new PostmarkTransportStub();
$this->assertEquals($transport->getServerToken(), 'TESTING_SERVER');
}

public function testFailedResponse()
{
$message = new Swift_Message();

$transport = new PostmarkTransportStub([new Response(401)]);
$transport->registerPlugin(new \Postmark\ThrowExceptionOnFailurePlugin());

$this->expectException(\Swift_TransportException::class);
$transport->send($message);
}
}


Expand Down

0 comments on commit f3932a6

Please sign in to comment.