|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Enqueue\SnsQs; |
| 6 | + |
| 7 | +use Enqueue\Dsn\Dsn; |
| 8 | +use Enqueue\Sns\SnsConnectionFactory; |
| 9 | +use Enqueue\Sqs\SqsConnectionFactory; |
| 10 | +use Interop\Queue\ConnectionFactory; |
| 11 | +use Interop\Queue\Context; |
| 12 | + |
| 13 | +class SnsQsConnectionFactory implements ConnectionFactory |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var string|array|null |
| 17 | + */ |
| 18 | + private $snsConfig; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var string|array|null |
| 22 | + */ |
| 23 | + private $sqsConfig; |
| 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 | + * snsqs: |
| 39 | + * snsqs:?key=aKey&secret=aSecret&token=aToken |
| 40 | + * |
| 41 | + * @param array|string|null $config |
| 42 | + */ |
| 43 | + public function __construct($config = 'snsqs:') |
| 44 | + { |
| 45 | + if (empty($config)) { |
| 46 | + $this->snsConfig = []; |
| 47 | + $this->sqsConfig = []; |
| 48 | + } elseif (is_string($config)) { |
| 49 | + $this->parseDsn($config); |
| 50 | + } elseif (is_array($config)) { |
| 51 | + if (array_key_exists('dsn', $config)) { |
| 52 | + $this->parseDsn($config['dsn']); |
| 53 | + } else { |
| 54 | + if (array_key_exists('sns', $config)) { |
| 55 | + $this->snsConfig = $config['sns']; |
| 56 | + } else { |
| 57 | + $this->snsConfig = $config; |
| 58 | + } |
| 59 | + |
| 60 | + if (array_key_exists('sqs', $config)) { |
| 61 | + $this->sqsConfig = $config['sqs']; |
| 62 | + } else { |
| 63 | + $this->sqsConfig = $config; |
| 64 | + } |
| 65 | + } |
| 66 | + } else { |
| 67 | + throw new \LogicException(sprintf('The config must be either an array of options, a DSN string, null or instance of %s', AwsSnsClient::class)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return SnsQsContext |
| 73 | + */ |
| 74 | + public function createContext(): Context |
| 75 | + { |
| 76 | + return new SnsQsContext(function() { |
| 77 | + return (new SnsConnectionFactory($this->snsConfig))->createContext(); |
| 78 | + }, function() { |
| 79 | + return (new SqsConnectionFactory($this->sqsConfig))->createContext(); |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + private function parseDsn(string $dsn): void |
| 84 | + { |
| 85 | + $dsn = Dsn::parseFirst($dsn); |
| 86 | + |
| 87 | + if ('snsqs' !== $dsn->getSchemeProtocol()) { |
| 88 | + throw new \LogicException(sprintf( |
| 89 | + 'The given scheme protocol "%s" is not supported. It must be "snsqs"', |
| 90 | + $dsn->getSchemeProtocol() |
| 91 | + )); |
| 92 | + } |
| 93 | + |
| 94 | + $this->snsConfig = 'sns:?'.$dsn->getQueryString(); |
| 95 | + $this->sqsConfig = 'sqs:?'.$dsn->getQueryString(); |
| 96 | + } |
| 97 | +} |
0 commit comments