From 8f680788358781a08b7a6afd698666e33eadaa06 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 17 Sep 2022 13:56:59 +0200 Subject: [PATCH] Migrated RabbitMqOptions to immutable object --- module/Core/config/dependencies.config.php | 3 +- .../RabbitMq/NotifyNewShortUrlToRabbitMq.php | 2 +- .../RabbitMq/NotifyVisitToRabbitMq.php | 4 +- module/Core/src/Options/RabbitMqOptions.php | 37 +++--------------- .../NotifyNewShortUrlToRabbitMqTest.php | 32 ++++++++-------- .../RabbitMq/NotifyVisitToRabbitMqTest.php | 38 +++++++++---------- 6 files changed, 42 insertions(+), 74 deletions(-) diff --git a/module/Core/config/dependencies.config.php b/module/Core/config/dependencies.config.php index df63eeff..8c06001e 100644 --- a/module/Core/config/dependencies.config.php +++ b/module/Core/config/dependencies.config.php @@ -28,7 +28,7 @@ return [ Options\UrlShortenerOptions::class => ConfigAbstractFactory::class, Options\TrackingOptions::class => [ValinorConfigFactory::class, 'config.tracking'], Options\QrCodeOptions::class => [ValinorConfigFactory::class, 'config.qr_codes'], - Options\RabbitMqOptions::class => ConfigAbstractFactory::class, + Options\RabbitMqOptions::class => [ValinorConfigFactory::class, 'config.rabbitmq'], Options\WebhookOptions::class => ConfigAbstractFactory::class, Service\UrlShortener::class => ConfigAbstractFactory::class, @@ -88,7 +88,6 @@ return [ Options\RedirectOptions::class => ['config.redirects'], Options\UrlShortenerOptions::class => ['config.url_shortener'], - Options\RabbitMqOptions::class => ['config.rabbitmq'], Options\WebhookOptions::class => ['config.visits_webhooks'], Service\UrlShortener::class => [ diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php index 488247d7..daa7cafb 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php @@ -26,7 +26,7 @@ class NotifyNewShortUrlToRabbitMq extends AbstractNotifyNewShortUrlListener protected function isEnabled(): bool { - return $this->options->isEnabled(); + return $this->options->enabled; } protected function getRemoteSystem(): RemoteSystem diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php index 0faa795c..989de0a5 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php @@ -35,7 +35,7 @@ class NotifyVisitToRabbitMq extends AbstractNotifyVisitListener protected function determineUpdatesForVisit(Visit $visit): array { // Once the two deprecated cases below have been removed, make parent method private - if (! $this->options->legacyVisitsPublishing()) { + if (! $this->options->legacyVisitsPublishing) { return parent::determineUpdatesForVisit($visit); } @@ -61,7 +61,7 @@ class NotifyVisitToRabbitMq extends AbstractNotifyVisitListener protected function isEnabled(): bool { - return $this->options->isEnabled(); + return $this->options->enabled; } protected function getRemoteSystem(): RemoteSystem diff --git a/module/Core/src/Options/RabbitMqOptions.php b/module/Core/src/Options/RabbitMqOptions.php index 388cd2ea..cc25f3bf 100644 --- a/module/Core/src/Options/RabbitMqOptions.php +++ b/module/Core/src/Options/RabbitMqOptions.php @@ -4,37 +4,12 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Options; -use Laminas\Stdlib\AbstractOptions; - -class RabbitMqOptions extends AbstractOptions +final class RabbitMqOptions { - protected $__strictMode__ = false; // phpcs:ignore - - private bool $enabled = false; - /** @deprecated */ - private bool $legacyVisitsPublishing = false; - - public function isEnabled(): bool - { - return $this->enabled; - } - - protected function setEnabled(bool $enabled): self - { - $this->enabled = $enabled; - return $this; - } - - /** @deprecated */ - public function legacyVisitsPublishing(): bool - { - return $this->legacyVisitsPublishing; - } - - /** @deprecated */ - protected function setLegacyVisitsPublishing(bool $legacyVisitsPublishing): self - { - $this->legacyVisitsPublishing = $legacyVisitsPublishing; - return $this; + public function __construct( + public readonly bool $enabled = false, + /** @deprecated */ + public readonly bool $legacyVisitsPublishing = false, + ) { } } diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php index 9cf44977..5365fe0e 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php @@ -27,12 +27,10 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase { use ProphecyTrait; - private NotifyNewShortUrlToRabbitMq $listener; private ObjectProphecy $helper; private ObjectProphecy $updatesGenerator; private ObjectProphecy $em; private ObjectProphecy $logger; - private RabbitMqOptions $options; protected function setUp(): void { @@ -40,23 +38,12 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase $this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class); - $this->options = new RabbitMqOptions(['enabled' => true]); - - $this->listener = new NotifyNewShortUrlToRabbitMq( - $this->helper->reveal(), - $this->updatesGenerator->reveal(), - $this->em->reveal(), - $this->logger->reveal(), - $this->options, - ); } /** @test */ public function doesNothingWhenTheFeatureIsNotEnabled(): void { - $this->options->enabled = false; - - ($this->listener)(new ShortUrlCreated('123')); + ($this->listener(false))(new ShortUrlCreated('123')); $this->em->find(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); @@ -74,7 +61,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase ['shortUrlId' => $shortUrlId, 'name' => 'RabbitMQ'], ); - ($this->listener)(new ShortUrlCreated($shortUrlId)); + ($this->listener())(new ShortUrlCreated($shortUrlId)); $find->shouldHaveBeenCalledOnce(); $logWarning->shouldHaveBeenCalledOnce(); @@ -92,7 +79,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase $update, ); - ($this->listener)(new ShortUrlCreated($shortUrlId)); + ($this->listener())(new ShortUrlCreated($shortUrlId)); $find->shouldHaveBeenCalledOnce(); $generateUpdate->shouldHaveBeenCalledOnce(); @@ -114,7 +101,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase ); $publish = $this->helper->publishUpdate($update)->willThrow($e); - ($this->listener)(new ShortUrlCreated($shortUrlId)); + ($this->listener())(new ShortUrlCreated($shortUrlId)); $this->logger->debug( 'Error while trying to notify {name} with new short URL. {e}', @@ -131,4 +118,15 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase yield [new Exception('Exception Error')]; yield [new DomainException('DomainException Error')]; } + + private function listener(bool $enabled = true): NotifyNewShortUrlToRabbitMq + { + return new NotifyNewShortUrlToRabbitMq( + $this->helper->reveal(), + $this->updatesGenerator->reveal(), + $this->em->reveal(), + $this->logger->reveal(), + new RabbitMqOptions($enabled), + ); + } } diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php index 05ee7568..59f9c26a 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php @@ -35,12 +35,10 @@ class NotifyVisitToRabbitMqTest extends TestCase { use ProphecyTrait; - private NotifyVisitToRabbitMq $listener; private ObjectProphecy $helper; private ObjectProphecy $updatesGenerator; private ObjectProphecy $em; private ObjectProphecy $logger; - private RabbitMqOptions $options; protected function setUp(): void { @@ -48,24 +46,12 @@ class NotifyVisitToRabbitMqTest extends TestCase $this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class); - $this->options = new RabbitMqOptions(['enabled' => true, 'legacy_visits_publishing' => false]); - - $this->listener = new NotifyVisitToRabbitMq( - $this->helper->reveal(), - $this->updatesGenerator->reveal(), - $this->em->reveal(), - $this->logger->reveal(), - new OrphanVisitDataTransformer(), - $this->options, - ); } /** @test */ public function doesNothingWhenTheFeatureIsNotEnabled(): void { - $this->options->enabled = false; - - ($this->listener)(new VisitLocated('123')); + ($this->listener(new RabbitMqOptions(enabled: false)))(new VisitLocated('123')); $this->em->find(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); @@ -83,7 +69,7 @@ class NotifyVisitToRabbitMqTest extends TestCase ['visitId' => $visitId, 'name' => 'RabbitMQ'], ); - ($this->listener)(new VisitLocated($visitId)); + ($this->listener())(new VisitLocated($visitId)); $findVisit->shouldHaveBeenCalledOnce(); $logWarning->shouldHaveBeenCalledOnce(); @@ -105,7 +91,7 @@ class NotifyVisitToRabbitMqTest extends TestCase )->shouldBeCalledOnce(); }); - ($this->listener)(new VisitLocated($visitId)); + ($this->listener())(new VisitLocated($visitId)); $findVisit->shouldHaveBeenCalledOnce(); $this->helper->publishUpdate(Argument::type(Update::class))->shouldHaveBeenCalledTimes( @@ -144,7 +130,7 @@ class NotifyVisitToRabbitMqTest extends TestCase ); $publish = $this->helper->publishUpdate(Argument::cetera())->willThrow($e); - ($this->listener)(new VisitLocated($visitId)); + ($this->listener())(new VisitLocated($visitId)); $this->logger->debug( 'Error while trying to notify {name} with new visit. {e}', @@ -172,13 +158,11 @@ class NotifyVisitToRabbitMqTest extends TestCase callable $assert, callable $setup, ): void { - $this->options->legacyVisitsPublishing = $legacy; - $visitId = '123'; $findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit); $setup($this->updatesGenerator); - ($this->listener)(new VisitLocated($visitId)); + ($this->listener(new RabbitMqOptions(true, $legacy)))(new VisitLocated($visitId)); $findVisit->shouldHaveBeenCalledOnce(); $assert($this->helper, $this->updatesGenerator); @@ -247,4 +231,16 @@ class NotifyVisitToRabbitMqTest extends TestCase }, ]; } + + private function listener(?RabbitMqOptions $options = null): NotifyVisitToRabbitMq + { + return new NotifyVisitToRabbitMq( + $this->helper->reveal(), + $this->updatesGenerator->reveal(), + $this->em->reveal(), + $this->logger->reveal(), + new OrphanVisitDataTransformer(), + $options ?? new RabbitMqOptions(enabled: true, legacyVisitsPublishing: false), + ); + } }