mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-28 00:38:46 +03:00
Extracted real-time update topic names to an enum
This commit is contained in:
parent
fc6b4c12b2
commit
4d1af867a4
4 changed files with 29 additions and 18 deletions
|
@ -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]);
|
||||
|
|
|
@ -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()),
|
||||
];
|
||||
}
|
||||
|
||||
|
|
19
module/Core/src/EventDispatcher/Topic.php
Normal file
19
module/Core/src/EventDispatcher/Topic.php
Normal 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 ?? '');
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
|
|
Loading…
Reference in a new issue