|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Enqueue\Sqs; |
| 6 | + |
| 7 | +use Aws\MultiRegionClient; |
| 8 | +use Aws\Result; |
| 9 | +use Aws\Sqs\SqsClient as AwsSqsClient; |
| 10 | + |
| 11 | +class SqsClient |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var AwsSqsClient |
| 15 | + */ |
| 16 | + private $singleClient; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var MultiRegionClient |
| 20 | + */ |
| 21 | + private $multiClient; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var callable |
| 25 | + */ |
| 26 | + private $inputClient; |
| 27 | + |
| 28 | + public function __construct($inputClient) |
| 29 | + { |
| 30 | + $this->inputClient = is_callable($inputClient) ? $inputClient : function () use ($inputClient) { |
| 31 | + return $inputClient; |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + public function deleteMessage(array $args): Result |
| 36 | + { |
| 37 | + return $this->callApi('deleteMessage', $args); |
| 38 | + } |
| 39 | + |
| 40 | + public function receiveMessage(array $args): Result |
| 41 | + { |
| 42 | + return $this->callApi('receiveMessage', $args); |
| 43 | + } |
| 44 | + |
| 45 | + public function purgeQueue(array $args): Result |
| 46 | + { |
| 47 | + return $this->callApi('purgeQueue', $args); |
| 48 | + } |
| 49 | + |
| 50 | + public function getQueueUrl(array $args): Result |
| 51 | + { |
| 52 | + return $this->callApi('getQueueUrl', $args); |
| 53 | + } |
| 54 | + |
| 55 | + public function createQueue(array $args): Result |
| 56 | + { |
| 57 | + return $this->callApi('createQueue', $args); |
| 58 | + } |
| 59 | + |
| 60 | + public function deleteQueue(array $args): Result |
| 61 | + { |
| 62 | + return $this->callApi('deleteQueue', $args); |
| 63 | + } |
| 64 | + |
| 65 | + public function sendMessage(array $args): Result |
| 66 | + { |
| 67 | + return $this->callApi('sendMessage', $args); |
| 68 | + } |
| 69 | + |
| 70 | + public function getAWSClient(): AwsSqsClient |
| 71 | + { |
| 72 | + $this->resolveClient(); |
| 73 | + |
| 74 | + if ($this->singleClient) { |
| 75 | + return $this->singleClient; |
| 76 | + } |
| 77 | + |
| 78 | + if ($this->multiClient) { |
| 79 | + $mr = new \ReflectionMethod($this->multiClient, 'getClientFromPool'); |
| 80 | + $mr->setAccessible(true); |
| 81 | + $singleClient = $mr->invoke($this->multiClient, $this->multiClient->getRegion()); |
| 82 | + $mr->setAccessible(false); |
| 83 | + |
| 84 | + return $singleClient; |
| 85 | + } |
| 86 | + |
| 87 | + throw new \LogicException('The multi or single client must be set'); |
| 88 | + } |
| 89 | + |
| 90 | + private function callApi(string $name, array $args): Result |
| 91 | + { |
| 92 | + if ($this->singleClient) { |
| 93 | + unset($args['@region']); |
| 94 | + |
| 95 | + return call_user_func([$this->singleClient, $name], $args); |
| 96 | + } |
| 97 | + |
| 98 | + if ($this->multiClient) { |
| 99 | + return call_user_func([$this->multiClient, $name], $args); |
| 100 | + } |
| 101 | + |
| 102 | + throw new \LogicException('The multi or single client must be set'); |
| 103 | + } |
| 104 | + |
| 105 | + private function resolveClient(): void |
| 106 | + { |
| 107 | + if ($this->singleClient || $this->multiClient) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + $client = call_user_func($this->inputClient); |
| 112 | + if ($client instanceof MultiRegionClient) { |
| 113 | + $this->multiClient = $client; |
| 114 | + |
| 115 | + return; |
| 116 | + } |
| 117 | + if ($client instanceof AwsSqsClient) { |
| 118 | + $this->singleClient = $client; |
| 119 | + |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + throw new \LogicException(sprintf( |
| 124 | + 'The input client must be an instance of "%s" or "%s" or a callable that returns one of those. Got "%s"', |
| 125 | + AwsSqsClient::class, |
| 126 | + MultiRegionClient::class, |
| 127 | + is_object($client) ? get_class($client) : gettype($client) |
| 128 | + )); |
| 129 | + } |
| 130 | +} |
0 commit comments