|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Enqueue\Sns; |
| 6 | + |
| 7 | +use Aws\Sdk; |
| 8 | +use Aws\Sns\SnsClient as AwsSnsClient; |
| 9 | +use Enqueue\Dsn\Dsn; |
| 10 | +use Interop\Queue\ConnectionFactory; |
| 11 | +use Interop\Queue\Context; |
| 12 | + |
| 13 | +class SnsConnectionFactory implements ConnectionFactory |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var array |
| 17 | + */ |
| 18 | + private $config; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var SnsClient |
| 22 | + */ |
| 23 | + private $client; |
| 24 | + |
| 25 | + /** |
| 26 | + * $config = [ |
| 27 | + * 'key' => null AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. |
| 28 | + * 'secret' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. |
| 29 | + * 'token' => null, AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment. |
| 30 | + * 'region' => null, (string, required) Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions. |
| 31 | + * 'version' => '2012-11-05', (string, required) The version of the webservice to utilize |
| 32 | + * 'lazy' => true, Enable lazy connection (boolean) |
| 33 | + * 'endpoint' => null (string, default=null) The full URI of the webservice. This is only required when connecting to a custom endpoint e.g. localstack |
| 34 | + * ]. |
| 35 | + * |
| 36 | + * or |
| 37 | + * |
| 38 | + * sns: |
| 39 | + * sns::?key=aKey&secret=aSecret&token=aToken |
| 40 | + * |
| 41 | + * @param array|string|SnsClient|null $config |
| 42 | + */ |
| 43 | + public function __construct($config = 'sns:') |
| 44 | + { |
| 45 | + if ($config instanceof AwsSnsClient) { |
| 46 | + $this->client = new SnsClient($config); |
| 47 | + $this->config = ['lazy' => false] + $this->defaultConfig(); |
| 48 | + |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (empty($config)) { |
| 53 | + $config = []; |
| 54 | + } elseif (is_string($config)) { |
| 55 | + $config = $this->parseDsn($config); |
| 56 | + } elseif (is_array($config)) { |
| 57 | + if (array_key_exists('dsn', $config)) { |
| 58 | + $config = array_replace_recursive($config, $this->parseDsn($config['dsn'])); |
| 59 | + |
| 60 | + unset($config['dsn']); |
| 61 | + } |
| 62 | + } else { |
| 63 | + throw new \LogicException(sprintf('The config must be either an array of options, a DSN string, null or instance of %s', AwsSnsClient::class)); |
| 64 | + } |
| 65 | + |
| 66 | + $this->config = array_replace($this->defaultConfig(), $config); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @return SnsContext |
| 71 | + */ |
| 72 | + public function createContext(): Context |
| 73 | + { |
| 74 | + return new SnsContext($this->establishConnection(), $this->config); |
| 75 | + } |
| 76 | + |
| 77 | + private function establishConnection(): SnsClient |
| 78 | + { |
| 79 | + if ($this->client) { |
| 80 | + return $this->client; |
| 81 | + } |
| 82 | + |
| 83 | + $config = [ |
| 84 | + 'version' => $this->config['version'], |
| 85 | + 'region' => $this->config['region'], |
| 86 | + ]; |
| 87 | + |
| 88 | + if (isset($this->config['endpoint'])) { |
| 89 | + $config['endpoint'] = $this->config['endpoint']; |
| 90 | + } |
| 91 | + |
| 92 | + if ($this->config['key'] && $this->config['secret']) { |
| 93 | + $config['credentials'] = [ |
| 94 | + 'key' => $this->config['key'], |
| 95 | + 'secret' => $this->config['secret'], |
| 96 | + ]; |
| 97 | + |
| 98 | + if ($this->config['token']) { |
| 99 | + $config['credentials']['token'] = $this->config['token']; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $establishConnection = function () use ($config) { |
| 104 | + return (new Sdk(['Sns' => $config]))->createMultiRegionSns(); |
| 105 | + }; |
| 106 | + |
| 107 | + $this->client = $this->config['lazy'] ? |
| 108 | + new SnsClient($establishConnection) : |
| 109 | + new SnsClient($establishConnection()) |
| 110 | + ; |
| 111 | + |
| 112 | + return $this->client; |
| 113 | + } |
| 114 | + |
| 115 | + private function parseDsn(string $dsn): array |
| 116 | + { |
| 117 | + $dsn = Dsn::parseFirst($dsn); |
| 118 | + |
| 119 | + if ('sns' !== $dsn->getSchemeProtocol()) { |
| 120 | + throw new \LogicException(sprintf( |
| 121 | + 'The given scheme protocol "%s" is not supported. It must be "sns"', |
| 122 | + $dsn->getSchemeProtocol() |
| 123 | + )); |
| 124 | + } |
| 125 | + |
| 126 | + return array_filter(array_replace($dsn->getQuery(), [ |
| 127 | + 'key' => $dsn->getString('key'), |
| 128 | + 'secret' => $dsn->getString('secret'), |
| 129 | + 'token' => $dsn->getString('token'), |
| 130 | + 'region' => $dsn->getString('region'), |
| 131 | + 'version' => $dsn->getString('version'), |
| 132 | + 'lazy' => $dsn->getBool('lazy'), |
| 133 | + 'endpoint' => $dsn->getString('endpoint'), |
| 134 | + ]), function ($value) { return null !== $value; }); |
| 135 | + } |
| 136 | + |
| 137 | + private function defaultConfig(): array |
| 138 | + { |
| 139 | + return [ |
| 140 | + 'key' => null, |
| 141 | + 'secret' => null, |
| 142 | + 'token' => null, |
| 143 | + 'region' => null, |
| 144 | + 'version' => '2010-03-31', |
| 145 | + 'lazy' => true, |
| 146 | + 'endpoint' => null, |
| 147 | + ]; |
| 148 | + } |
| 149 | +} |
0 commit comments