diff --git a/Events/PostTransactionEvent.php b/Events/PostTransactionEvent.php index 0f5710c..3e62e88 100644 --- a/Events/PostTransactionEvent.php +++ b/Events/PostTransactionEvent.php @@ -8,7 +8,7 @@ class PostTransactionEvent extends Event { /** - * @var ResponseInterface + * @var ResponseInterface|null */ protected $response; @@ -20,10 +20,10 @@ class PostTransactionEvent extends Event /** * PostTransactionEvent constructor. * - * @param \Psr\Http\Message\ResponseInterface $response - * @param string $serviceName + * @param \Psr\Http\Message\ResponseInterface|null $response + * @param string $serviceName */ - public function __construct(ResponseInterface $response, $serviceName) + public function __construct(ResponseInterface $response = null, $serviceName) { $this->response = $response; $this->serviceName = $serviceName; @@ -45,9 +45,9 @@ public function getTransaction() /** * Sets the transaction inline with the event. * - * @param ResponseInterface $response + * @param ResponseInterface|null $response */ - public function setTransaction(ResponseInterface $response) + public function setTransaction(ResponseInterface $response = null) { $this->response = $response; } diff --git a/Middleware/EventDispatchMiddleware.php b/Middleware/EventDispatchMiddleware.php index a260afe..f1952d7 100644 --- a/Middleware/EventDispatchMiddleware.php +++ b/Middleware/EventDispatchMiddleware.php @@ -3,6 +3,8 @@ namespace EightPoints\Bundle\GuzzleBundle\Middleware; use EightPoints\Bundle\GuzzleBundle\Events\PostTransactionEvent; +use Exception; +use GuzzleHttp\Exception\RequestException; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -52,28 +54,32 @@ public function dispatchEvent() $this->eventDispatcher->dispatch(GuzzleEvents::PRE_TRANSACTION, $preTransactionEvent); // Continue the handler chain. - $promise = $handler($preTransactionEvent->getTransaction(), $options); - // Handle the response form teh server. + $promise = $handler($request, $options); + + // Handle the response form the server. return $promise->then( function (ResponseInterface $response) { - // Create hte Post Transaction event. + // Create the Post Transaction event. $postTransactionEvent = new PostTransactionEvent($response, $this->serviceName); // Dispatch the event on the symfony event dispatcher. $this->eventDispatcher->dispatch(GuzzleEvents::POST_TRANSACTION, $postTransactionEvent); // Continue down the chain. - return $postTransactionEvent->getTransaction(); + return $response; }, - function (\Exception $exception) { - // Create hte Post Transaction event. - $postTransactionEvent = new PostTransactionEvent($exception->getResponse(), $this->serviceName); + function (Exception $reason) { + // Get the response. The response in a RequestException can be null too. + $response = $reason instanceof RequestException ? $reason->getResponse() : null; + + // Create the Post Transaction event. + $postTransactionEvent = new PostTransactionEvent($response, $this->serviceName); // Dispatch the event on the symfony event dispatcher. $this->eventDispatcher->dispatch(GuzzleEvents::POST_TRANSACTION, $postTransactionEvent); // Continue down the chain. - return $postTransactionEvent->getTransaction(); + return \GuzzleHttp\Promise\rejection_for($reason); } ); };