Skip to content

Commit 2c1e8fd

Browse files
committed
[symfony] add ability to define connection factory class.
1 parent 35646a7 commit 2c1e8fd

3 files changed

Lines changed: 130 additions & 6 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Enqueue\Bundle\Tests\Functional\App;
4+
5+
use Enqueue\ConnectionFactoryFactoryInterface;
6+
use Enqueue\Sqs\SqsConnectionFactory;
7+
use Interop\Queue\PsrConnectionFactory;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
10+
class SqsCustomConnectionFactoryFactory implements ConnectionFactoryFactoryInterface
11+
{
12+
/**
13+
* @var ContainerInterface
14+
*/
15+
private $container;
16+
17+
public function __construct(ContainerInterface $container)
18+
{
19+
$this->container = $container;
20+
}
21+
22+
public function create($config): PsrConnectionFactory
23+
{
24+
if (false == isset($config['service'])) {
25+
throw new \LogicException('The sqs client has to be set');
26+
}
27+
28+
return new SqsConnectionFactory($this->container->get($config['service']));
29+
}
30+
}

‎pkg/enqueue/Symfony/DependencyInjection/TransportFactory.php‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function addConfiguration(ArrayNodeDefinition $builder): void
4343
throw new \LogicException('Both options factory_class and factory_service are set. Please choose one.');
4444
}
4545

46+
if (isset($v['connection_factory_class']) && (isset($v['factory_class']) || isset($v['factory_service']))) {
47+
throw new \LogicException('The option connection_factory_class must not be used with factory_class or factory_service at the same time. Please choose one.');
48+
}
49+
4650
return $v;
4751
}
4852

@@ -56,6 +60,7 @@ public function addConfiguration(ArrayNodeDefinition $builder): void
5660
->ignoreExtraKeys(false)
5761
->children()
5862
->scalarNode('dsn')->cannotBeEmpty()->isRequired()->end()
63+
->scalarNode('connection_factory_class')->end()
5964
->scalarNode('factory_service')->end()
6065
->scalarNode('factory_class')->end()
6166
->end()
@@ -79,10 +84,19 @@ public function createConnectionFactory(ContainerBuilder $container, array $conf
7984

8085
unset($config['factory_service'], $config['factory_class']);
8186

82-
$container->register($factoryId, PsrConnectionFactory::class)
83-
->setFactory([$factoryFactoryService, 'create'])
84-
->addArgument($config)
85-
;
87+
if (array_key_exists('connection_factory_class', $config)) {
88+
$connectionFactoryClass = $config['connection_factory_class'];
89+
unset($config['connection_factory_class']);
90+
91+
$container->register($factoryId, $connectionFactoryClass)
92+
->addArgument($config)
93+
;
94+
} else {
95+
$container->register($factoryId, PsrConnectionFactory::class)
96+
->setFactory([$factoryFactoryService, 'create'])
97+
->addArgument($config)
98+
;
99+
}
86100

87101
$container->setAlias('enqueue.transport.connection_factory', new Alias($factoryId, true));
88102

‎pkg/enqueue/Tests/Symfony/DependencyInjection/TransportFactoryTest.php‎

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,42 @@ public function testThrowIfFactoryClassAndFactoryServiceSetAtTheSameTime()
137137
]]);
138138
}
139139

140+
public function testThrowIfConnectionFactoryClassUsedWithFactoryClassAtTheSameTime()
141+
{
142+
$transport = new TransportFactory('default');
143+
$tb = new TreeBuilder();
144+
$rootNode = $tb->root('foo');
145+
146+
$transport->addConfiguration($rootNode);
147+
$processor = new Processor();
148+
149+
$this->expectException(\LogicException::class);
150+
$this->expectExceptionMessage('The option connection_factory_class must not be used with factory_class or factory_service at the same time. Please choose one.');
151+
$processor->process($tb->buildTree(), [[
152+
'dsn' => 'foo:',
153+
'connection_factory_class' => 'aFactoryClass',
154+
'factory_service' => 'aFactoryService',
155+
]]);
156+
}
157+
158+
public function testThrowIfConnectionFactoryClassUsedWithFactoryServiceAtTheSameTime()
159+
{
160+
$transport = new TransportFactory('default');
161+
$tb = new TreeBuilder();
162+
$rootNode = $tb->root('foo');
163+
164+
$transport->addConfiguration($rootNode);
165+
$processor = new Processor();
166+
167+
$this->expectException(\LogicException::class);
168+
$this->expectExceptionMessage('The option connection_factory_class must not be used with factory_class or factory_service at the same time. Please choose one.');
169+
$processor->process($tb->buildTree(), [[
170+
'dsn' => 'foo:',
171+
'connection_factory_class' => 'aFactoryClass',
172+
'factory_service' => 'aFactoryService',
173+
]]);
174+
}
175+
140176
public function testShouldAllowSetFactoryClass()
141177
{
142178
$transport = new TransportFactory('default');
@@ -173,6 +209,24 @@ public function testShouldAllowSetFactoryService()
173209
$this->assertSame('theFactoryService', $config['factory_service']);
174210
}
175211

212+
public function testShouldAllowSetConnectionFactoryClass()
213+
{
214+
$transport = new TransportFactory('default');
215+
$tb = new TreeBuilder();
216+
$rootNode = $tb->root('foo');
217+
218+
$transport->addConfiguration($rootNode);
219+
$processor = new Processor();
220+
221+
$config = $processor->process($tb->buildTree(), [[
222+
'dsn' => 'foo:',
223+
'connection_factory_class' => 'theFactoryClass',
224+
]]);
225+
226+
$this->assertArrayHasKey('connection_factory_class', $config);
227+
$this->assertSame('theFactoryClass', $config['connection_factory_class']);
228+
}
229+
176230
public function testThrowIfExtraOptionGiven()
177231
{
178232
$transport = new TransportFactory('default');
@@ -218,7 +272,7 @@ public function testShouldCreateConnectionFactoryFromDSN()
218272
);
219273
}
220274

221-
public function testShouldCreateConnectionFactoryUsingCustomFactortyClass()
275+
public function testShouldCreateConnectionFactoryUsingCustomFactoryClass()
222276
{
223277
$container = new ContainerBuilder();
224278

@@ -253,7 +307,7 @@ public function testShouldCreateConnectionFactoryUsingCustomFactortyClass()
253307
);
254308
}
255309

256-
public function testShouldCreateConnectionFactoryUsingCustomFactortyService()
310+
public function testShouldCreateConnectionFactoryUsingCustomFactoryService()
257311
{
258312
$container = new ContainerBuilder();
259313

@@ -282,6 +336,32 @@ public function testShouldCreateConnectionFactoryUsingCustomFactortyService()
282336
);
283337
}
284338

339+
public function testShouldCreateConnectionFactoryUsingConnectionFactoryClassWithoutFactory()
340+
{
341+
$container = new ContainerBuilder();
342+
343+
$transport = new TransportFactory('default');
344+
345+
$serviceId = $transport->createConnectionFactory($container, ['dsn' => 'foo:', 'connection_factory_class' => 'theFactoryClass']);
346+
347+
$this->assertEquals('enqueue.transport.default.connection_factory', $serviceId);
348+
349+
$this->assertTrue($container->hasDefinition('enqueue.transport.default.connection_factory'));
350+
351+
$this->assertEmpty($container->getDefinition('enqueue.transport.default.connection_factory')->getFactory());
352+
$this->assertSame('theFactoryClass', $container->getDefinition('enqueue.transport.default.connection_factory')->getClass());
353+
$this->assertSame(
354+
[['dsn' => 'foo:']],
355+
$container->getDefinition('enqueue.transport.default.connection_factory')->getArguments())
356+
;
357+
358+
$this->assertTrue($container->hasAlias('enqueue.transport.connection_factory'));
359+
$this->assertEquals(
360+
'enqueue.transport.default.connection_factory',
361+
(string) $container->getAlias('enqueue.transport.connection_factory')
362+
);
363+
}
364+
285365
public function testShouldCreateContextFromDsn()
286366
{
287367
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)