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\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
  14. use Symfony\Component\Messenger\Stamp\DelayStamp;
  15. use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
  16. use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
  17. use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
  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 $failureSender;
  26.     private $logger;
  27.     public function __construct(SenderInterface $failureSenderLoggerInterface $logger null)
  28.     {
  29.         $this->failureSender $failureSender;
  30.         $this->logger $logger;
  31.     }
  32.     public function onMessageFailed(WorkerMessageFailedEvent $event)
  33.     {
  34.         if ($event->willRetry()) {
  35.             return;
  36.         }
  37.         $envelope $event->getEnvelope();
  38.         // avoid re-sending to the failed sender
  39.         if (null !== $envelope->last(SentToFailureTransportStamp::class)) {
  40.             return;
  41.         }
  42.         $envelope $envelope->with(
  43.             new SentToFailureTransportStamp($event->getReceiverName()),
  44.             new DelayStamp(0),
  45.             new RedeliveryStamp(0)
  46.         );
  47.         if (null !== $this->logger) {
  48.             $this->logger->info('Rejected message {class} will be sent to the failure transport {transport}.', [
  49.                 'class' => \get_class($envelope->getMessage()),
  50.                 'transport' => \get_class($this->failureSender),
  51.             ]);
  52.         }
  53.         $this->failureSender->send($envelope);
  54.     }
  55.     public static function getSubscribedEvents()
  56.     {
  57.         return [
  58.             WorkerMessageFailedEvent::class => ['onMessageFailed', -100],
  59.         ];
  60.     }
  61. }