Skip to content

Commit 5c3c0d8

Browse files
authored
Merge pull request #799 from aldenw/master
Allow setting SNS message attributes, other fields
2 parents 50a1a56 + ce8388a commit 5c3c0d8

3 files changed

Lines changed: 273 additions & 2 deletions

File tree

‎pkg/sns/SnsMessage.php‎

Lines changed: 192 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,218 @@
66

77
use Interop\Queue\Impl\MessageTrait;
88
use Interop\Queue\Message;
9+
use Psr\Http\Message\StreamInterface;
910

1011
class SnsMessage implements Message
1112
{
1213
use MessageTrait;
1314

15+
/**
16+
* @var string|null
17+
*/
1418
private $snsMessageId;
1519

16-
public function __construct(string $body = '', array $properties = [], array $headers = [])
17-
{
20+
/**
21+
* @var string|null
22+
*/
23+
private $messageStructure;
24+
25+
/**
26+
* @var string|null
27+
*/
28+
private $phoneNumber;
29+
30+
/**
31+
* @var string|null
32+
*/
33+
private $subject;
34+
35+
/**
36+
* @var array|null
37+
*/
38+
private $messageAttributes;
39+
40+
/**
41+
* @var string|null
42+
*/
43+
private $targetArn;
44+
45+
/**
46+
* SnsMessage constructor.
47+
*
48+
* See AWS documentation for message attribute structure.
49+
*
50+
* @see https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-sns-2010-03-31.html#shape-messageattributevalue
51+
*
52+
* @param string $body
53+
* @param array $properties
54+
* @param array $headers
55+
* @param array|null $messageAttributes
56+
* @param string|null $messageStructure
57+
* @param string|null $phoneNumber
58+
* @param string|null $subject
59+
* @param string|null $targetArn
60+
*/
61+
public function __construct(
62+
string $body = '',
63+
array $properties = [],
64+
array $headers = [],
65+
array $messageAttributes = null,
66+
string $messageStructure = null,
67+
string $phoneNumber = null,
68+
string $subject = null,
69+
string $targetArn = null
70+
) {
1871
$this->body = $body;
1972
$this->properties = $properties;
2073
$this->headers = $headers;
74+
$this->messageAttributes = $messageAttributes;
75+
$this->messageStructure = $messageStructure;
76+
$this->phoneNumber = $phoneNumber;
77+
$this->subject = $subject;
78+
$this->targetArn = $targetArn;
2179
$this->redelivered = false;
2280
}
2381

82+
/**
83+
* @return string|null
84+
*/
2485
public function getSnsMessageId(): ?string
2586
{
2687
return $this->snsMessageId;
2788
}
2889

90+
/**
91+
* @param string|null $snsMessageId
92+
*/
2993
public function setSnsMessageId(?string $snsMessageId): void
3094
{
3195
$this->snsMessageId = $snsMessageId;
3296
}
97+
98+
/**
99+
* @return string|null
100+
*/
101+
public function getMessageStructure(): ?string
102+
{
103+
return $this->messageStructure;
104+
}
105+
106+
/**
107+
* @param string|null $messageStructure
108+
*/
109+
public function setMessageStructure(?string $messageStructure): void
110+
{
111+
$this->messageStructure = $messageStructure;
112+
}
113+
114+
/**
115+
* @return string|null
116+
*/
117+
public function getPhoneNumber(): ?string
118+
{
119+
return $this->phoneNumber;
120+
}
121+
122+
/**
123+
* @param string|null $phoneNumber
124+
*/
125+
public function setPhoneNumber(?string $phoneNumber): void
126+
{
127+
$this->phoneNumber = $phoneNumber;
128+
}
129+
130+
/**
131+
* @return string|null
132+
*/
133+
public function getSubject(): ?string
134+
{
135+
return $this->subject;
136+
}
137+
138+
/**
139+
* @param string|null $subject
140+
*/
141+
public function setSubject(?string $subject): void
142+
{
143+
$this->subject = $subject;
144+
}
145+
146+
/**
147+
* @return array|null
148+
*/
149+
public function getMessageAttributes(): ?array
150+
{
151+
return $this->messageAttributes;
152+
}
153+
154+
/**
155+
* @param array|null $messageAttributes
156+
*/
157+
public function setMessageAttributes(?array $messageAttributes): void
158+
{
159+
$this->messageAttributes = $messageAttributes;
160+
}
161+
162+
/**
163+
* @param string $name
164+
* @param null $default
165+
*
166+
* @return array|null
167+
*/
168+
public function getAttribute(string $name, $default = null)
169+
{
170+
return array_key_exists($name, $this->messageAttributes) ? $this->messageAttributes[$name] : $default;
171+
}
172+
173+
/**
174+
* Attribute array format:
175+
* [
176+
* 'BinaryValue' => <string || resource || Psr\Http\Message\StreamInterface>,
177+
* 'DataType' => '<string>', // REQUIRED
178+
* 'StringValue' => '<string>',
179+
* ].
180+
*
181+
* @param string $name
182+
* @param array|null $attribute
183+
*/
184+
public function setAttribute(string $name, ?array $attribute): void
185+
{
186+
if (null === $attribute) {
187+
unset($this->messageAttributes[$name]);
188+
} else {
189+
$this->messageAttributes[$name] = $attribute;
190+
}
191+
}
192+
193+
/**
194+
* @param string $name
195+
* @param string $dataType String, String.Array, Number, or Binary
196+
* @param string|resource|StreamInterface $value
197+
*/
198+
public function addAttribute(string $name, string $dataType, $value): void
199+
{
200+
$valueKey = 'Binary' === $dataType ? 'BinaryValue' : 'StringValue';
201+
202+
$this->messageAttributes[$name] = [
203+
'DataType' => $dataType,
204+
$valueKey => $value,
205+
];
206+
}
207+
208+
/**
209+
* @return string|null
210+
*/
211+
public function getTargetArn(): ?string
212+
{
213+
return $this->targetArn;
214+
}
215+
216+
/**
217+
* @param string|null $targetArn
218+
*/
219+
public function setTargetArn(?string $targetArn): void
220+
{
221+
$this->targetArn = $targetArn;
222+
}
33223
}

‎pkg/sns/SnsProducer.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ public function send(Destination $destination, Message $message): void
5757
'TopicArn' => $topicArn,
5858
];
5959

60+
if (null !== $message->getMessageAttributes()) {
61+
$arguments['MessageAttributes'] = array_merge(
62+
$arguments['MessageAttributes'],
63+
$message->getMessageAttributes()
64+
);
65+
}
66+
67+
if (null !== ($structure = $message->getMessageStructure())) {
68+
$arguments['MessageStructure'] = $structure;
69+
}
70+
if (null !== ($phone = $message->getPhoneNumber())) {
71+
$arguments['PhoneNumber'] = $phone;
72+
}
73+
if (null !== ($subject = $message->getSubject())) {
74+
$arguments['Subject'] = $subject;
75+
}
76+
if (null !== ($targetArn = $message->getTargetArn())) {
77+
$arguments['TargetArn'] = $targetArn;
78+
}
79+
6080
$result = $this->context->getSnsClient()->publish($arguments);
6181

6282
if (false == $result->hasKey('MessageId')) {

‎pkg/sns/Tests/SnsProducerTest.php‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,67 @@ public function testShouldPublish()
126126
$producer->send($destination, $message);
127127
}
128128

129+
/**
130+
* @throws InvalidMessageException
131+
*/
132+
public function testShouldPublishWithMergedAttributes()
133+
{
134+
$context = $this->createSnsContextMock();
135+
$client = $this->createSnsClientMock();
136+
137+
$context
138+
->expects($this->once())
139+
->method('getSnsClient')
140+
->will($this->returnValue($client));
141+
142+
$expectedArgument = [
143+
'Message' => 'message',
144+
'MessageAttributes' => [
145+
'Headers' => [
146+
'DataType' => 'String',
147+
'StringValue' => '[[],[]]',
148+
],
149+
'Foo' => [
150+
'DataType' => 'String',
151+
'StringValue' => 'foo-value',
152+
],
153+
'Bar' => [
154+
'DataType' => 'Binary',
155+
'BinaryValue' => 'bar-val',
156+
],
157+
],
158+
'TopicArn' => '',
159+
'MessageStructure' => 'structure',
160+
'PhoneNumber' => 'phone',
161+
'Subject' => 'subject',
162+
'TargetArn' => 'target_arn',
163+
];
164+
165+
$client
166+
->expects($this->once())
167+
->method('publish')
168+
->with($this->identicalTo($expectedArgument))
169+
->willReturn(new Result(['MessageId' => 'theMessageId']));
170+
171+
$attributes = [
172+
'Foo' => [
173+
'DataType' => 'String',
174+
'StringValue' => 'foo-value',
175+
],
176+
];
177+
178+
$message = new SnsMessage(
179+
'message', [], [], $attributes, 'structure', 'phone',
180+
'subject', 'target_arn'
181+
);
182+
$message->addAttribute('Bar', 'Binary', 'bar-val');
183+
184+
$destination = new SnsDestination('queue-name');
185+
186+
$producer = new SnsProducer($context);
187+
$producer->send($destination, $message);
188+
}
189+
129190
/**
130191
* @return \PHPUnit_Framework_MockObject_MockObject|SnsContext
131192
*/

0 commit comments

Comments
 (0)