Skip to content

Commit

Permalink
Merge pull request #530 from php-enqueue/sqs-config-enhancement
Browse files Browse the repository at this point in the history
[sqs] Configuration enhancements
  • Loading branch information
makasim authored Sep 10, 2018
2 parents 5eeb4a9 + 5c9c6c1 commit 0282a6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
47 changes: 24 additions & 23 deletions pkg/sqs/SqsConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Enqueue\Sqs;

use Aws\Sqs\SqsClient;
use Enqueue\Dsn\Dsn;
use Interop\Queue\PsrConnectionFactory;
use Interop\Queue\PsrContext;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqs/Tests/SqsConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ 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());
}

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');
}

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');
}

/**
Expand Down
9 changes: 2 additions & 7 deletions pkg/sqs/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": {
Expand Down

0 comments on commit 0282a6c

Please sign in to comment.