RedisSender.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Redis\Transport;
  11. use Symfony\Component\Messenger\Envelope;
  12. use Symfony\Component\Messenger\Stamp\DelayStamp;
  13. use Symfony\Component\Messenger\Transport\Sender\SenderInterface;
  14. use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
  15. /**
  16. * @author Alexander Schranz <alexander@sulu.io>
  17. * @author Antoine Bluchet <soyuka@gmail.com>
  18. */
  19. class RedisSender implements SenderInterface
  20. {
  21. private $connection;
  22. private $serializer;
  23. public function __construct(Connection $connection, SerializerInterface $serializer)
  24. {
  25. $this->connection = $connection;
  26. $this->serializer = $serializer;
  27. }
  28. /**
  29. * {@inheritdoc}
  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. $delayInMs = null !== $delayStamp ? $delayStamp->getDelay() : 0;
  37. $this->connection->add($encodedMessage['body'], $encodedMessage['headers'] ?? [], $delayInMs);
  38. return $envelope;
  39. }
  40. }
  41. class_alias(RedisSender::class, \Symfony\Component\Messenger\Transport\RedisExt\RedisSender::class);