From 8fae557807737d300abd4194632263c47486966c Mon Sep 17 00:00:00 2001 From: Ben Davies Date: Fri, 14 Jun 2019 12:53:44 +0100 Subject: [PATCH] deserialize sqs message attributes --- pkg/sqs/SqsConsumer.php | 4 ++++ pkg/sqs/SqsMessage.php | 21 +++++++++++++++++++++ pkg/sqs/Tests/SqsConsumerTest.php | 11 ++++++++++- pkg/sqs/Tests/SqsMessageTest.php | 15 +++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/pkg/sqs/SqsConsumer.php b/pkg/sqs/SqsConsumer.php index 4b89eb7ff..aa91f3df3 100644 --- a/pkg/sqs/SqsConsumer.php +++ b/pkg/sqs/SqsConsumer.php @@ -188,6 +188,10 @@ protected function convertMessage(array $sqsMessage): SqsMessage $message->setBody($sqsMessage['Body']); $message->setReceiptHandle($sqsMessage['ReceiptHandle']); + if (isset($sqsMessage['Attributes'])) { + $message->setAttributes($sqsMessage['Attributes']); + } + if (isset($sqsMessage['Attributes']['ApproximateReceiveCount'])) { $message->setRedelivered(((int) $sqsMessage['Attributes']['ApproximateReceiveCount']) > 1); } diff --git a/pkg/sqs/SqsMessage.php b/pkg/sqs/SqsMessage.php index 3b0f46bd9..cd2b1bdfe 100644 --- a/pkg/sqs/SqsMessage.php +++ b/pkg/sqs/SqsMessage.php @@ -23,6 +23,11 @@ class SqsMessage implements Message */ private $headers; + /** + * @var array + */ + private $attributes; + /** * @var bool */ @@ -58,6 +63,7 @@ public function __construct(string $body = '', array $properties = [], array $he $this->body = $body; $this->properties = $properties; $this->headers = $headers; + $this->attributes = []; $this->redelivered = false; $this->delaySeconds = 0; $this->requeueVisibilityTimeout = 0; @@ -113,6 +119,21 @@ public function getHeader(string $name, $default = null) return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default; } + public function setAttributes(array $attributes): void + { + $this->attributes = $attributes; + } + + public function getAttributes(): array + { + return $this->attributes; + } + + public function getAttribute(string $name, $default = null) + { + return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default; + } + public function isRedelivered(): bool { return $this->redelivered; diff --git a/pkg/sqs/Tests/SqsConsumerTest.php b/pkg/sqs/Tests/SqsConsumerTest.php index ea5c557b5..9fdded9e2 100644 --- a/pkg/sqs/Tests/SqsConsumerTest.php +++ b/pkg/sqs/Tests/SqsConsumerTest.php @@ -294,7 +294,10 @@ public function testShouldReceiveMessage() 'Body' => 'The Body', 'ReceiptHandle' => 'The Receipt', 'Attributes' => [ - 'ApproximateReceiveCount' => 3, + 'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:foo@bar.com', + 'ApproximateFirstReceiveTimestamp' => '1560512269481', + 'ApproximateReceiveCount' => '3', + 'SentTimestamp' => '1560512260079', ], 'MessageAttributes' => [ 'Headers' => [ @@ -336,6 +339,12 @@ public function testShouldReceiveMessage() $this->assertEquals('The Body', $result->getBody()); $this->assertEquals(['hkey' => 'hvalue'], $result->getHeaders()); $this->assertEquals(['key' => 'value'], $result->getProperties()); + $this->assertEquals([ + 'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:foo@bar.com', + 'ApproximateFirstReceiveTimestamp' => '1560512269481', + 'ApproximateReceiveCount' => '3', + 'SentTimestamp' => '1560512260079', + ], $result->getAttributes()); $this->assertTrue($result->isRedelivered()); $this->assertEquals('The Receipt', $result->getReceiptHandle()); } diff --git a/pkg/sqs/Tests/SqsMessageTest.php b/pkg/sqs/Tests/SqsMessageTest.php index a6d4e25fb..5da37b531 100644 --- a/pkg/sqs/Tests/SqsMessageTest.php +++ b/pkg/sqs/Tests/SqsMessageTest.php @@ -16,6 +16,7 @@ public function testCouldBeConstructedWithoutArguments() $this->assertSame('', $message->getBody()); $this->assertSame([], $message->getProperties()); $this->assertSame([], $message->getHeaders()); + $this->assertSame([], $message->getAttributes()); } public function testCouldBeConstructedWithOptionalArguments() @@ -90,4 +91,18 @@ public function testShouldAllowGetReceiptHandle() $this->assertSame('theId', $message->getReceiptHandle()); } + + public function testShouldAllowSettingAndGettingAttributes() + { + $message = new SqsMessage(); + $message->setAttributes($attributes = [ + 'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:foo@bar.com', + 'ApproximateFirstReceiveTimestamp' => '1560512269481', + 'ApproximateReceiveCount' => '2', + 'SentTimestamp' => '1560512260079', + ]); + + $this->assertSame($attributes, $message->getAttributes()); + $this->assertSame($attributes['SenderId'], $message->getAttribute('SenderId')); + } }