diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php index 8e66dedb..0d5368a2 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php @@ -10,12 +10,11 @@ use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated; +use Shlinkio\Shlink\Core\EventDispatcher\Topic; use Throwable; class NotifyNewShortUrlToRabbitMq { - private const NEW_SHORT_URL_QUEUE = 'https://shlink.io/new-short-url'; - public function __construct( private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper, private readonly EntityManagerInterface $em, @@ -45,7 +44,7 @@ class NotifyNewShortUrlToRabbitMq try { $this->rabbitMqHelper->publishPayloadInQueue( $this->shortUrlTransformer->transform($shortUrl), - self::NEW_SHORT_URL_QUEUE, + Topic::NEW_SHORT_URL->value, ); } catch (Throwable $e) { $this->logger->debug('Error while trying to notify RabbitMQ with new short URL. {e}', ['e' => $e]); diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php index 929e37c0..a10d8673 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php @@ -10,15 +10,11 @@ use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; +use Shlinkio\Shlink\Core\EventDispatcher\Topic; use Throwable; -use function sprintf; - class NotifyVisitToRabbitMq { - private const NEW_VISIT_QUEUE = 'https://shlink.io/new-visit'; - private const NEW_ORPHAN_VISIT_QUEUE = 'https://shlink.io/new-orphan-visit'; - public function __construct( private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper, private readonly EntityManagerInterface $em, @@ -62,12 +58,12 @@ class NotifyVisitToRabbitMq private function determineQueuesToPublishTo(Visit $visit): array { if ($visit->isOrphan()) { - return [self::NEW_ORPHAN_VISIT_QUEUE]; + return [Topic::NEW_ORPHAN_VISIT->value]; } return [ - self::NEW_VISIT_QUEUE, - sprintf('%s/%s', self::NEW_VISIT_QUEUE, $visit->getShortUrl()?->getShortCode()), + Topic::NEW_VISIT->value, + Topic::newShortUrlVisit($visit->getShortUrl()?->getShortCode()), ]; } diff --git a/module/Core/src/EventDispatcher/Topic.php b/module/Core/src/EventDispatcher/Topic.php new file mode 100644 index 00000000..0cba5a09 --- /dev/null +++ b/module/Core/src/EventDispatcher/Topic.php @@ -0,0 +1,19 @@ +value, $shortCode ?? ''); + } +} diff --git a/module/Core/src/Mercure/MercureUpdatesGenerator.php b/module/Core/src/Mercure/MercureUpdatesGenerator.php index 74b85388..f84d296d 100644 --- a/module/Core/src/Mercure/MercureUpdatesGenerator.php +++ b/module/Core/src/Mercure/MercureUpdatesGenerator.php @@ -6,16 +6,13 @@ namespace Shlinkio\Shlink\Core\Mercure; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Core\Entity\Visit; +use Shlinkio\Shlink\Core\EventDispatcher\Topic; use Symfony\Component\Mercure\Update; use function Shlinkio\Shlink\Common\json_encode; -use function sprintf; final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface { - private const NEW_VISIT_TOPIC = 'https://shlink.io/new-visit'; - private const NEW_ORPHAN_VISIT_TOPIC = 'https://shlink.io/new-orphan-visit'; - public function __construct( private DataTransformerInterface $shortUrlTransformer, private DataTransformerInterface $orphanVisitTransformer, @@ -24,7 +21,7 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface public function newVisitUpdate(Visit $visit): Update { - return new Update(self::NEW_VISIT_TOPIC, json_encode([ + return new Update(Topic::NEW_VISIT->value, json_encode([ 'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()), 'visit' => $visit, ])); @@ -32,7 +29,7 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface public function newOrphanVisitUpdate(Visit $visit): Update { - return new Update(self::NEW_ORPHAN_VISIT_TOPIC, json_encode([ + return new Update(Topic::NEW_ORPHAN_VISIT->value, json_encode([ 'visit' => $this->orphanVisitTransformer->transform($visit), ])); } @@ -40,7 +37,7 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface public function newShortUrlVisitUpdate(Visit $visit): Update { $shortUrl = $visit->getShortUrl(); - $topic = sprintf('%s/%s', self::NEW_VISIT_TOPIC, $shortUrl?->getShortCode()); + $topic = Topic::newShortUrlVisit($shortUrl?->getShortCode()); return new Update($topic, json_encode([ 'shortUrl' => $this->shortUrlTransformer->transform($shortUrl),