mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Migrated RabbitMqOptions to immutable object
This commit is contained in:
@@ -28,7 +28,7 @@ return [
|
|||||||
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
|
Options\UrlShortenerOptions::class => ConfigAbstractFactory::class,
|
||||||
Options\TrackingOptions::class => [ValinorConfigFactory::class, 'config.tracking'],
|
Options\TrackingOptions::class => [ValinorConfigFactory::class, 'config.tracking'],
|
||||||
Options\QrCodeOptions::class => [ValinorConfigFactory::class, 'config.qr_codes'],
|
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,
|
Options\WebhookOptions::class => ConfigAbstractFactory::class,
|
||||||
|
|
||||||
Service\UrlShortener::class => ConfigAbstractFactory::class,
|
Service\UrlShortener::class => ConfigAbstractFactory::class,
|
||||||
@@ -88,7 +88,6 @@ return [
|
|||||||
|
|
||||||
Options\RedirectOptions::class => ['config.redirects'],
|
Options\RedirectOptions::class => ['config.redirects'],
|
||||||
Options\UrlShortenerOptions::class => ['config.url_shortener'],
|
Options\UrlShortenerOptions::class => ['config.url_shortener'],
|
||||||
Options\RabbitMqOptions::class => ['config.rabbitmq'],
|
|
||||||
Options\WebhookOptions::class => ['config.visits_webhooks'],
|
Options\WebhookOptions::class => ['config.visits_webhooks'],
|
||||||
|
|
||||||
Service\UrlShortener::class => [
|
Service\UrlShortener::class => [
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class NotifyNewShortUrlToRabbitMq extends AbstractNotifyNewShortUrlListener
|
|||||||
|
|
||||||
protected function isEnabled(): bool
|
protected function isEnabled(): bool
|
||||||
{
|
{
|
||||||
return $this->options->isEnabled();
|
return $this->options->enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getRemoteSystem(): RemoteSystem
|
protected function getRemoteSystem(): RemoteSystem
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class NotifyVisitToRabbitMq extends AbstractNotifyVisitListener
|
|||||||
protected function determineUpdatesForVisit(Visit $visit): array
|
protected function determineUpdatesForVisit(Visit $visit): array
|
||||||
{
|
{
|
||||||
// Once the two deprecated cases below have been removed, make parent method private
|
// 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);
|
return parent::determineUpdatesForVisit($visit);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ class NotifyVisitToRabbitMq extends AbstractNotifyVisitListener
|
|||||||
|
|
||||||
protected function isEnabled(): bool
|
protected function isEnabled(): bool
|
||||||
{
|
{
|
||||||
return $this->options->isEnabled();
|
return $this->options->enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getRemoteSystem(): RemoteSystem
|
protected function getRemoteSystem(): RemoteSystem
|
||||||
|
|||||||
@@ -4,37 +4,12 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\Options;
|
namespace Shlinkio\Shlink\Core\Options;
|
||||||
|
|
||||||
use Laminas\Stdlib\AbstractOptions;
|
final class RabbitMqOptions
|
||||||
|
|
||||||
class RabbitMqOptions extends AbstractOptions
|
|
||||||
{
|
{
|
||||||
protected $__strictMode__ = false; // phpcs:ignore
|
public function __construct(
|
||||||
|
public readonly bool $enabled = false,
|
||||||
private bool $enabled = false;
|
/** @deprecated */
|
||||||
/** @deprecated */
|
public readonly bool $legacyVisitsPublishing = false,
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,12 +27,10 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
|
|||||||
{
|
{
|
||||||
use ProphecyTrait;
|
use ProphecyTrait;
|
||||||
|
|
||||||
private NotifyNewShortUrlToRabbitMq $listener;
|
|
||||||
private ObjectProphecy $helper;
|
private ObjectProphecy $helper;
|
||||||
private ObjectProphecy $updatesGenerator;
|
private ObjectProphecy $updatesGenerator;
|
||||||
private ObjectProphecy $em;
|
private ObjectProphecy $em;
|
||||||
private ObjectProphecy $logger;
|
private ObjectProphecy $logger;
|
||||||
private RabbitMqOptions $options;
|
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
@@ -40,23 +38,12 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
|
|||||||
$this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class);
|
$this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class);
|
||||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
||||||
$this->logger = $this->prophesize(LoggerInterface::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 */
|
/** @test */
|
||||||
public function doesNothingWhenTheFeatureIsNotEnabled(): void
|
public function doesNothingWhenTheFeatureIsNotEnabled(): void
|
||||||
{
|
{
|
||||||
$this->options->enabled = false;
|
($this->listener(false))(new ShortUrlCreated('123'));
|
||||||
|
|
||||||
($this->listener)(new ShortUrlCreated('123'));
|
|
||||||
|
|
||||||
$this->em->find(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->em->find(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
@@ -74,7 +61,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
|
|||||||
['shortUrlId' => $shortUrlId, 'name' => 'RabbitMQ'],
|
['shortUrlId' => $shortUrlId, 'name' => 'RabbitMQ'],
|
||||||
);
|
);
|
||||||
|
|
||||||
($this->listener)(new ShortUrlCreated($shortUrlId));
|
($this->listener())(new ShortUrlCreated($shortUrlId));
|
||||||
|
|
||||||
$find->shouldHaveBeenCalledOnce();
|
$find->shouldHaveBeenCalledOnce();
|
||||||
$logWarning->shouldHaveBeenCalledOnce();
|
$logWarning->shouldHaveBeenCalledOnce();
|
||||||
@@ -92,7 +79,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
|
|||||||
$update,
|
$update,
|
||||||
);
|
);
|
||||||
|
|
||||||
($this->listener)(new ShortUrlCreated($shortUrlId));
|
($this->listener())(new ShortUrlCreated($shortUrlId));
|
||||||
|
|
||||||
$find->shouldHaveBeenCalledOnce();
|
$find->shouldHaveBeenCalledOnce();
|
||||||
$generateUpdate->shouldHaveBeenCalledOnce();
|
$generateUpdate->shouldHaveBeenCalledOnce();
|
||||||
@@ -114,7 +101,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
|
|||||||
);
|
);
|
||||||
$publish = $this->helper->publishUpdate($update)->willThrow($e);
|
$publish = $this->helper->publishUpdate($update)->willThrow($e);
|
||||||
|
|
||||||
($this->listener)(new ShortUrlCreated($shortUrlId));
|
($this->listener())(new ShortUrlCreated($shortUrlId));
|
||||||
|
|
||||||
$this->logger->debug(
|
$this->logger->debug(
|
||||||
'Error while trying to notify {name} with new short URL. {e}',
|
'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 Exception('Exception Error')];
|
||||||
yield [new DomainException('DomainException 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),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,12 +35,10 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
|||||||
{
|
{
|
||||||
use ProphecyTrait;
|
use ProphecyTrait;
|
||||||
|
|
||||||
private NotifyVisitToRabbitMq $listener;
|
|
||||||
private ObjectProphecy $helper;
|
private ObjectProphecy $helper;
|
||||||
private ObjectProphecy $updatesGenerator;
|
private ObjectProphecy $updatesGenerator;
|
||||||
private ObjectProphecy $em;
|
private ObjectProphecy $em;
|
||||||
private ObjectProphecy $logger;
|
private ObjectProphecy $logger;
|
||||||
private RabbitMqOptions $options;
|
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
@@ -48,24 +46,12 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
|||||||
$this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class);
|
$this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class);
|
||||||
$this->em = $this->prophesize(EntityManagerInterface::class);
|
$this->em = $this->prophesize(EntityManagerInterface::class);
|
||||||
$this->logger = $this->prophesize(LoggerInterface::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 */
|
/** @test */
|
||||||
public function doesNothingWhenTheFeatureIsNotEnabled(): void
|
public function doesNothingWhenTheFeatureIsNotEnabled(): void
|
||||||
{
|
{
|
||||||
$this->options->enabled = false;
|
($this->listener(new RabbitMqOptions(enabled: false)))(new VisitLocated('123'));
|
||||||
|
|
||||||
($this->listener)(new VisitLocated('123'));
|
|
||||||
|
|
||||||
$this->em->find(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->em->find(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
|
||||||
@@ -83,7 +69,7 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
|||||||
['visitId' => $visitId, 'name' => 'RabbitMQ'],
|
['visitId' => $visitId, 'name' => 'RabbitMQ'],
|
||||||
);
|
);
|
||||||
|
|
||||||
($this->listener)(new VisitLocated($visitId));
|
($this->listener())(new VisitLocated($visitId));
|
||||||
|
|
||||||
$findVisit->shouldHaveBeenCalledOnce();
|
$findVisit->shouldHaveBeenCalledOnce();
|
||||||
$logWarning->shouldHaveBeenCalledOnce();
|
$logWarning->shouldHaveBeenCalledOnce();
|
||||||
@@ -105,7 +91,7 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
|||||||
)->shouldBeCalledOnce();
|
)->shouldBeCalledOnce();
|
||||||
});
|
});
|
||||||
|
|
||||||
($this->listener)(new VisitLocated($visitId));
|
($this->listener())(new VisitLocated($visitId));
|
||||||
|
|
||||||
$findVisit->shouldHaveBeenCalledOnce();
|
$findVisit->shouldHaveBeenCalledOnce();
|
||||||
$this->helper->publishUpdate(Argument::type(Update::class))->shouldHaveBeenCalledTimes(
|
$this->helper->publishUpdate(Argument::type(Update::class))->shouldHaveBeenCalledTimes(
|
||||||
@@ -144,7 +130,7 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
|||||||
);
|
);
|
||||||
$publish = $this->helper->publishUpdate(Argument::cetera())->willThrow($e);
|
$publish = $this->helper->publishUpdate(Argument::cetera())->willThrow($e);
|
||||||
|
|
||||||
($this->listener)(new VisitLocated($visitId));
|
($this->listener())(new VisitLocated($visitId));
|
||||||
|
|
||||||
$this->logger->debug(
|
$this->logger->debug(
|
||||||
'Error while trying to notify {name} with new visit. {e}',
|
'Error while trying to notify {name} with new visit. {e}',
|
||||||
@@ -172,13 +158,11 @@ class NotifyVisitToRabbitMqTest extends TestCase
|
|||||||
callable $assert,
|
callable $assert,
|
||||||
callable $setup,
|
callable $setup,
|
||||||
): void {
|
): void {
|
||||||
$this->options->legacyVisitsPublishing = $legacy;
|
|
||||||
|
|
||||||
$visitId = '123';
|
$visitId = '123';
|
||||||
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit);
|
$findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit);
|
||||||
$setup($this->updatesGenerator);
|
$setup($this->updatesGenerator);
|
||||||
|
|
||||||
($this->listener)(new VisitLocated($visitId));
|
($this->listener(new RabbitMqOptions(true, $legacy)))(new VisitLocated($visitId));
|
||||||
|
|
||||||
$findVisit->shouldHaveBeenCalledOnce();
|
$findVisit->shouldHaveBeenCalledOnce();
|
||||||
$assert($this->helper, $this->updatesGenerator);
|
$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),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user