-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSnsClient.php
145 lines (113 loc) · 3.69 KB
/
SnsClient.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
<?php
declare(strict_types=1);
namespace Enqueue\Sns;
use Aws\MultiRegionClient;
use Aws\Result;
use Aws\Sns\SnsClient as AwsSnsClient;
class SnsClient
{
/**
* @var AwsSnsClient
*/
private $singleClient;
/**
* @var MultiRegionClient
*/
private $multiClient;
/**
* @var callable
*/
private $inputClient;
/**
* @param AwsSnsClient|MultiRegionClient|callable $inputClient
*/
public function __construct($inputClient)
{
$this->inputClient = $inputClient;
}
public function createTopic(array $args): Result
{
return $this->callApi('createTopic', $args);
}
public function deleteTopic(string $topicArn): Result
{
return $this->callApi('DeleteTopic', [
'TopicArn' => $topicArn,
]);
}
public function publish(array $args): Result
{
return $this->callApi('publish', $args);
}
public function subscribe(array $args): Result
{
return $this->callApi('subscribe', $args);
}
public function unsubscribe(array $args): Result
{
return $this->callApi('unsubscribe', $args);
}
public function setSubscriptionAttributes(array $args): Result
{
return $this->callApi('setSubscriptionAttributes', $args);
}
public function listSubscriptionsByTopic(array $args): Result
{
return $this->callApi('ListSubscriptionsByTopic', $args);
}
public function getAWSClient(): AwsSnsClient
{
$this->resolveClient();
if ($this->singleClient) {
return $this->singleClient;
}
if ($this->multiClient) {
$mr = new \ReflectionMethod($this->multiClient, 'getClientFromPool');
$mr->setAccessible(true);
$singleClient = $mr->invoke($this->multiClient, $this->multiClient->getRegion());
$mr->setAccessible(false);
return $singleClient;
}
throw new \LogicException('The multi or single client must be set');
}
private function callApi(string $name, array $args): Result
{
$this->resolveClient();
if ($this->singleClient) {
if (false == empty($args['@region'])) {
throw new \LogicException('Cannot send message to another region because transport is configured with single aws client');
}
unset($args['@region']);
return call_user_func([$this->singleClient, $name], $args);
}
if ($this->multiClient) {
return call_user_func([$this->multiClient, $name], $args);
}
throw new \LogicException('The multi or single client must be set');
}
private function resolveClient(): void
{
if ($this->singleClient || $this->multiClient) {
return;
}
$client = $this->inputClient;
if ($client instanceof MultiRegionClient) {
$this->multiClient = $client;
return;
} elseif ($client instanceof AwsSnsClient) {
$this->singleClient = $client;
return;
} elseif (is_callable($client)) {
$client = call_user_func($client);
if ($client instanceof MultiRegionClient) {
$this->multiClient = $client;
return;
}
if ($client instanceof AwsSnsClient) {
$this->singleClient = $client;
return;
}
}
throw new \LogicException(sprintf('The input client must be an instance of "%s" or "%s" or a callable that returns one of those. Got "%s"', AwsSnsClient::class, MultiRegionClient::class, is_object($client) ? get_class($client) : gettype($client)));
}
}