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