AmqpStamp.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Amqp\Transport;
  11. use Symfony\Component\Messenger\Stamp\NonSendableStampInterface;
  12. /**
  13. * @author Guillaume Gammelin <ggammelin@gmail.com>
  14. * @author Samuel Roze <samuel.roze@gmail.com>
  15. */
  16. final class AmqpStamp implements NonSendableStampInterface
  17. {
  18. private $routingKey;
  19. private $flags;
  20. private $attributes;
  21. public function __construct(string $routingKey = null, int $flags = \AMQP_NOPARAM, array $attributes = [])
  22. {
  23. $this->routingKey = $routingKey;
  24. $this->flags = $flags;
  25. $this->attributes = $attributes;
  26. }
  27. public function getRoutingKey(): ?string
  28. {
  29. return $this->routingKey;
  30. }
  31. public function getFlags(): int
  32. {
  33. return $this->flags;
  34. }
  35. public function getAttributes(): array
  36. {
  37. return $this->attributes;
  38. }
  39. public static function createFromAmqpEnvelope(\AMQPEnvelope $amqpEnvelope, self $previousStamp = null): self
  40. {
  41. $attr = $previousStamp->attributes ?? [];
  42. $attr['headers'] = $attr['headers'] ?? $amqpEnvelope->getHeaders();
  43. $attr['content_type'] = $attr['content_type'] ?? $amqpEnvelope->getContentType();
  44. $attr['content_encoding'] = $attr['content_encoding'] ?? $amqpEnvelope->getContentEncoding();
  45. $attr['delivery_mode'] = $attr['delivery_mode'] ?? $amqpEnvelope->getDeliveryMode();
  46. $attr['priority'] = $attr['priority'] ?? $amqpEnvelope->getPriority();
  47. $attr['timestamp'] = $attr['timestamp'] ?? $amqpEnvelope->getTimestamp();
  48. $attr['app_id'] = $attr['app_id'] ?? $amqpEnvelope->getAppId();
  49. $attr['message_id'] = $attr['message_id'] ?? $amqpEnvelope->getMessageId();
  50. $attr['user_id'] = $attr['user_id'] ?? $amqpEnvelope->getUserId();
  51. $attr['expiration'] = $attr['expiration'] ?? $amqpEnvelope->getExpiration();
  52. $attr['type'] = $attr['type'] ?? $amqpEnvelope->getType();
  53. $attr['reply_to'] = $attr['reply_to'] ?? $amqpEnvelope->getReplyTo();
  54. return new self($previousStamp->routingKey ?? $amqpEnvelope->getRoutingKey(), $previousStamp->flags ?? \AMQP_NOPARAM, $attr);
  55. }
  56. public static function createWithAttributes(array $attributes, self $previousStamp = null): self
  57. {
  58. return new self(
  59. $previousStamp->routingKey ?? null,
  60. $previousStamp->flags ?? \AMQP_NOPARAM,
  61. array_merge($previousStamp->attributes ?? [], $attributes)
  62. );
  63. }
  64. }
  65. class_alias(AmqpStamp::class, \Symfony\Component\Messenger\Transport\AmqpExt\AmqpStamp::class);