Skip to content

Commit 2423e0a

Browse files
authored
Merge pull request #444 from elazar/feature/sqs-connection-factory-client
WIP: Add support for using a pre-configured client with the SQS driver
2 parents c36670c + ce7dd8f commit 2423e0a

File tree

8 files changed

+55
-6
lines changed

8 files changed

+55
-6
lines changed

docs/bundle/config_reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ enqueue:
228228
polling_interval: 1000
229229
lazy: true
230230
sqs:
231+
client: null
231232
key: null
232233
secret: null
233234
token: null

docs/transport/sqs.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ $factory = new SqsConnectionFactory('sqs:?key=aKey&secret=aSecret&region=aRegion
3434

3535
$psrContext = $factory->createContext();
3636

37+
// using a pre-configured client
38+
$client = new Aws\Sqs\SqsClient([ /* ... */ ]);
39+
$factory = new SqsConnectionFactory($client);
40+
3741
// if you have enqueue/enqueue library installed you can use a function from there to create the context
3842
$psrContext = \Enqueue\dsn_to_context('sqs:');
3943
```
@@ -109,4 +113,4 @@ $fooQueue = $psrContext->createQueue('foo');
109113
$psrContext->purge($fooQueue);
110114
```
111115

112-
[back to index](../index.md)
116+
[back to index](../index.md)

pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
parameters:
22
locale: 'en'
33
secret: 'ThisTokenIsNotSoSecretChangeIt'
4-
4+
env(AWS_SQS_REGION): 'us-east-1'
5+
env(AWS_SQS_KEY): 'key'
6+
env(AWS_SQS_SECRET): 'secret'
57

68
framework:
79
#esi: ~
@@ -33,3 +35,14 @@ services:
3335
public: true
3436
tags:
3537
- { name: 'enqueue.client.processor' }
38+
39+
test.sqs_client:
40+
public: true
41+
class: Aws\Sqs\SqsClient
42+
arguments:
43+
-
44+
region: '%env(AWS_SQS_REGION)%'
45+
version: '2012-11-05'
46+
credentials:
47+
key: '%env(AWS_SQS_KEY)%'
48+
secret: '%env(AWS_SQS_SECRET)%'

pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ public function provideEnqueueConfigs()
204204
],
205205
],
206206
]];
207+
208+
yield 'sqs_client' => [[
209+
'transport' => [
210+
'default' => 'sqs',
211+
'sqs' => [
212+
'client' => 'test.sqs_client',
213+
],
214+
],
215+
]];
207216
}
208217

209218
yield 'mongodb_dsn' => [[

pkg/sqs/SqsConnectionFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,16 @@ class SqsConnectionFactory implements PsrConnectionFactory
3434
* sqs:
3535
* sqs::?key=aKey&secret=aSecret&token=aToken
3636
*
37-
* @param array|string|null $config
37+
* @param array|string|SqsClient|null $config
3838
*/
3939
public function __construct($config = 'sqs:')
4040
{
41-
if (empty($config) || 'sqs:' === $config) {
41+
if ($config instanceof SqsClient) {
42+
$this->client = $config;
43+
$this->config = ['lazy' => false] + $this->defaultConfig();
44+
45+
return;
46+
} elseif (empty($config) || 'sqs:' === $config) {
4247
$config = [];
4348
} elseif (is_string($config)) {
4449
$config = $this->parseDsn($config);

pkg/sqs/Symfony/SqsTransportFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public function addConfiguration(ArrayNodeDefinition $builder)
3434
{
3535
$builder
3636
->children()
37+
->scalarNode('client')->defaultNull()->end()
3738
->scalarNode('key')->defaultNull()->end()
3839
->scalarNode('secret')->defaultNull()->end()
3940
->scalarNode('token')->defaultNull()->end()
40-
->scalarNode('region')->isRequired()->end()
41+
->scalarNode('region')->end()
4142
->integerNode('retries')->defaultValue(3)->end()
4243
->scalarNode('version')->cannotBeEmpty()->defaultValue('2012-11-05')->end()
4344
->booleanNode('lazy')
@@ -53,8 +54,10 @@ public function addConfiguration(ArrayNodeDefinition $builder)
5354
*/
5455
public function createConnectionFactory(ContainerBuilder $container, array $config)
5556
{
57+
$arguments = empty($config['client']) ? $config : new Reference($config['client']);
58+
5659
$factory = new Definition(SqsConnectionFactory::class);
57-
$factory->setArguments([$config]);
60+
$factory->setArguments([$arguments]);
5861

5962
$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
6063
$container->setDefinition($factoryId, $factory);

pkg/sqs/Tests/SqsConnectionFactoryTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Enqueue\Sqs\Tests;
44

5+
use Aws\Sqs\SqsClient;
56
use Enqueue\Sqs\SqsConnectionFactory;
67
use Enqueue\Sqs\SqsContext;
78
use Enqueue\Test\ClassExtensionTrait;
@@ -48,6 +49,18 @@ public function testCouldBeConstructedWithCustomConfiguration()
4849
], 'config', $factory);
4950
}
5051

52+
public function testCouldBeConstructedWithClient()
53+
{
54+
$client = $this->createMock(SqsClient::class);
55+
56+
$factory = new SqsConnectionFactory($client);
57+
58+
$context = $factory->createContext();
59+
60+
$this->assertInstanceOf(SqsContext::class, $context);
61+
$this->assertAttributeSame($client, 'client', $context);
62+
}
63+
5164
public function testShouldCreateLazyContext()
5265
{
5366
$factory = new SqsConnectionFactory(['lazy' => true]);

pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public function testShouldAllowAddConfiguration()
6464
'version' => 'theVersion',
6565
'lazy' => false,
6666
'endpoint' => 'theEndpoint',
67+
'client' => null,
6768
], $config);
6869
}
6970

0 commit comments

Comments
 (0)