Skip to content

Commit 9b01008

Browse files
committed
Use connection factory factory.
1 parent cf678ba commit 9b01008

File tree

3 files changed

+37
-58
lines changed

3 files changed

+37
-58
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"illuminate/queue": "^5.6",
1010
"queue-interop/amqp-interop": "0.8.x-dev",
1111
"queue-interop/queue-interop": "0.7.x-dev",
12-
"enqueue/amqp-tools": "0.9.x-dev"
12+
"enqueue/enqueue": "0.9.x-dev",
13+
"enqueue/dsn": "0.9.x-dev"
1314
},
1415
"require-dev": {
1516
"phpunit/phpunit": "~5.5",

src/AmqpConnector.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Connector.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,54 @@
22

33
namespace Enqueue\LaravelQueue;
44

5+
use Enqueue\AmqpTools\DelayStrategy;
6+
use Enqueue\AmqpTools\DelayStrategyAware;
7+
use Enqueue\AmqpTools\RabbitMqDelayPluginDelayStrategy;
8+
use Enqueue\AmqpTools\RabbitMqDlxDelayStrategy;
9+
use Enqueue\ConnectionFactoryFactory;
10+
use Enqueue\ConnectionFactoryFactoryInterface;
11+
use Illuminate\Contracts\Queue\Queue as QueueContract;
512
use Illuminate\Queue\Connectors\ConnectorInterface;
6-
use Interop\Queue\ConnectionFactory;
13+
use Interop\Amqp\AmqpContext;
714

815
class Connector implements ConnectorInterface
916
{
10-
/**
11-
* {@inheritdoc}
12-
*/
13-
public function connect(array $config)
17+
public function connect(array $config): QueueContract
1418
{
1519
$config = array_replace([
16-
'connection_factory_class' => null,
20+
'dsn' => null,
21+
'factory_class' => null,
1722
'queue' => 'default',
1823
'time_to_run' => 0,
1924
], $config);
2025

21-
if (empty($config['connection_factory_class'])) {
22-
throw new \LogicException('The "connection_factory_class" option is required');
23-
}
26+
$queue = $config['queue'];
27+
$timeToRum = $config['time_to_run'];
28+
$connectionFactoryFactoryClass = $config['factory_class'] ?? ConnectionFactoryFactory::class;
2429

25-
$factoryClass = $config['connection_factory_class'];
26-
if (false == class_exists($factoryClass)) {
27-
throw new \LogicException(sprintf('The "connection_factory_class" option "%s" is not a class', $factoryClass));
28-
}
30+
unset($config['factory_class']);
2931

30-
$rc = new \ReflectionClass($factoryClass);
31-
if (false == $rc->implementsInterface(ConnectionFactory::class)) {
32-
throw new \LogicException(sprintf('The "connection_factory_class" option must contain a class that implements "%s" but it is not', ConnectionFactory::class));
33-
}
32+
/** @var ConnectionFactoryFactoryInterface $factory */
33+
$factory = new $connectionFactoryFactoryClass();
34+
$connection = $factory->create($config);
35+
$context = $connection->createContext();
3436

35-
/** @var ConnectionFactory $factory */
36-
$factory = new $factoryClass($config);
37+
if ($context instanceof AmqpContext) {
38+
$config = array_replace(['delay_strategy' => 'rabbitmq_dlx'], $config);
39+
40+
if ($context instanceof DelayStrategyAware && 'rabbitmq_dlx' == $config['delay_strategy']) {
41+
$context->setDelayStrategy(new RabbitMqDlxDelayStrategy());
42+
}
43+
if ($context instanceof DelayStrategyAware && 'rabbitmq_delay_plugin' == $config['delay_strategy']) {
44+
$context->setDelayStrategy(new RabbitMqDelayPluginDelayStrategy());
45+
}
46+
if ($context instanceof DelayStrategyAware && $config['delay_strategy'] instanceof DelayStrategy) {
47+
$context->setDelayStrategy($config['delay_strategy']);
48+
}
49+
50+
return new AmqpQueue($context, $queue, $timeToRum);
51+
}
3752

38-
return new Queue($factory->createContext(), $config['queue'], $config['time_to_run']);
53+
return new Queue($context, $queue, $timeToRum);
3954
}
4055
}

0 commit comments

Comments
 (0)