From 97d24d76d882a95d55a3510e5eeccda21719bec1 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 24 Jul 2022 12:37:57 +0200 Subject: [PATCH] Fixed new short URL event payload to RabbitMQ, and started to add logic for Mercure --- module/Core/config/event_dispatcher.config.php | 1 + .../Mercure/NotifyVisitToMercure.php | 8 ++++---- .../RabbitMq/NotifyNewShortUrlToRabbitMq.php | 2 +- .../RabbitMq/NotifyVisitToRabbitMq.php | 15 +++++++++++++++ .../Core/src/Mercure/MercureUpdatesGenerator.php | 12 ++++++++++-- .../Mercure/MercureUpdatesGeneratorInterface.php | 3 +++ .../RabbitMq/NotifyVisitToRabbitMqTest.php | 4 ++++ 7 files changed, 38 insertions(+), 7 deletions(-) diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index f7ef63b3..e710bd7d 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -87,6 +87,7 @@ return [ 'em', 'Logger_Shlink', Visit\Transformer\OrphanVisitDataTransformer::class, + ShortUrl\Transformer\ShortUrlDataTransformer::class, 'config.rabbitmq.enabled', ], EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => [ diff --git a/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php b/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php index fa520f08..11d3a8e4 100644 --- a/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php +++ b/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php @@ -18,10 +18,10 @@ use function Functional\each; class NotifyVisitToMercure { public function __construct( - private HubInterface $hub, - private MercureUpdatesGeneratorInterface $updatesGenerator, - private EntityManagerInterface $em, - private LoggerInterface $logger, + private readonly HubInterface $hub, + private readonly MercureUpdatesGeneratorInterface $updatesGenerator, + private readonly EntityManagerInterface $em, + private readonly LoggerInterface $logger, ) { } diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php index 0d5368a2..583f9420 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php @@ -43,7 +43,7 @@ class NotifyNewShortUrlToRabbitMq try { $this->rabbitMqHelper->publishPayloadInQueue( - $this->shortUrlTransformer->transform($shortUrl), + ['shortUrl' => $this->shortUrlTransformer->transform($shortUrl)], Topic::NEW_SHORT_URL->value, ); } catch (Throwable $e) { diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php index a10d8673..897bff29 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php @@ -20,6 +20,7 @@ class NotifyVisitToRabbitMq private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, private readonly DataTransformerInterface $orphanVisitTransformer, + private readonly DataTransformerInterface $shortUrlTransformer, // @phpstan-ignore-line private readonly bool $isEnabled, ) { } @@ -69,6 +70,20 @@ class NotifyVisitToRabbitMq private function visitToPayload(Visit $visit): array { + // FIXME This was defined incorrectly. + // According to the spec, both the visit and the short URL it belongs to, should be published. + // The shape should be ['visit' => [...], 'shortUrl' => ?[...]] + // However, this would be a breaking change, so we need a flag that determines the shape of the payload. + return ! $visit->isOrphan() ? $visit->jsonSerialize() : $this->orphanVisitTransformer->transform($visit); + + if ($visit->isOrphan()) { // @phpstan-ignore-line + return ['visit' => $this->orphanVisitTransformer->transform($visit)]; + } + + return [ + 'visit' => $visit->jsonSerialize(), + 'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()), + ]; } } diff --git a/module/Core/src/Mercure/MercureUpdatesGenerator.php b/module/Core/src/Mercure/MercureUpdatesGenerator.php index f84d296d..0f01faa2 100644 --- a/module/Core/src/Mercure/MercureUpdatesGenerator.php +++ b/module/Core/src/Mercure/MercureUpdatesGenerator.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Mercure; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; +use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\EventDispatcher\Topic; use Symfony\Component\Mercure\Update; @@ -14,8 +15,8 @@ use function Shlinkio\Shlink\Common\json_encode; final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface { public function __construct( - private DataTransformerInterface $shortUrlTransformer, - private DataTransformerInterface $orphanVisitTransformer, + private readonly DataTransformerInterface $shortUrlTransformer, + private readonly DataTransformerInterface $orphanVisitTransformer, ) { } @@ -44,4 +45,11 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface 'visit' => $visit, ])); } + + public function newShortUrlUpdate(ShortUrl $shortUrl): Update + { + return new Update(Topic::NEW_SHORT_URL->value, json_encode([ + 'shortUrl' => $this->shortUrlTransformer->transform($shortUrl), + ])); + } } diff --git a/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php b/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php index 951e805c..ee0cd593 100644 --- a/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php +++ b/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Mercure; +use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; use Symfony\Component\Mercure\Update; @@ -14,4 +15,6 @@ interface MercureUpdatesGeneratorInterface public function newOrphanVisitUpdate(Visit $visit): Update; public function newShortUrlVisitUpdate(Visit $visit): Update; + + public function newShortUrlUpdate(ShortUrl $shortUrl): Update; } diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php index 18a99425..2558dfc3 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php @@ -20,6 +20,8 @@ use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyVisitToRabbitMq; use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Core\Model\Visitor; +use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; +use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer; use Throwable; @@ -46,6 +48,7 @@ class NotifyVisitToRabbitMqTest extends TestCase $this->em->reveal(), $this->logger->reveal(), new OrphanVisitDataTransformer(), + new ShortUrlDataTransformer(new ShortUrlStringifier([])), true, ); } @@ -58,6 +61,7 @@ class NotifyVisitToRabbitMqTest extends TestCase $this->em->reveal(), $this->logger->reveal(), new OrphanVisitDataTransformer(), + new ShortUrlDataTransformer(new ShortUrlStringifier([])), false, );