From 91afdf3fdb0a554f535efb1045461a924b88c63c Mon Sep 17 00:00:00 2001 From: "ruud.van.der.weijde@werkspot.nl" Date: Wed, 12 Jun 2019 20:58:46 +0200 Subject: [PATCH] Be able to throw exceptions Add responseReceived event which is triggered directly after the performed call. Add ThrowExceptionOnFailurePlugin which listens to the responseReceived event and throws an exception on failures. --- README.md | 12 +++++++ .../ThrowExceptionOnFailurePlugin.php | 13 +++++++ src/Postmark/Transport.php | 20 ++++++----- tests/ThrowExceptionOnFailurePluginTest.php | 34 +++++++++++++++++++ tests/TransportTest.php | 13 ++++++- 5 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/Postmark/ThrowExceptionOnFailurePlugin.php create mode 100644 tests/ThrowExceptionOnFailurePluginTest.php diff --git a/README.md b/README.md index e2e3daf..d33afcf 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,15 @@ $mailer->send($message); ?> ``` + +##### 3. Throw exceptions on Postmark api errors + +```php +$transport = new \Postmark\Transport(''); +$transport->registerPlugin(new \Postmark\ThrowExceptionOnFailurePlugin()); + +$message = new Swift_Message('Hello from Postmark!'); +$mailer->send($message); // Exception is throw when response !== 200 + +?> +``` diff --git a/src/Postmark/ThrowExceptionOnFailurePlugin.php b/src/Postmark/ThrowExceptionOnFailurePlugin.php new file mode 100644 index 0000000..9bcad91 --- /dev/null +++ b/src/Postmark/ThrowExceptionOnFailurePlugin.php @@ -0,0 +1,13 @@ +isValid()) { + throw new \Swift_TransportException($event->getResponse()); + } + } +} diff --git a/src/Postmark/Transport.php b/src/Postmark/Transport.php index 3dce12f..34be046 100644 --- a/src/Postmark/Transport.php +++ b/src/Postmark/Transport.php @@ -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; } } @@ -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; } diff --git a/tests/ThrowExceptionOnFailurePluginTest.php b/tests/ThrowExceptionOnFailurePluginTest.php new file mode 100644 index 0000000..3289970 --- /dev/null +++ b/tests/ThrowExceptionOnFailurePluginTest.php @@ -0,0 +1,34 @@ +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); + } +} diff --git a/tests/TransportTest.php b/tests/TransportTest.php index 3eb6079..4fe3218 100644 --- a/tests/TransportTest.php +++ b/tests/TransportTest.php @@ -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); + } }