RedisTransport.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Transport\Serialization\PhpSerializer;
  13. use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
  14. use Symfony\Component\Messenger\Transport\SetupableTransportInterface;
  15. use Symfony\Component\Messenger\Transport\TransportInterface;
  16. /**
  17. * @author Alexander Schranz <alexander@sulu.io>
  18. * @author Antoine Bluchet <soyuka@gmail.com>
  19. */
  20. class RedisTransport implements TransportInterface, SetupableTransportInterface
  21. {
  22. private $serializer;
  23. private $connection;
  24. private $receiver;
  25. private $sender;
  26. public function __construct(Connection $connection, SerializerInterface $serializer = null)
  27. {
  28. $this->connection = $connection;
  29. $this->serializer = $serializer ?? new PhpSerializer();
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function get(): iterable
  35. {
  36. return ($this->receiver ?? $this->getReceiver())->get();
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function ack(Envelope $envelope): void
  42. {
  43. ($this->receiver ?? $this->getReceiver())->ack($envelope);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function reject(Envelope $envelope): void
  49. {
  50. ($this->receiver ?? $this->getReceiver())->reject($envelope);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function send(Envelope $envelope): Envelope
  56. {
  57. return ($this->sender ?? $this->getSender())->send($envelope);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function setup(): void
  63. {
  64. $this->connection->setup();
  65. }
  66. private function getReceiver(): RedisReceiver
  67. {
  68. return $this->receiver = new RedisReceiver($this->connection, $this->serializer);
  69. }
  70. private function getSender(): RedisSender
  71. {
  72. return $this->sender = new RedisSender($this->connection, $this->serializer);
  73. }
  74. }
  75. class_alias(RedisTransport::class, \Symfony\Component\Messenger\Transport\RedisExt\RedisTransport::class);