Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add support for using a pre-configured client with the SQS driver #444

Merged
merged 9 commits into from
Jul 10, 2018
1 change: 1 addition & 0 deletions docs/bundle/config_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ enqueue:
polling_interval: 1000
lazy: true
sqs:
client: null
key: null
secret: null
token: null
Expand Down
6 changes: 5 additions & 1 deletion docs/transport/sqs.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ $factory = new SqsConnectionFactory('sqs:?key=aKey&secret=aSecret&region=aRegion

$psrContext = $factory->createContext();

// using a pre-configured client
$client = new Aws\Sqs\SqsClient([ /* ... */ ]);
$factory = new SqsConnectionFactory($client);

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('sqs:');
```
Expand Down Expand Up @@ -109,4 +113,4 @@ $fooQueue = $psrContext->createQueue('foo');
$psrContext->purge($fooQueue);
```

[back to index](../index.md)
[back to index](../index.md)
15 changes: 14 additions & 1 deletion pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
parameters:
locale: 'en'
secret: 'ThisTokenIsNotSoSecretChangeIt'

env(AWS_SQS_REGION): 'us-east-1'
env(AWS_SQS_KEY): 'key'
env(AWS_SQS_SECRET): 'secret'

framework:
#esi: ~
Expand Down Expand Up @@ -33,3 +35,14 @@ services:
public: true
tags:
- { name: 'enqueue.client.processor' }

test.sqs_client:
public: true
class: Aws\Sqs\SqsClient
arguments:
-
region: '%env(AWS_SQS_REGION)%'
version: '2012-11-05'
credentials:
key: '%env(AWS_SQS_KEY)%'
secret: '%env(AWS_SQS_SECRET)%'
9 changes: 9 additions & 0 deletions pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,15 @@ public function provideEnqueueConfigs()
],
],
]];

yield 'sqs_client' => [[
'transport' => [
'default' => 'sqs',
'sqs' => [
'client' => 'test.sqs_client',
],
],
]];
}

yield 'mongodb_dsn' => [[
Expand Down
9 changes: 7 additions & 2 deletions pkg/sqs/SqsConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ class SqsConnectionFactory implements PsrConnectionFactory
* sqs:
* sqs::?key=aKey&secret=aSecret&token=aToken
*
* @param array|string|null $config
* @param array|string|SqsClient|null $config
*/
public function __construct($config = 'sqs:')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the constructor should accept an instance of Client, no need to pass it as an array parameter. We would not be able to apply other options in the array in any case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The symfony transport should have it as a client option though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

{
if (empty($config) || 'sqs:' === $config) {
if ($config instanceof SqsClient) {
$this->client = $config;
$this->config = ['lazy' => false] + $this->defaultConfig();

return;
} elseif (empty($config) || 'sqs:' === $config) {
$config = [];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
Expand Down
7 changes: 5 additions & 2 deletions pkg/sqs/Symfony/SqsTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->children()
->scalarNode('client')->defaultNull()->end()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect there to be something like the following in createConnectionFactory() below:

if (false == empty($config['client'])) {
    $config['client'] = new Reference($config['client']);
}

->scalarNode('key')->defaultNull()->end()
->scalarNode('secret')->defaultNull()->end()
->scalarNode('token')->defaultNull()->end()
->scalarNode('region')->isRequired()->end()
->scalarNode('region')->end()
->integerNode('retries')->defaultValue(3)->end()
->scalarNode('version')->cannotBeEmpty()->defaultValue('2012-11-05')->end()
->booleanNode('lazy')
Expand All @@ -53,8 +54,10 @@ public function addConfiguration(ArrayNodeDefinition $builder)
*/
public function createConnectionFactory(ContainerBuilder $container, array $config)
{
$arguments = empty($config['client']) ? $config : new Reference($config['client']);

$factory = new Definition(SqsConnectionFactory::class);
$factory->setArguments([$config]);
$factory->setArguments([$arguments]);

$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
$container->setDefinition($factoryId, $factory);
Expand Down
13 changes: 13 additions & 0 deletions pkg/sqs/Tests/SqsConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enqueue\Sqs\Tests;

use Aws\Sqs\SqsClient;
use Enqueue\Sqs\SqsConnectionFactory;
use Enqueue\Sqs\SqsContext;
use Enqueue\Test\ClassExtensionTrait;
Expand Down Expand Up @@ -48,6 +49,18 @@ public function testCouldBeConstructedWithCustomConfiguration()
], 'config', $factory);
}

public function testCouldBeConstructedWithClient()
{
$client = $this->createMock(SqsClient::class);

$factory = new SqsConnectionFactory($client);

$context = $factory->createContext();

$this->assertInstanceOf(SqsContext::class, $context);
$this->assertAttributeSame($client, 'client', $context);
}

public function testShouldCreateLazyContext()
{
$factory = new SqsConnectionFactory(['lazy' => true]);
Expand Down
1 change: 1 addition & 0 deletions pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function testShouldAllowAddConfiguration()
'version' => 'theVersion',
'lazy' => false,
'endpoint' => 'theEndpoint',
'client' => null,
], $config);
}

Expand Down