StompDestination.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. declare(strict_types=1);
  3. namespace Enqueue\Stomp;
  4. use Interop\Queue\Queue;
  5. use Interop\Queue\Topic;
  6. class StompDestination implements Topic, Queue
  7. {
  8. const TYPE_TOPIC = 'topic';
  9. const TYPE_EXCHANGE = 'exchange';
  10. const TYPE_QUEUE = 'queue';
  11. const TYPE_AMQ_QUEUE = 'amq/queue';
  12. const TYPE_TEMP_QUEUE = 'temp-queue';
  13. const TYPE_REPLY_QUEUE = 'reply-queue';
  14. const HEADER_DURABLE = 'durable';
  15. const HEADER_AUTO_DELETE = 'auto-delete';
  16. const HEADER_EXCLUSIVE = 'exclusive';
  17. /**
  18. * @var string
  19. */
  20. private $name;
  21. /**
  22. * @var string
  23. */
  24. private $type;
  25. /**
  26. * @var string
  27. */
  28. private $routingKey;
  29. /**
  30. * @var array
  31. */
  32. private $headers;
  33. public function __construct()
  34. {
  35. $this->headers = [
  36. self::HEADER_DURABLE => false,
  37. self::HEADER_AUTO_DELETE => true,
  38. self::HEADER_EXCLUSIVE => false,
  39. ];
  40. }
  41. public function getStompName(): string
  42. {
  43. return $this->name;
  44. }
  45. public function setStompName(string $name): void
  46. {
  47. $this->name = $name;
  48. }
  49. public function getQueueName(): string
  50. {
  51. if (empty($this->getStompName())) {
  52. throw new \LogicException('Destination name is not set');
  53. }
  54. $name = '/'.$this->getType().'/'.$this->getStompName();
  55. if ($this->getRoutingKey()) {
  56. $name .= '/'.$this->getRoutingKey();
  57. }
  58. return $name;
  59. }
  60. public function getTopicName(): string
  61. {
  62. return $this->getQueueName();
  63. }
  64. public function getType(): string
  65. {
  66. return $this->type;
  67. }
  68. public function setType(string $type): void
  69. {
  70. $types = [
  71. self::TYPE_TOPIC,
  72. self::TYPE_EXCHANGE,
  73. self::TYPE_QUEUE,
  74. self::TYPE_AMQ_QUEUE,
  75. self::TYPE_TEMP_QUEUE,
  76. self::TYPE_REPLY_QUEUE,
  77. ];
  78. if (false == in_array($type, $types, true)) {
  79. throw new \LogicException(sprintf('Invalid destination type: "%s"', $type));
  80. }
  81. $this->type = $type;
  82. }
  83. public function getRoutingKey(): ?string
  84. {
  85. return $this->routingKey;
  86. }
  87. public function setRoutingKey(string $routingKey = null): void
  88. {
  89. $this->routingKey = $routingKey;
  90. }
  91. public function isDurable(): bool
  92. {
  93. return $this->getHeader(self::HEADER_DURABLE, false);
  94. }
  95. public function setDurable(bool $durable): void
  96. {
  97. $this->setHeader(self::HEADER_DURABLE, $durable);
  98. }
  99. public function isAutoDelete(): bool
  100. {
  101. return $this->getHeader(self::HEADER_AUTO_DELETE, false);
  102. }
  103. public function setAutoDelete(bool $autoDelete): void
  104. {
  105. $this->setHeader(self::HEADER_AUTO_DELETE, $autoDelete);
  106. }
  107. public function isExclusive(): bool
  108. {
  109. return $this->getHeader(self::HEADER_EXCLUSIVE, false);
  110. }
  111. public function setExclusive(bool $exclusive): void
  112. {
  113. $this->setHeader(self::HEADER_EXCLUSIVE, $exclusive);
  114. }
  115. public function setHeaders(array $headers): void
  116. {
  117. $this->headers = $headers;
  118. }
  119. public function getHeaders(): array
  120. {
  121. return $this->headers;
  122. }
  123. public function getHeader(string $name, $default = null)
  124. {
  125. return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default;
  126. }
  127. public function setHeader(string $name, $value): void
  128. {
  129. $this->headers[$name] = $value;
  130. }
  131. }