-
Notifications
You must be signed in to change notification settings - Fork 6
/
SnsQsContext.php
214 lines (180 loc) · 6.08 KB
/
SnsQsContext.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\SnsQs;
use Enqueue\Sns\SnsContext;
use Enqueue\Sns\SnsSubscribe;
use Enqueue\Sns\SnsUnsubscribe;
use Enqueue\Sqs\SqsContext;
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 SnsQsContext implements Context
{
/**
* @var SnsContext
*/
private $snsContext;
/**
* @var callable
*/
private $snsContextFactory;
/**
* @var SqsContext
*/
private $sqsContext;
/**
* @var callable
*/
private $sqsContextFactory;
/**
* @param SnsContext|callable $snsContext
* @param SqsContext|callable $sqsContext
*/
public function __construct($snsContext, $sqsContext)
{
if ($snsContext instanceof SnsContext) {
$this->snsContext = $snsContext;
} elseif (is_callable($snsContext)) {
$this->snsContextFactory = $snsContext;
} else {
throw new \InvalidArgumentException(sprintf('The $snsContext argument must be either %s or callable that returns %s once called.', SnsContext::class, SnsContext::class));
}
if ($sqsContext instanceof SqsContext) {
$this->sqsContext = $sqsContext;
} elseif (is_callable($sqsContext)) {
$this->sqsContextFactory = $sqsContext;
} else {
throw new \InvalidArgumentException(sprintf('The $sqsContext argument must be either %s or callable that returns %s once called.', SqsContext::class, SqsContext::class));
}
}
/**
* @return SnsQsMessage
*/
public function createMessage(string $body = '', array $properties = [], array $headers = []): Message
{
return new SnsQsMessage($body, $properties, $headers);
}
/**
* @return SnsQsTopic
*/
public function createTopic(string $topicName): Topic
{
return new SnsQsTopic($topicName);
}
/**
* @return SnsQsQueue
*/
public function createQueue(string $queueName): Queue
{
return new SnsQsQueue($queueName);
}
public function createTemporaryQueue(): Queue
{
throw TemporaryQueueNotSupportedException::providerDoestNotSupportIt();
}
public function createProducer(): Producer
{
return new SnsQsProducer($this->getSnsContext(), $this->getSqsContext());
}
/**
* @param SnsQsQueue $destination
*/
public function createConsumer(Destination $destination): Consumer
{
InvalidDestinationException::assertDestinationInstanceOf($destination, SnsQsQueue::class);
return new SnsQsConsumer($this, $this->getSqsContext()->createConsumer($destination), $destination);
}
/**
* @param SnsQsQueue $queue
*/
public function purgeQueue(Queue $queue): void
{
InvalidDestinationException::assertDestinationInstanceOf($queue, SnsQsQueue::class);
$this->getSqsContext()->purgeQueue($queue);
}
public function createSubscriptionConsumer(): SubscriptionConsumer
{
throw SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt();
}
public function declareTopic(SnsQsTopic $topic): void
{
$this->getSnsContext()->declareTopic($topic);
}
public function setTopicArn(SnsQsTopic $topic, string $arn): void
{
$this->getSnsContext()->setTopicArn($topic, $arn);
}
public function deleteTopic(SnsQsTopic $topic): void
{
$this->getSnsContext()->deleteTopic($topic);
}
public function declareQueue(SnsQsQueue $queue): void
{
$this->getSqsContext()->declareQueue($queue);
}
public function deleteQueue(SnsQsQueue $queue): void
{
$this->getSqsContext()->deleteQueue($queue);
}
public function bind(SnsQsTopic $topic, SnsQsQueue $queue): void
{
$this->getSnsContext()->subscribe(new SnsSubscribe(
$topic,
$this->getSqsContext()->getQueueArn($queue),
SnsSubscribe::PROTOCOL_SQS
));
}
public function unbind(SnsQsTopic $topic, SnsQsQueue $queue): void
{
$this->getSnsContext()->unsubscibe(new SnsUnsubscribe(
$topic,
$this->getSqsContext()->getQueueArn($queue),
SnsSubscribe::PROTOCOL_SQS
));
}
public function close(): void
{
$this->getSnsContext()->close();
$this->getSqsContext()->close();
}
public function setSubscriptionAttributes(SnsQsTopic $topic, SnsQsQueue $queue, array $attributes): void
{
$this->getSnsContext()->setSubscriptionAttributes(new SnsSubscribe(
$topic,
$this->getSqsContext()->getQueueArn($queue),
SnsSubscribe::PROTOCOL_SQS,
false,
$attributes,
));
}
private function getSnsContext(): SnsContext
{
if (null === $this->snsContext) {
$context = call_user_func($this->snsContextFactory);
if (false == $context instanceof SnsContext) {
throw new \LogicException(sprintf('The factory must return instance of %s. It returned %s', SnsContext::class, is_object($context) ? get_class($context) : gettype($context)));
}
$this->snsContext = $context;
}
return $this->snsContext;
}
private function getSqsContext(): SqsContext
{
if (null === $this->sqsContext) {
$context = call_user_func($this->sqsContextFactory);
if (false == $context instanceof SqsContext) {
throw new \LogicException(sprintf('The factory must return instance of %s. It returned %s', SqsContext::class, is_object($context) ? get_class($context) : gettype($context)));
}
$this->sqsContext = $context;
}
return $this->sqsContext;
}
}