diff --git a/docs/bundle/config_reference.md b/docs/bundle/config_reference.md index 7b54dd23f..03290a602 100644 --- a/docs/bundle/config_reference.md +++ b/docs/bundle/config_reference.md @@ -228,6 +228,7 @@ enqueue: polling_interval: 1000 lazy: true sqs: + client: null key: null secret: null token: null diff --git a/docs/transport/sqs.md b/docs/transport/sqs.md index 03da0ebeb..9c688f8ec 100644 --- a/docs/transport/sqs.md +++ b/docs/transport/sqs.md @@ -34,6 +34,10 @@ $factory = new SqsConnectionFactory('sqs:?key=aKey&secret=aSecret®ion=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:'); ``` @@ -109,4 +113,4 @@ $fooQueue = $psrContext->createQueue('foo'); $psrContext->purge($fooQueue); ``` -[back to index](../index.md) \ No newline at end of file +[back to index](../index.md) diff --git a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml index 9bcc45a30..ae7f57943 100644 --- a/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml +++ b/pkg/enqueue-bundle/Tests/Functional/App/config/custom-config.yml @@ -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: ~ @@ -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)%' diff --git a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php index 34adc2c68..2b53b16c2 100644 --- a/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php +++ b/pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php @@ -204,6 +204,15 @@ public function provideEnqueueConfigs() ], ], ]]; + + yield 'sqs_client' => [[ + 'transport' => [ + 'default' => 'sqs', + 'sqs' => [ + 'client' => 'test.sqs_client', + ], + ], + ]]; } yield 'mongodb_dsn' => [[ diff --git a/pkg/sqs/SqsConnectionFactory.php b/pkg/sqs/SqsConnectionFactory.php index 38006c097..275752b6c 100644 --- a/pkg/sqs/SqsConnectionFactory.php +++ b/pkg/sqs/SqsConnectionFactory.php @@ -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:') { - 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); diff --git a/pkg/sqs/Symfony/SqsTransportFactory.php b/pkg/sqs/Symfony/SqsTransportFactory.php index 58c0c1a12..5f68c7684 100644 --- a/pkg/sqs/Symfony/SqsTransportFactory.php +++ b/pkg/sqs/Symfony/SqsTransportFactory.php @@ -34,10 +34,11 @@ public function addConfiguration(ArrayNodeDefinition $builder) { $builder ->children() + ->scalarNode('client')->defaultNull()->end() ->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') @@ -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); diff --git a/pkg/sqs/Tests/SqsConnectionFactoryTest.php b/pkg/sqs/Tests/SqsConnectionFactoryTest.php index d10447c04..aa3cf88de 100644 --- a/pkg/sqs/Tests/SqsConnectionFactoryTest.php +++ b/pkg/sqs/Tests/SqsConnectionFactoryTest.php @@ -2,6 +2,7 @@ namespace Enqueue\Sqs\Tests; +use Aws\Sqs\SqsClient; use Enqueue\Sqs\SqsConnectionFactory; use Enqueue\Sqs\SqsContext; use Enqueue\Test\ClassExtensionTrait; @@ -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]); diff --git a/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php b/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php index a263856a3..19bdb3cb8 100644 --- a/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php +++ b/pkg/sqs/Tests/Symfony/SqsTransportFactoryTest.php @@ -64,6 +64,7 @@ public function testShouldAllowAddConfiguration() 'version' => 'theVersion', 'lazy' => false, 'endpoint' => 'theEndpoint', + 'client' => null, ], $config); }