vendor/symfony/messenger/EventListener/SendFailedMessageToFailureTransportListener.php line 37

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\Messenger\EventListener;
  11. use Psr\Container\ContainerInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  15. use Symfony\Component\Messenger\Stamp\DelayStamp;
  16. use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
  17. use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
  18. /**
  19.  * Sends a rejected message to a "failure transport".
  20.  *
  21.  * @author Ryan Weaver <ryan@symfonycasts.com>
  22.  */
  23. class SendFailedMessageToFailureTransportListener implements EventSubscriberInterface
  24. {
  25.     private $failureSenders;
  26.     private $logger;
  27.     public function __construct(ContainerInterface $failureSendersLoggerInterface $logger null)
  28.     {
  29.         $this->failureSenders $failureSenders;
  30.         $this->logger $logger;
  31.     }
  32.     public function onMessageFailed(WorkerMessageFailedEvent $event)
  33.     {
  34.         if ($event->willRetry()) {
  35.             return;
  36.         }
  37.         if (!$this->failureSenders->has($event->getReceiverName())) {
  38.             return;
  39.         }
  40.         $failureSender $this->failureSenders->get($event->getReceiverName());
  41.         $envelope $event->getEnvelope();
  42.         // avoid re-sending to the failed sender
  43.         if (null !== $envelope->last(SentToFailureTransportStamp::class)) {
  44.             return;
  45.         }
  46.         $envelope $envelope->with(
  47.             new SentToFailureTransportStamp($event->getReceiverName()),
  48.             new DelayStamp(0),
  49.             new RedeliveryStamp(0)
  50.         );
  51.         if (null !== $this->logger) {
  52.             $this->logger->info('Rejected message {class} will be sent to the failure transport {transport}.', [
  53.                 'class' => \get_class($envelope->getMessage()),
  54.                 'transport' => \get_class($failureSender),
  55.             ]);
  56.         }
  57.         $failureSender->send($envelope);
  58.     }
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             WorkerMessageFailedEvent::class => ['onMessageFailed', -100],
  63.         ];
  64.     }
  65. }