Extracted real-time update topic names to an enum

This commit is contained in:
Alejandro Celaya 2022-07-24 12:05:55 +02:00
parent fc6b4c12b2
commit 4d1af867a4
4 changed files with 29 additions and 18 deletions

View file

@ -10,12 +10,11 @@ use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated; use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Throwable; use Throwable;
class NotifyNewShortUrlToRabbitMq class NotifyNewShortUrlToRabbitMq
{ {
private const NEW_SHORT_URL_QUEUE = 'https://shlink.io/new-short-url';
public function __construct( public function __construct(
private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper, private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper,
private readonly EntityManagerInterface $em, private readonly EntityManagerInterface $em,
@ -45,7 +44,7 @@ class NotifyNewShortUrlToRabbitMq
try { try {
$this->rabbitMqHelper->publishPayloadInQueue( $this->rabbitMqHelper->publishPayloadInQueue(
$this->shortUrlTransformer->transform($shortUrl), $this->shortUrlTransformer->transform($shortUrl),
self::NEW_SHORT_URL_QUEUE, Topic::NEW_SHORT_URL->value,
); );
} catch (Throwable $e) { } catch (Throwable $e) {
$this->logger->debug('Error while trying to notify RabbitMQ with new short URL. {e}', ['e' => $e]); $this->logger->debug('Error while trying to notify RabbitMQ with new short URL. {e}', ['e' => $e]);

View file

@ -10,15 +10,11 @@ use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Throwable; use Throwable;
use function sprintf;
class NotifyVisitToRabbitMq 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( public function __construct(
private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper, private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper,
private readonly EntityManagerInterface $em, private readonly EntityManagerInterface $em,
@ -62,12 +58,12 @@ class NotifyVisitToRabbitMq
private function determineQueuesToPublishTo(Visit $visit): array private function determineQueuesToPublishTo(Visit $visit): array
{ {
if ($visit->isOrphan()) { if ($visit->isOrphan()) {
return [self::NEW_ORPHAN_VISIT_QUEUE]; return [Topic::NEW_ORPHAN_VISIT->value];
} }
return [ return [
self::NEW_VISIT_QUEUE, Topic::NEW_VISIT->value,
sprintf('%s/%s', self::NEW_VISIT_QUEUE, $visit->getShortUrl()?->getShortCode()), Topic::newShortUrlVisit($visit->getShortUrl()?->getShortCode()),
]; ];
} }

View file

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher;
use function sprintf;
enum Topic: string
{
case NEW_VISIT = 'https://shlink.io/new-visit';
case NEW_ORPHAN_VISIT = 'https://shlink.io/new-orphan-visit';
case NEW_SHORT_URL = 'https://shlink.io/new-short-url';
public static function newShortUrlVisit(?string $shortCode): string
{
return sprintf('%s/%s', self::NEW_VISIT->value, $shortCode ?? '');
}
}

View file

@ -6,16 +6,13 @@ namespace Shlinkio\Shlink\Core\Mercure;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Symfony\Component\Mercure\Update; use Symfony\Component\Mercure\Update;
use function Shlinkio\Shlink\Common\json_encode; use function Shlinkio\Shlink\Common\json_encode;
use function sprintf;
final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface 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( public function __construct(
private DataTransformerInterface $shortUrlTransformer, private DataTransformerInterface $shortUrlTransformer,
private DataTransformerInterface $orphanVisitTransformer, private DataTransformerInterface $orphanVisitTransformer,
@ -24,7 +21,7 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface
public function newVisitUpdate(Visit $visit): Update 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()), 'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()),
'visit' => $visit, 'visit' => $visit,
])); ]));
@ -32,7 +29,7 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface
public function newOrphanVisitUpdate(Visit $visit): Update 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), 'visit' => $this->orphanVisitTransformer->transform($visit),
])); ]));
} }
@ -40,7 +37,7 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface
public function newShortUrlVisitUpdate(Visit $visit): Update public function newShortUrlVisitUpdate(Visit $visit): Update
{ {
$shortUrl = $visit->getShortUrl(); $shortUrl = $visit->getShortUrl();
$topic = sprintf('%s/%s', self::NEW_VISIT_TOPIC, $shortUrl?->getShortCode()); $topic = Topic::newShortUrlVisit($shortUrl?->getShortCode());
return new Update($topic, json_encode([ return new Update($topic, json_encode([
'shortUrl' => $this->shortUrlTransformer->transform($shortUrl), 'shortUrl' => $this->shortUrlTransformer->transform($shortUrl),