-
Notifications
You must be signed in to change notification settings - Fork 18
/
SqsContext.php
212 lines (172 loc) · 5.71 KB
/
SqsContext.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
<?php
declare(strict_types=1);
namespace Enqueue\Sqs;
use Aws\Sqs\SqsClient as AwsSqsClient;
use Interop\Queue\Consumer;
use Interop\Queue\Context;
use Interop\Queue\Destination;
use Interop\Queue\Exception\InvalidDestinationException;
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 SqsContext implements Context
{
/**
* @var SqsClient
*/
private $client;
/**
* @var array
*/
private $queueUrls;
/**
* @var array
*/
private $queueArns;
/**
* @var array
*/
private $config;
public function __construct(SqsClient $client, array $config)
{
$this->client = $client;
$this->config = $config;
$this->queueUrls = [];
$this->queueArns = [];
}
/**
* @return SqsMessage
*/
public function createMessage(string $body = '', array $properties = [], array $headers = []): Message
{
return new SqsMessage($body, $properties, $headers);
}
/**
* @return SqsDestination
*/
public function createTopic(string $topicName): Topic
{
return new SqsDestination($topicName);
}
/**
* @return SqsDestination
*/
public function createQueue(string $queueName): Queue
{
return new SqsDestination($queueName);
}
public function createTemporaryQueue(): Queue
{
throw TemporaryQueueNotSupportedException::providerDoestNotSupportIt();
}
/**
* @return SqsProducer
*/
public function createProducer(): Producer
{
return new SqsProducer($this);
}
/**
* @param SqsDestination $destination
*
* @return SqsConsumer
*/
public function createConsumer(Destination $destination): Consumer
{
InvalidDestinationException::assertDestinationInstanceOf($destination, SqsDestination::class);
return new SqsConsumer($this, $destination);
}
public function close(): void
{
}
/**
* @param SqsDestination $queue
*/
public function purgeQueue(Queue $queue): void
{
InvalidDestinationException::assertDestinationInstanceOf($queue, SqsDestination::class);
$this->client->purgeQueue([
'@region' => $queue->getRegion(),
'QueueUrl' => $this->getQueueUrl($queue),
]);
}
public function createSubscriptionConsumer(): SubscriptionConsumer
{
throw SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt();
}
public function getAwsSqsClient(): AwsSqsClient
{
return $this->client->getAWSClient();
}
public function getSqsClient(): SqsClient
{
return $this->client;
}
/**
* @deprecated use getAwsSqsClient method
*/
public function getClient(): AwsSqsClient
{
@trigger_error('The method is deprecated since 0.9.2. SqsContext::getAwsSqsClient() method should be used.', E_USER_DEPRECATED);
return $this->getAwsSqsClient();
}
public function getQueueUrl(SqsDestination $destination): string
{
if (isset($this->queueUrls[$destination->getQueueName()])) {
return $this->queueUrls[$destination->getQueueName()];
}
$arguments = [
'@region' => $destination->getRegion(),
'QueueName' => $destination->getQueueName(),
];
if ($destination->getQueueOwnerAWSAccountId()) {
$arguments['QueueOwnerAWSAccountId'] = $destination->getQueueOwnerAWSAccountId();
} elseif (false == empty($this->config['queue_owner_aws_account_id'])) {
$arguments['QueueOwnerAWSAccountId'] = $this->config['queue_owner_aws_account_id'];
}
$result = $this->client->getQueueUrl($arguments);
if (false == $result->hasKey('QueueUrl')) {
throw new \RuntimeException(sprintf('QueueUrl cannot be resolved. queueName: "%s"', $destination->getQueueName()));
}
return $this->queueUrls[$destination->getQueueName()] = (string) $result->get('QueueUrl');
}
public function getQueueArn(SqsDestination $destination): string
{
if (isset($this->queueArns[$destination->getQueueName()])) {
return $this->queueArns[$destination->getQueueName()];
}
$arguments = [
'@region' => $destination->getRegion(),
'QueueUrl' => $this->getQueueUrl($destination),
'AttributeNames' => ['QueueArn'],
];
$result = $this->client->getQueueAttributes($arguments);
if (false == $arn = $result->search('Attributes.QueueArn')) {
throw new \RuntimeException(sprintf('QueueArn cannot be resolved. queueName: "%s"', $destination->getQueueName()));
}
return $this->queueArns[$destination->getQueueName()] = (string) $arn;
}
public function declareQueue(SqsDestination $dest): void
{
$result = $this->client->createQueue([
'@region' => $dest->getRegion(),
'Attributes' => $dest->getAttributes(),
'QueueName' => $dest->getQueueName(),
]);
if (false == $result->hasKey('QueueUrl')) {
throw new \RuntimeException(sprintf('Cannot create queue. queueName: "%s"', $dest->getQueueName()));
}
$this->queueUrls[$dest->getQueueName()] = $result->get('QueueUrl');
}
public function deleteQueue(SqsDestination $dest): void
{
$this->client->deleteQueue([
'QueueUrl' => $this->getQueueUrl($dest),
]);
unset($this->queueUrls[$dest->getQueueName()]);
}
}