InMemoryTransport.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Transport;
  11. use Symfony\Component\Messenger\Envelope;
  12. use Symfony\Contracts\Service\ResetInterface;
  13. /**
  14. * Transport that stays in memory. Useful for testing purpose.
  15. *
  16. * @author Gary PEGEOT <garypegeot@gmail.com>
  17. */
  18. class InMemoryTransport implements TransportInterface, ResetInterface
  19. {
  20. /**
  21. * @var Envelope[]
  22. */
  23. private $sent = [];
  24. /**
  25. * @var Envelope[]
  26. */
  27. private $acknowledged = [];
  28. /**
  29. * @var Envelope[]
  30. */
  31. private $rejected = [];
  32. /**
  33. * @var Envelope[]
  34. */
  35. private $queue = [];
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function get(): iterable
  40. {
  41. return array_values($this->queue);
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function ack(Envelope $envelope): void
  47. {
  48. $this->acknowledged[] = $envelope;
  49. $id = spl_object_hash($envelope->getMessage());
  50. unset($this->queue[$id]);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function reject(Envelope $envelope): void
  56. {
  57. $this->rejected[] = $envelope;
  58. $id = spl_object_hash($envelope->getMessage());
  59. unset($this->queue[$id]);
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function send(Envelope $envelope): Envelope
  65. {
  66. $this->sent[] = $envelope;
  67. $id = spl_object_hash($envelope->getMessage());
  68. $this->queue[$id] = $envelope;
  69. return $envelope;
  70. }
  71. public function reset()
  72. {
  73. $this->sent = $this->queue = $this->rejected = $this->acknowledged = [];
  74. }
  75. /**
  76. * @return Envelope[]
  77. */
  78. public function getAcknowledged(): array
  79. {
  80. return $this->acknowledged;
  81. }
  82. /**
  83. * @return Envelope[]
  84. */
  85. public function getRejected(): array
  86. {
  87. return $this->rejected;
  88. }
  89. /**
  90. * @return Envelope[]
  91. */
  92. public function getSent(): array
  93. {
  94. return $this->sent;
  95. }
  96. }