From 9fc6fbfa4f653dd887bddb54321b575610f9b2a7 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar Date: Thu, 1 Mar 2018 16:09:50 +0200 Subject: [PATCH] [bundle] fix for "Transport factory with such name already added" --- .../DependencyInjection/EnqueueExtension.php | 17 +++++++-- pkg/enqueue-bundle/EnqueueBundle.php | 36 +++++++++---------- .../Tests/Unit/EnqueueBundleTest.php | 18 +++++----- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php index 305668ac4..17c6d41d3 100644 --- a/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php +++ b/pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php @@ -41,13 +41,24 @@ public function addTransportFactory(TransportFactoryInterface $transportFactory) { $name = $transportFactory->getName(); - if (empty($name)) { - throw new \LogicException('Transport factory name cannot be empty'); - } if (array_key_exists($name, $this->factories)) { throw new \LogicException(sprintf('Transport factory with such name already added. Name %s', $name)); } + $this->setTransportFactory($transportFactory); + } + + /** + * @param TransportFactoryInterface $transportFactory + */ + public function setTransportFactory(TransportFactoryInterface $transportFactory) + { + $name = $transportFactory->getName(); + + if (empty($name)) { + throw new \LogicException('Transport factory name cannot be empty'); + } + $this->factories[$name] = $transportFactory; } diff --git a/pkg/enqueue-bundle/EnqueueBundle.php b/pkg/enqueue-bundle/EnqueueBundle.php index 94cd076a3..b22e55250 100644 --- a/pkg/enqueue-bundle/EnqueueBundle.php +++ b/pkg/enqueue-bundle/EnqueueBundle.php @@ -54,11 +54,11 @@ public function build(ContainerBuilder $container) $extension = $container->getExtension('enqueue'); if (class_exists(StompConnectionFactory::class)) { - $extension->addTransportFactory(new StompTransportFactory('stomp')); - $extension->addTransportFactory(new RabbitMqStompTransportFactory('rabbitmq_stomp')); + $extension->setTransportFactory(new StompTransportFactory('stomp')); + $extension->setTransportFactory(new RabbitMqStompTransportFactory('rabbitmq_stomp')); } else { - $extension->addTransportFactory(new MissingTransportFactory('stomp', ['enqueue/stomp'])); - $extension->addTransportFactory(new MissingTransportFactory('rabbitmq_stomp', ['enqueue/stomp'])); + $extension->setTransportFactory(new MissingTransportFactory('stomp', ['enqueue/stomp'])); + $extension->setTransportFactory(new MissingTransportFactory('rabbitmq_stomp', ['enqueue/stomp'])); } if ( @@ -66,42 +66,42 @@ class_exists(AmqpBunnyConnectionFactory::class) || class_exists(AmqpExtConnectionFactory::class) || class_exists(AmqpLibConnectionFactory::class) ) { - $extension->addTransportFactory(new AmqpTransportFactory('amqp')); - $extension->addTransportFactory(new RabbitMqAmqpTransportFactory('rabbitmq_amqp')); + $extension->setTransportFactory(new AmqpTransportFactory('amqp')); + $extension->setTransportFactory(new RabbitMqAmqpTransportFactory('rabbitmq_amqp')); } else { $amppPackages = ['enqueue/amqp-ext', 'enqueue/amqp-bunny', 'enqueue/amqp-lib']; - $extension->addTransportFactory(new MissingTransportFactory('amqp', $amppPackages)); - $extension->addTransportFactory(new MissingTransportFactory('rabbitmq_amqp', $amppPackages)); + $extension->setTransportFactory(new MissingTransportFactory('amqp', $amppPackages)); + $extension->setTransportFactory(new MissingTransportFactory('rabbitmq_amqp', $amppPackages)); } if (class_exists(FsConnectionFactory::class)) { - $extension->addTransportFactory(new FsTransportFactory('fs')); + $extension->setTransportFactory(new FsTransportFactory('fs')); } else { - $extension->addTransportFactory(new MissingTransportFactory('fs', ['enqueue/fs'])); + $extension->setTransportFactory(new MissingTransportFactory('fs', ['enqueue/fs'])); } if (class_exists(RedisConnectionFactory::class)) { - $extension->addTransportFactory(new RedisTransportFactory('redis')); + $extension->setTransportFactory(new RedisTransportFactory('redis')); } else { - $extension->addTransportFactory(new MissingTransportFactory('redis', ['enqueue/redis'])); + $extension->setTransportFactory(new MissingTransportFactory('redis', ['enqueue/redis'])); } if (class_exists(DbalConnectionFactory::class)) { - $extension->addTransportFactory(new DbalTransportFactory('dbal')); + $extension->setTransportFactory(new DbalTransportFactory('dbal')); } else { - $extension->addTransportFactory(new MissingTransportFactory('dbal', ['enqueue/dbal'])); + $extension->setTransportFactory(new MissingTransportFactory('dbal', ['enqueue/dbal'])); } if (class_exists(SqsConnectionFactory::class)) { - $extension->addTransportFactory(new SqsTransportFactory('sqs')); + $extension->setTransportFactory(new SqsTransportFactory('sqs')); } else { - $extension->addTransportFactory(new MissingTransportFactory('sqs', ['enqueue/sqs'])); + $extension->setTransportFactory(new MissingTransportFactory('sqs', ['enqueue/sqs'])); } if (class_exists(GpsConnectionFactory::class)) { - $extension->addTransportFactory(new GpsTransportFactory('gps')); + $extension->setTransportFactory(new GpsTransportFactory('gps')); } else { - $extension->addTransportFactory(new MissingTransportFactory('gps', ['enqueue/gps'])); + $extension->setTransportFactory(new MissingTransportFactory('gps', ['enqueue/gps'])); } $container->addCompilerPass(new AsyncEventsPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 100); diff --git a/pkg/enqueue-bundle/Tests/Unit/EnqueueBundleTest.php b/pkg/enqueue-bundle/Tests/Unit/EnqueueBundleTest.php index 7ac8f4cbb..270c4d315 100644 --- a/pkg/enqueue-bundle/Tests/Unit/EnqueueBundleTest.php +++ b/pkg/enqueue-bundle/Tests/Unit/EnqueueBundleTest.php @@ -98,12 +98,12 @@ public function testShouldRegisterStompAndRabbitMqStompTransportFactories() $extensionMock ->expects($this->at(0)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(StompTransportFactory::class)) ; $extensionMock ->expects($this->at(1)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(RabbitMqStompTransportFactory::class)) ; @@ -120,7 +120,7 @@ public function testShouldRegisterAmqpAndRabbitMqAmqpTransportFactories() $extensionMock ->expects($this->at(2)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(AmqpTransportFactory::class)) ->willReturnCallback(function (AmqpTransportFactory $factory) { $this->assertSame('amqp', $factory->getName()); @@ -128,7 +128,7 @@ public function testShouldRegisterAmqpAndRabbitMqAmqpTransportFactories() ; $extensionMock ->expects($this->at(3)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(RabbitMqAmqpTransportFactory::class)) ->willReturnCallback(function (RabbitMqAmqpTransportFactory $factory) { $this->assertSame('rabbitmq_amqp', $factory->getName()); @@ -148,7 +148,7 @@ public function testShouldRegisterFSTransportFactory() $extensionMock ->expects($this->at(4)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(FsTransportFactory::class)) ; @@ -165,7 +165,7 @@ public function testShouldRegisterRedisTransportFactory() $extensionMock ->expects($this->at(5)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(RedisTransportFactory::class)) ; @@ -182,7 +182,7 @@ public function testShouldRegisterDbalTransportFactory() $extensionMock ->expects($this->at(6)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(DbalTransportFactory::class)) ; @@ -199,7 +199,7 @@ public function testShouldRegisterSqsTransportFactory() $extensionMock ->expects($this->at(7)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(SqsTransportFactory::class)) ; @@ -216,7 +216,7 @@ public function testShouldRegisterGpsTransportFactory() $extensionMock ->expects($this->at(8)) - ->method('addTransportFactory') + ->method('setTransportFactory') ->with($this->isInstanceOf(GpsTransportFactory::class)) ;