forked from php-enqueue/sns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnsContext.php
214 lines (176 loc) · 5.88 KB
/
SnsContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
declare(strict_types=1);
namespace Enqueue\Sns;
use Aws\Sns\SnsClient as AwsSnsClient;
use Interop\Queue\Consumer;
use Interop\Queue\Context;
use Interop\Queue\Destination;
use Interop\Queue\Exception\PurgeQueueNotSupportedException;
use Interop\Queue\Exception\SubscriptionConsumerNotSupportedException;
use Interop\Queue\Exception\TemporaryQueueNotSupportedException;
use Interop\Queue\Message;
use Interop\Queue\Producer;
use Interop\Queue\Queue;
use Interop\Queue\SubscriptionConsumer;
use Interop\Queue\Topic;
class SnsContext implements Context
{
/**
* @var SnsClient
*/
private $client;
/**
* @var array
*/
private $config;
private $topicArns;
public function __construct(SnsClient $client, array $config)
{
$this->client = $client;
$this->config = $config;
$this->topicArns = $config['topic_arns'] ?? [];
}
/**
* @return SnsMessage
*/
public function createMessage(string $body = '', array $properties = [], array $headers = []): Message
{
return new SnsMessage($body, $properties, $headers);
}
/**
* @return SnsDestination
*/
public function createTopic(string $topicName): Topic
{
return new SnsDestination($topicName);
}
/**
* @return SnsDestination
*/
public function createQueue(string $queueName): Queue
{
return new SnsDestination($queueName);
}
public function declareTopic(SnsDestination $destination): void
{
$result = $this->client->createTopic([
'Attributes' => $destination->getAttributes(),
'Name' => $destination->getQueueName(),
]);
if (false == $result->hasKey('TopicArn')) {
throw new \RuntimeException(sprintf('Cannot create topic. topicName: "%s"', $destination->getTopicName()));
}
$this->topicArns[$destination->getTopicName()] = (string) $result->get('TopicArn');
}
public function setTopicArn(SnsDestination $destination, string $arn): void
{
$this->topicArns[$destination->getTopicName()] = $arn;
}
public function deleteTopic(SnsDestination $destination): void
{
$this->client->deleteTopic($this->getTopicArn($destination));
unset($this->topicArns[$destination->getTopicName()]);
}
public function subscribe(SnsSubscribe $subscribe): void
{
foreach ($this->getSubscriptions($subscribe->getTopic()) as $subscription) {
if ($subscription['Protocol'] === $subscribe->getProtocol()
&& $subscription['Endpoint'] === $subscribe->getEndpoint()) {
return;
}
}
$this->client->subscribe([
'Attributes' => $subscribe->getAttributes(),
'Endpoint' => $subscribe->getEndpoint(),
'Protocol' => $subscribe->getProtocol(),
'ReturnSubscriptionArn' => $subscribe->isReturnSubscriptionArn(),
'TopicArn' => $this->getTopicArn($subscribe->getTopic()),
]);
}
public function unsubscibe(SnsUnsubscribe $unsubscribe): void
{
foreach ($this->getSubscriptions($unsubscribe->getTopic()) as $subscription) {
if ($subscription['Protocol'] != $unsubscribe->getProtocol()) {
continue;
}
if ($subscription['Endpoint'] != $unsubscribe->getEndpoint()) {
continue;
}
$this->client->unsubscribe([
'SubscriptionArn' => $subscription['SubscriptionArn'],
]);
}
}
public function getSubscriptions(SnsDestination $destination): array
{
$args = [
'TopicArn' => $this->getTopicArn($destination),
];
$subscriptions = [];
while (true) {
$result = $this->client->listSubscriptionsByTopic($args);
$subscriptions = array_merge($subscriptions, $result->get('Subscriptions'));
if (false == $result->hasKey('NextToken')) {
break;
}
$args['NextToken'] = $result->get('NextToken');
}
return $subscriptions;
}
public function setSubscriptionAttributes(SnsSubscribe $subscribe): void
{
foreach ($this->getSubscriptions($subscribe->getTopic()) as $subscription) {
$this->client->setSubscriptionAttributes(array_merge(
$subscribe->getAttributes(),
['SubscriptionArn' => $subscription['SubscriptionArn']],
));
}
}
public function getTopicArn(SnsDestination $destination): string
{
if (false == array_key_exists($destination->getTopicName(), $this->topicArns)) {
$this->declareTopic($destination);
}
return $this->topicArns[$destination->getTopicName()];
}
public function createTemporaryQueue(): Queue
{
throw TemporaryQueueNotSupportedException::providerDoestNotSupportIt();
}
/**
* @return SnsProducer
*/
public function createProducer(): Producer
{
return new SnsProducer($this);
}
/**
* @param SnsDestination $destination
*/
public function createConsumer(Destination $destination): Consumer
{
throw new \LogicException('SNS transport does not support consumption. You should consider using SQS instead.');
}
public function close(): void
{
}
/**
* @param SnsDestination $queue
*/
public function purgeQueue(Queue $queue): void
{
PurgeQueueNotSupportedException::providerDoestNotSupportIt();
}
public function createSubscriptionConsumer(): SubscriptionConsumer
{
throw SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt();
}
public function getAwsSnsClient(): AwsSnsClient
{
return $this->client->getAWSClient();
}
public function getSnsClient(): SnsClient
{
return $this->client;
}
}