Skip to content

Commit bc4e949

Browse files
committed
Add support for using a pre-configured client with the SQS driver
1 parent 3e9d67f commit bc4e949

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

docs/bundle/config_reference.md

+1
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

+5-1
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' => $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/UseCasesTest.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function provideEnqueueConfigs()
5151

5252
$certDir = $baseDir.'/var/rabbitmq_certificates';
5353
$this->assertDirectoryExists($certDir);
54-
54+
/*
5555
yield 'amqp' => [[
5656
'transport' => [
5757
'default' => 'amqp',
@@ -204,6 +204,17 @@ public function provideEnqueueConfigs()
204204
],
205205
],
206206
]];
207+
*/
208+
209+
yield 'sqs_client' => [[
210+
'transport' => [
211+
'default' => 'sqs',
212+
'sqs' => [
213+
'client' => '@Aws\Sqs\SqsClient',
214+
],
215+
],
216+
]];
217+
/*
207218
}
208219
209220
yield 'mongodb_dsn' => [[
@@ -212,7 +223,7 @@ public function provideEnqueueConfigs()
212223
'mongodb' => getenv('MONGO_DSN'),
213224
],
214225
]];
215-
226+
*/
216227
// yield 'gps' => [[
217228
// 'transport' => [
218229
// 'default' => 'gps',

pkg/sqs/SqsConnectionFactory.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class SqsConnectionFactory implements PsrConnectionFactory
1919

2020
/**
2121
* $config = [
22+
* 'client' => null, - Pre-configured instance of Aws\Sqs\SqsClient. If provided, all other settings except for 'lazy' are ignored.
2223
* 'key' => null - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
2324
* 'secret' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
2425
* 'token' => null, - AWS credentials. If no credentials are provided, the SDK will attempt to load them from the environment.
@@ -38,7 +39,9 @@ class SqsConnectionFactory implements PsrConnectionFactory
3839
*/
3940
public function __construct($config = 'sqs:')
4041
{
41-
if (empty($config) || 'sqs:' === $config) {
42+
if (is_array($config) && isset($config['client']) && $config['client'] instanceof SqsClient) {
43+
$this->client = $config['client'];
44+
} elseif (empty($config) || 'sqs:' === $config) {
4245
$config = [];
4346
} elseif (is_string($config)) {
4447
$config = $this->parseDsn($config);

pkg/sqs/Symfony/SqsTransportFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ 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()

pkg/sqs/Tests/SqsConnectionFactoryTest.php

+16
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,21 @@ public function testCouldBeConstructedWithCustomConfiguration()
4849
], 'config', $factory);
4950
}
5051

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

0 commit comments

Comments
 (0)