Migrated RabbitMqOptions to immutable object

This commit is contained in:
Alejandro Celaya 2022-09-17 13:56:59 +02:00
parent 0c34032fd3
commit 8f68078835
6 changed files with 42 additions and 74 deletions

View file

@ -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 => [

View file

@ -26,7 +26,7 @@ class NotifyNewShortUrlToRabbitMq extends AbstractNotifyNewShortUrlListener
protected function isEnabled(): bool
{
return $this->options->isEnabled();
return $this->options->enabled;
}
protected function getRemoteSystem(): RemoteSystem

View file

@ -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

View file

@ -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;
public function __construct(
public readonly 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 readonly bool $legacyVisitsPublishing = false,
) {
}
}

View file

@ -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),
);
}
}

View file

@ -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),
);
}
}