Skip to content

[SNSQS] added possibility to send message attributes using snsqs transport #1195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions pkg/snsqs/SnsQsMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,27 @@ class SnsQsMessage implements Message
*/
private $sqsMessage;

public function __construct(string $body = '', array $properties = [], array $headers = [])
{
/**
* @var array|null
*/
private $messageAttributes;

/**
* See AWS documentation for message attribute structure.
*
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#shape-messageattributevalue
*/
public function __construct(
string $body = '',
array $properties = [],
array $headers = [],
array $messageAttributes = null
) {
$this->body = $body;
$this->properties = $properties;
$this->headers = $headers;
$this->redelivered = false;
$this->messageAttributes = $messageAttributes;
}

public function setSqsMessage(SqsMessage $message): void
Expand All @@ -34,4 +49,14 @@ public function getSqsMessage(): SqsMessage
{
return $this->sqsMessage;
}

public function getMessageAttributes(): ?array
{
return $this->messageAttributes;
}

public function setMessageAttributes(?array $messageAttributes): void
{
$this->messageAttributes = $messageAttributes;
}
}
13 changes: 2 additions & 11 deletions pkg/snsqs/SnsQsProducer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ public function send(Destination $destination, Message $message): void
InvalidMessageException::assertMessageInstanceOf($message, SnsQsMessage::class);

if (false == $destination instanceof SnsQsTopic && false == $destination instanceof SnsQsQueue) {
throw new InvalidDestinationException(sprintf(
'The destination must be an instance of [%s|%s] but got %s.',
SnsQsTopic::class, SnsQsQueue::class,
is_object($destination) ? get_class($destination) : gettype($destination)
));
throw new InvalidDestinationException(sprintf('The destination must be an instance of [%s|%s] but got %s.', SnsQsTopic::class, SnsQsQueue::class, is_object($destination) ? get_class($destination) : gettype($destination)));
}

if ($destination instanceof SnsQsTopic) {
Expand All @@ -64,6 +60,7 @@ public function send(Destination $destination, Message $message): void
$message->getProperties(),
$message->getHeaders()
);
$snsMessage->setMessageAttributes($message->getMessageAttributes());

$this->getSnsProducer()->send($destination, $snsMessage);
} else {
Expand All @@ -79,10 +76,6 @@ public function send(Destination $destination, Message $message): void

/**
* Delivery delay is supported by SQSProducer.
*
* @param int|null $deliveryDelay
*
* @return Producer
*/
public function setDeliveryDelay(int $deliveryDelay = null): Producer
{
Expand All @@ -93,8 +86,6 @@ public function setDeliveryDelay(int $deliveryDelay = null): Producer

/**
* Delivery delay is supported by SQSProducer.
*
* @return int|null
*/
public function getDeliveryDelay(): ?int
{
Expand Down
22 changes: 22 additions & 0 deletions pkg/snsqs/Tests/SnsQsProducerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Enqueue\SnsQs\Tests;

use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsMessage;
use Enqueue\Sns\SnsProducer;
use Enqueue\SnsQs\SnsQsMessage;
use Enqueue\SnsQs\SnsQsProducer;
Expand Down Expand Up @@ -91,6 +92,7 @@ public function testShouldGetDeliveryDelayFromSQSProducer()
public function testShouldSendSnsTopicMessageToSnsProducer()
{
$snsMock = $this->createSnsContextMock();
$snsMock->method('createMessage')->willReturn(new SnsMessage());
$destination = new SnsQsTopic('');

$snsProducerStub = $this->prophesize(SnsProducer::class);
Expand All @@ -102,6 +104,26 @@ public function testShouldSendSnsTopicMessageToSnsProducer()
$producer->send($destination, new SnsQsMessage());
}

public function testShouldSendSnsTopicMessageWithAttributesToSnsProducer()
{
$snsMock = $this->createSnsContextMock();
$snsMock->method('createMessage')->willReturn(new SnsMessage());
$destination = new SnsQsTopic('');

$snsProducerStub = $this->prophesize(SnsProducer::class);
$snsProducerStub->send(
$destination,
Argument::that(function (SnsMessage $snsMessage) {
return $snsMessage->getMessageAttributes() === ['foo' => 'bar'];
})
)->shouldBeCalledOnce();

$snsMock->method('createProducer')->willReturn($snsProducerStub->reveal());

$producer = new SnsQsProducer($snsMock, $this->createSqsContextMock());
$producer->send($destination, new SnsQsMessage('', [], [], ['foo' => 'bar']));
}

public function testShouldSendSqsMessageToSqsProducer()
{
$sqsMock = $this->createSqsContextMock();
Expand Down