-
Notifications
You must be signed in to change notification settings - Fork 18
/
SqsClient.php
158 lines (124 loc) · 3.94 KB
/
SqsClient.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
<?php
declare(strict_types=1);
namespace Enqueue\Sqs;
use Aws\MultiRegionClient;
use Aws\Result;
use Aws\Sqs\SqsClient as AwsSqsClient;
class SqsClient
{
/**
* @var AwsSqsClient
*/
private $singleClient;
/**
* @var MultiRegionClient
*/
private $multiClient;
/**
* @var callable
*/
private $inputClient;
/**
* @param AwsSqsClient|MultiRegionClient|callable $inputClient
*/
public function __construct($inputClient)
{
$this->inputClient = $inputClient;
}
public function deleteMessage(array $args): Result
{
return $this->callApi('deleteMessage', $args);
}
public function receiveMessage(array $args): Result
{
return $this->callApi('receiveMessage', $args);
}
public function changeMessageVisibility(array $args): Result
{
return $this->callApi('changeMessageVisibility', $args);
}
public function purgeQueue(array $args): Result
{
return $this->callApi('purgeQueue', $args);
}
public function getQueueUrl(array $args): Result
{
return $this->callApi('getQueueUrl', $args);
}
public function getQueueAttributes(array $args): Result
{
return $this->callApi('getQueueAttributes', $args);
}
public function createQueue(array $args): Result
{
return $this->callApi('createQueue', $args);
}
public function deleteQueue(array $args): Result
{
return $this->callApi('deleteQueue', $args);
}
public function sendMessage(array $args): Result
{
return $this->callApi('sendMessage', $args);
}
public function getAWSClient(): AwsSqsClient
{
$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 AwsSqsClient) {
$this->singleClient = $client;
return;
} elseif (is_callable($client)) {
$client = call_user_func($client);
if ($client instanceof MultiRegionClient) {
$this->multiClient = $client;
return;
}
if ($client instanceof AwsSqsClient) {
$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"',
AwsSqsClient::class,
MultiRegionClient::class,
is_object($client) ? get_class($client) : gettype($client)
));
}
}