Skip to content

Commit 309d3bd

Browse files
authored
Merge pull request #901 from bendavies/sqs-message-attributes
deserialize sqs message attributes
2 parents c6ba55c + 8fae557 commit 309d3bd

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

pkg/sqs/SqsConsumer.php

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ protected function convertMessage(array $sqsMessage): SqsMessage
188188
$message->setBody($sqsMessage['Body']);
189189
$message->setReceiptHandle($sqsMessage['ReceiptHandle']);
190190

191+
if (isset($sqsMessage['Attributes'])) {
192+
$message->setAttributes($sqsMessage['Attributes']);
193+
}
194+
191195
if (isset($sqsMessage['Attributes']['ApproximateReceiveCount'])) {
192196
$message->setRedelivered(((int) $sqsMessage['Attributes']['ApproximateReceiveCount']) > 1);
193197
}

pkg/sqs/SqsMessage.php

+21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class SqsMessage implements Message
2323
*/
2424
private $headers;
2525

26+
/**
27+
* @var array
28+
*/
29+
private $attributes;
30+
2631
/**
2732
* @var bool
2833
*/
@@ -58,6 +63,7 @@ public function __construct(string $body = '', array $properties = [], array $he
5863
$this->body = $body;
5964
$this->properties = $properties;
6065
$this->headers = $headers;
66+
$this->attributes = [];
6167
$this->redelivered = false;
6268
$this->delaySeconds = 0;
6369
$this->requeueVisibilityTimeout = 0;
@@ -113,6 +119,21 @@ public function getHeader(string $name, $default = null)
113119
return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default;
114120
}
115121

122+
public function setAttributes(array $attributes): void
123+
{
124+
$this->attributes = $attributes;
125+
}
126+
127+
public function getAttributes(): array
128+
{
129+
return $this->attributes;
130+
}
131+
132+
public function getAttribute(string $name, $default = null)
133+
{
134+
return array_key_exists($name, $this->attributes) ? $this->attributes[$name] : $default;
135+
}
136+
116137
public function isRedelivered(): bool
117138
{
118139
return $this->redelivered;

pkg/sqs/Tests/SqsConsumerTest.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,10 @@ public function testShouldReceiveMessage()
294294
'Body' => 'The Body',
295295
'ReceiptHandle' => 'The Receipt',
296296
'Attributes' => [
297-
'ApproximateReceiveCount' => 3,
297+
'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:[email protected]',
298+
'ApproximateFirstReceiveTimestamp' => '1560512269481',
299+
'ApproximateReceiveCount' => '3',
300+
'SentTimestamp' => '1560512260079',
298301
],
299302
'MessageAttributes' => [
300303
'Headers' => [
@@ -336,6 +339,12 @@ public function testShouldReceiveMessage()
336339
$this->assertEquals('The Body', $result->getBody());
337340
$this->assertEquals(['hkey' => 'hvalue'], $result->getHeaders());
338341
$this->assertEquals(['key' => 'value'], $result->getProperties());
342+
$this->assertEquals([
343+
'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:[email protected]',
344+
'ApproximateFirstReceiveTimestamp' => '1560512269481',
345+
'ApproximateReceiveCount' => '3',
346+
'SentTimestamp' => '1560512260079',
347+
], $result->getAttributes());
339348
$this->assertTrue($result->isRedelivered());
340349
$this->assertEquals('The Receipt', $result->getReceiptHandle());
341350
}

pkg/sqs/Tests/SqsMessageTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function testCouldBeConstructedWithoutArguments()
1616
$this->assertSame('', $message->getBody());
1717
$this->assertSame([], $message->getProperties());
1818
$this->assertSame([], $message->getHeaders());
19+
$this->assertSame([], $message->getAttributes());
1920
}
2021

2122
public function testCouldBeConstructedWithOptionalArguments()
@@ -90,4 +91,18 @@ public function testShouldAllowGetReceiptHandle()
9091

9192
$this->assertSame('theId', $message->getReceiptHandle());
9293
}
94+
95+
public function testShouldAllowSettingAndGettingAttributes()
96+
{
97+
$message = new SqsMessage();
98+
$message->setAttributes($attributes = [
99+
'SenderId' => 'AROAX5IAWYILCTYIS3OZ5:[email protected]',
100+
'ApproximateFirstReceiveTimestamp' => '1560512269481',
101+
'ApproximateReceiveCount' => '2',
102+
'SentTimestamp' => '1560512260079',
103+
]);
104+
105+
$this->assertSame($attributes, $message->getAttributes());
106+
$this->assertSame($attributes['SenderId'], $message->getAttribute('SenderId'));
107+
}
93108
}

0 commit comments

Comments
 (0)