vendor/symfony/doctrine-messenger/Transport/DoctrineSender.php line 46

  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\Bridge\Doctrine\Transport;
  11. use Doctrine\DBAL\Exception as DBALException;
  12. use Symfony\Component\Messenger\Envelope;
  13. use Symfony\Component\Messenger\Exception\TransportException;
  14. use Symfony\Component\Messenger\Stamp\DelayStamp;
  15. use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
  16. use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
  17. use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
  18. use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
  19. /**
  20.  * @author Vincent Touzet <vincent.touzet@gmail.com>
  21.  */
  22. class DoctrineSender implements SenderInterface
  23. {
  24.     private Connection $connection;
  25.     private SerializerInterface $serializer;
  26.     public function __construct(Connection $connectionSerializerInterface $serializer null)
  27.     {
  28.         $this->connection $connection;
  29.         $this->serializer $serializer ?? new PhpSerializer();
  30.     }
  31.     public function send(Envelope $envelope): Envelope
  32.     {
  33.         $encodedMessage $this->serializer->encode($envelope);
  34.         /** @var DelayStamp|null $delayStamp */
  35.         $delayStamp $envelope->last(DelayStamp::class);
  36.         $delay null !== $delayStamp $delayStamp->getDelay() : 0;
  37.         try {
  38.             $id $this->connection->send($encodedMessage['body'], $encodedMessage['headers'] ?? [], $delay);
  39.         } catch (DBALException $exception) {
  40.             throw new TransportException($exception->getMessage(), 0$exception);
  41.         }
  42.         return $envelope->with(new TransportMessageIdStamp($id));
  43.     }
  44. }