Skip to content

Commit

Permalink
#114: Fixed restored reject propagation in the EventDispatchMiddlewar…
Browse files Browse the repository at this point in the history
…e. (#115)

- Instead of returning the response the reject function now returns guzzle rejection for that reason.
- PostTransactionEvent now allowes null values as the response.
  • Loading branch information
TheLevti authored and Florian Preusner committed Aug 13, 2017
1 parent 38976c6 commit 27ac7f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
12 changes: 6 additions & 6 deletions Events/PostTransactionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class PostTransactionEvent extends Event
{
/**
* @var ResponseInterface
* @var ResponseInterface|null
*/
protected $response;

Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
22 changes: 14 additions & 8 deletions Middleware/EventDispatchMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
);
};
Expand Down

0 comments on commit 27ac7f9

Please sign in to comment.