vendor/eightpoints/guzzle-bundle/src/Middleware/EventDispatchMiddleware.php line 58

Open in your IDE?
  1. <?php
  2. namespace EightPoints\Bundle\GuzzleBundle\Middleware;
  3. use EightPoints\Bundle\GuzzleBundle\Events\Event;
  4. use EightPoints\Bundle\GuzzleBundle\Events\PostTransactionEvent;
  5. use Exception;
  6. use GuzzleHttp\Exception\RequestException;
  7. use Psr\Http\Message\RequestInterface;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
  11. use EightPoints\Bundle\GuzzleBundle\Events\GuzzleEvents;
  12. use EightPoints\Bundle\GuzzleBundle\Events\PreTransactionEvent;
  13. /**
  14.  * Dispatches an Event using the Symfony Event Dispatcher.
  15.  * Dispatches a PRE_TRANSACTION event, before the transaction is sent
  16.  * Dispatches a POST_TRANSACTION event, when the remote hosts responds.
  17.  */
  18. class EventDispatchMiddleware
  19. {
  20.     /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */
  21.     private $eventDispatcher;
  22.     /** @var string */
  23.     private $serviceName;
  24.     /**
  25.      * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
  26.      * @param string $serviceName
  27.      */
  28.     public function __construct(EventDispatcherInterface $eventDispatcherstring $serviceName)
  29.     {
  30.         $this->eventDispatcher $eventDispatcher;
  31.         $this->serviceName $serviceName;
  32.     }
  33.     /**
  34.      * @return \Closure
  35.      */
  36.     public function dispatchEvent() : \Closure
  37.     {
  38.         return function (callable $handler) {
  39.             return function (
  40.                 RequestInterface $request,
  41.                 array $options
  42.             ) use ($handler) {
  43.                 // Create the Pre Transaction event.
  44.                 $preTransactionEvent = new PreTransactionEvent($request$this->serviceName);
  45.                 // Dispatch it through the symfony Dispatcher.
  46.                 $this->doDispatch($preTransactionEventGuzzleEvents::PRE_TRANSACTION);
  47.                 $this->doDispatch($preTransactionEventGuzzleEvents::preTransactionFor($this->serviceName));
  48.                 // Continue the handler chain.
  49.                 $promise $handler($preTransactionEvent->getTransaction(), $options);
  50.                 // Handle the response form the server.
  51.                 return $promise->then(
  52.                     function (ResponseInterface $response) {
  53.                         // Create the Post Transaction event.
  54.                         $postTransactionEvent = new PostTransactionEvent($response$this->serviceName);
  55.                         // Dispatch the event on the symfony event dispatcher.
  56.                         $this->doDispatch($postTransactionEventGuzzleEvents::POST_TRANSACTION);
  57.                         $this->doDispatch($postTransactionEventGuzzleEvents::postTransactionFor($this->serviceName));
  58.                         // Continue down the chain.
  59.                         return $postTransactionEvent->getTransaction();
  60.                     },
  61.                     function (Exception $reason) {
  62.                         // Get the response. The response in a RequestException can be null too.
  63.                         $response $reason instanceof RequestException $reason->getResponse() : null;
  64.                         // Create the Post Transaction event.
  65.                         $postTransactionEvent = new PostTransactionEvent($response$this->serviceName);
  66.                         // Dispatch the event on the symfony event dispatcher.
  67.                         $this->doDispatch($postTransactionEventGuzzleEvents::POST_TRANSACTION);
  68.                         $this->doDispatch($postTransactionEventGuzzleEvents::postTransactionFor($this->serviceName));
  69.                         // Continue down the chain.
  70.                         return \GuzzleHttp\Promise\Create::rejectionFor($reason);
  71.                     }
  72.                 );
  73.             };
  74.         };
  75.     }
  76.     private function doDispatch(Event $eventstring $name): void
  77.     {
  78.         if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface) {
  79.             $this->eventDispatcher->dispatch($event$name);
  80.             return;
  81.         }
  82.         // BC compatibility
  83.         $this->eventDispatcher->dispatch($name$event);
  84.     }
  85. }