-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventBridgeContext.php
249 lines (205 loc) · 6.72 KB
/
EventBridgeContext.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
namespace Cmrad\EbSQS;
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\Queue;
use Interop\Queue\SubscriptionConsumer;
class EventBridgeContext implements Context
{
private mixed $eventBusArn;
public function __construct(private EventBridgeClient $client, private array $config)
{
}
public function createMessage(string $body = '', array $properties = [], array $headers = []): EventBridgeMessage
{
return new EventBridgeMessage($body, $properties, $headers);
}
public function createTopic(string $topicName): EventBridgeEventBus
{
return new EventBridgeEventBus($topicName);
}
public function createQueue(string $queueName): EventBridgeEventBus
{
return new EventBridgeEventBus($queueName);
}
public function createTemporaryQueue(): Queue
{
throw TemporaryQueueNotSupportedException::providerDoestNotSupportIt();
}
public function createProducer(): EventBridgeProducer
{
return new EventBridgeProducer($this);
}
public function createConsumer(Destination $destination): Consumer
{
throw new \LogicException(
'EventBridge transport does not support consumption. You should consider using SQS instead.'
);
}
public function close(): void
{
}
public function purgeQueue(Queue $queue): void
{
PurgeQueueNotSupportedException::providerDoestNotSupportIt();
}
public function createSubscriptionConsumer(): SubscriptionConsumer
{
throw SubscriptionConsumerNotSupportedException::providerDoestNotSupportIt();
}
public function getClient(): EventBridgeClient
{
return $this->client;
}
public function getConfig()
{
return $this->config;
}
public function getSource(): array
{
return [$this->config['source']] ?? [];
}
public function declareEventBus(EventBridgeEventBus $eventBus): void
{
$result = $this->client->createEventBus(
[
'Name' => $eventBus->getEventBusName(),
]
);
if(false === $result->hasKey('EventBusArn')) {
throw new \RuntimeException(sprintf('Cannot create event bus: %s', $eventBus->getEventBusName()));
}
$this->eventBusArn[$eventBus->getEventBusName()] = $result['EventBusArn'];
}
public function getEventBusArn(EventBridgeEventBus $eventBus): string
{
if (!array_key_exists($eventBus->getEventBusName(), $this->eventBusArn)) {
$this->declareEventBus($eventBus);
}
return $this->eventBusArn[$eventBus->getEventBusName()];
}
public function setEventBusArn(EventBridgeEventBus $eventBus, string $arn): void
{
$this->eventBusArn[$eventBus->getEventBusName()] = $arn;
}
public function deleteEventBus(EventBridgeEventBus $eventBus): void
{
$this->client->deleteEventBus(
[
'Name' => $eventBus->getEventBusName(),
]
);
unset($this->eventBusArn[$eventBus->getEventBusName()]);
}
public function createRule(EventBridgeRule $rule): void
{
foreach ($this->getRules($rule->getEventBus()) as $ruleAWS) {
if ($ruleAWS['Name'] === $rule->getName()) {
return;
}
}
$pattern = [
'source' => $rule->getSource(),
];
if ($rule->getDetailType()) {
$pattern['detail-type'] = $rule->getDetailType();
}
var_dump($rule->getName());
$args = [
'Name' => $rule->getName(),
'EventPattern' => json_encode($pattern),
'EventBusName' => $rule->getEventBus()->getEventBusName(),
];
if (false === empty($rule->getAttributes())) {
$args['Tags'] = $rule->getAttributes();
}
$this->client->putRule($args);
}
public function deleteRule(EventBridgeRule $rule): void
{
$this->client->deleteRule(
[
'Name' => $rule->getName(),
'EventBusName' => $rule->getEventBus()->getEventBusName(),
]
);
}
public function createTarget(EventBridgeRuleTarget $target): void
{
$this->client->putTargets(
[
'Rule' => $target->getRule()->getName(),
'Targets' => [
[
'Id' => $target->getId(),
'Arn' => $target->getArn(),
],
],
'EventBusName' => $target->getRule()->getEventBus()->getEventBusName(),
]
);
}
public function createTargets(EventBridgeRule $rule, array $targets): void
{
$args = [
'Rule' => $rule->getName(),
'Targets' => array_map(
fn(EventBridgeRuleTarget $target) => [
'Id' => $target->getId(),
'Arn' => $target->getArn(),
],
$targets
),
'EventBusName' => $rule->getEventBus()->getEventBusName(),
];
$this->client->putTargets($args);
}
public function getRules(EventBridgeEventBus $eventBus): array
{
$args = [
'EventBusName' => $this->getEventBusArn($eventBus),
];
$rules = [];
while (true) {
$result = $this->client->listRules($args);
$rules = array_merge($rules, $result->get('Rules'));
if (false === $result->hasKey('NextToken')) {
break;
}
$args['NextToken'] = $result->get('NextToken');
}
return $rules;
}
public function deleteTargets(EventBridgeRule $rule): void
{
$listTargets = $this->client->listTargetsByRule(
[
'Rule' => $rule->getName(),
'EventBusName' => $rule->getEventBus()->getEventBusName(),
]
);
$args = [
'Rule' => $rule->getName(),
'EventBusName' => $rule->getEventBus()->getEventBusName(),
'Ids' => array_map(
fn(array $target) => $target['Id'],
$listTargets->get('Targets')
),
];
$this->client->removeTargets($args);
}
public function getRuleArn(EventBridgeRule $rule): ?string
{
$rules = $this->getRules($rule->getEventBus());
foreach ($rules as $ruleAWS) {
if ($ruleAWS['Name'] === $rule->getName()) {
return $ruleAWS['Arn'];
}
}
return null;
}
}