vendor/api-platform/core/src/Symfony/Messenger/DispatchTrait.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\Messenger;
  12. use Symfony\Component\Messenger\Envelope;
  13. use Symfony\Component\Messenger\Exception\HandlerFailedException;
  14. use Symfony\Component\Messenger\MessageBusInterface;
  15. /**
  16.  * @internal
  17.  */
  18. trait DispatchTrait
  19. {
  20.     /**
  21.      * @var MessageBusInterface|null
  22.      */
  23.     private $messageBus;
  24.     /**
  25.      * @param object|Envelope $message
  26.      */
  27.     private function dispatch($message)
  28.     {
  29.         if (!$this->messageBus instanceof MessageBusInterface) {
  30.             throw new \InvalidArgumentException('The message bus is not set.');
  31.         }
  32.         if (!class_exists(HandlerFailedException::class)) {
  33.             return $this->messageBus->dispatch($message);
  34.         }
  35.         try {
  36.             return $this->messageBus->dispatch($message);
  37.         } catch (HandlerFailedException $e) {
  38.             // unwrap the exception thrown in handler for Symfony Messenger >= 4.3
  39.             while ($e instanceof HandlerFailedException) {
  40.                 /** @var \Throwable $e */
  41.                 $e $e->getPrevious();
  42.             }
  43.             throw $e;
  44.         }
  45.     }
  46. }
  47. class_alias(DispatchTrait::class, \ApiPlatform\Core\Bridge\Symfony\Messenger\DispatchTrait::class);