From 5c9c6c19e6e0ceab54fdd81e999c482a6c0402ad Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Mon, 10 Sep 2018 11:27:03 +0300 Subject: [PATCH] [sqs] Configuration enhancements --- pkg/sqs/SqsConnectionFactory.php | 47 ++++++++++--------- .../Tests/SqsConnectionFactoryConfigTest.php | 8 ++-- pkg/sqs/composer.json | 9 +--- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php index 0280a773a..05690c19f 100644 --- a/pkg/sqs/SqsConnectionFactory.php +++ b/pkg/sqs/SqsConnectionFactory.php @@ -5,6 +5,7 @@ namespace Enqueue\Sqs; use Aws\Sqs\SqsClient; +use Enqueue\Dsn\Dsn; use Interop\Queue\PsrConnectionFactory; use Interop\Queue\PsrContext; @@ -46,19 +47,20 @@ public function __construct($config = 'sqs:') $this->config = ['lazy' => false] + $this->defaultConfig(); return; - } elseif (empty($config) || 'sqs:' === $config) { + } + + if (empty($config)) { $config = []; } elseif (is_string($config)) { $config = $this->parseDsn($config); } elseif (is_array($config)) { - $dsn = array_key_exists('dsn', $config) ? $config['dsn'] : null; - unset($config['dsn']); + if (array_key_exists('dsn', $config)) { + $config = array_replace_recursive($config, $this->parseDsn($config['dsn'])); - if ($dsn) { - $config = array_replace($config, $this->parseDsn($dsn)); + unset($config['dsn']); } } else { - throw new \LogicException('The config must be either an array of options, a DSN string or null'); + throw new \LogicException(sprintf('The config must be either an array of options, a DSN string, null or instance of %s', SqsClient::class)); } $this->config = array_replace($this->defaultConfig(), $config); @@ -112,26 +114,25 @@ private function establishConnection(): SqsClient private function parseDsn(string $dsn): array { - if (false === strpos($dsn, 'sqs:')) { - throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "sqs:".', $dsn)); - } - - if (false === $config = parse_url($dsn)) { - throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn)); - } - - if ($query = parse_url($dsn, PHP_URL_QUERY)) { - $queryConfig = []; - parse_str($query, $queryConfig); + $dsn = new Dsn($dsn); - $config = array_replace($queryConfig, $config); + if ('sqs' !== $dsn->getSchemeProtocol()) { + throw new \LogicException(sprintf( + 'The given scheme protocol "%s" is not supported. It must be "sqs"', + $dsn->getSchemeProtocol() + )); } - unset($config['query'], $config['scheme']); - - $config['lazy'] = empty($config['lazy']) ? false : true; - - return $config; + return array_filter(array_replace($dsn->getQuery(), [ + 'key' => $dsn->getQueryParameter('key'), + 'secret' => $dsn->getQueryParameter('secret'), + 'token' => $dsn->getQueryParameter('token'), + 'region' => $dsn->getQueryParameter('region'), + 'retries' => $dsn->getInt('retries'), + 'version' => $dsn->getQueryParameter('version'), + 'lazy' => $dsn->getBool('lazy'), + 'endpoint' => $dsn->getQueryParameter('endpoint'), + ]), function ($value) { return null !== $value; }); } private function defaultConfig(): array diff --git a/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php b/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php index 2bd54c56e..f2ad15948 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php @@ -16,7 +16,7 @@ class SqsConnectionFactoryConfigTest extends TestCase public function testThrowNeitherArrayStringNorNullGivenAsConfig() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The config must be either an array of options, a DSN string or null'); + $this->expectExceptionMessage('The config must be either an array of options, a DSN string, null or instance of Aws\Sqs\SqsClient'); new SqsConnectionFactory(new \stdClass()); } @@ -24,7 +24,7 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig() public function testThrowIfSchemeIsNotAmqp() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "sqs:".'); + $this->expectExceptionMessage('The given scheme protocol "http" is not supported. It must be "sqs"'); new SqsConnectionFactory('http://example.com'); } @@ -32,9 +32,9 @@ public function testThrowIfSchemeIsNotAmqp() public function testThrowIfDsnCouldNotBeParsed() { $this->expectException(\LogicException::class); - $this->expectExceptionMessage('Failed to parse DSN "sqs://:@/"'); + $this->expectExceptionMessage('The DSN is invalid.'); - new SqsConnectionFactory('sqs://:@/'); + new SqsConnectionFactory('foo'); } /** diff --git a/pkg/sqs/composer.json b/pkg/sqs/composer.json index 3a334e819..eac171369 100644 --- a/pkg/sqs/composer.json +++ b/pkg/sqs/composer.json @@ -8,15 +8,13 @@ "require": { "php": "^7.1.3", "queue-interop/queue-interop": "0.7.x-dev", + "enqueue/dsn": "0.9.x-dev", "aws/aws-sdk-php": "~3.26" }, "require-dev": { "phpunit/phpunit": "~5.4.0", "enqueue/test": "0.9.x-dev", - "enqueue/enqueue": "0.9.x-dev", - "queue-interop/queue-spec": "0.6.x-dev", - "symfony/dependency-injection": "^3.4|^4", - "symfony/config": "^3.4|^4" + "queue-interop/queue-spec": "0.6.x-dev" }, "support": { "email": "opensource@forma-pro.com", @@ -31,9 +29,6 @@ "/Tests/" ] }, - "suggest": { - "enqueue/enqueue": "If you'd like to use advanced features like Client abstract layer or Symfony integration features" - }, "minimum-stability": "dev", "extra": { "branch-alias": {