diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index e710bd7d..9ae99e08 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -28,7 +28,7 @@ return [ EventDispatcher\UpdateGeoLiteDb::class, ], EventDispatcher\Event\ShortUrlCreated::class => [ -// EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class, + EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class, EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class, ], ], @@ -39,6 +39,7 @@ return [ EventDispatcher\LocateVisit::class => ConfigAbstractFactory::class, EventDispatcher\NotifyVisitToWebHooks::class => ConfigAbstractFactory::class, EventDispatcher\Mercure\NotifyVisitToMercure::class => ConfigAbstractFactory::class, + EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class => ConfigAbstractFactory::class, EventDispatcher\RabbitMq\NotifyVisitToRabbitMq::class => ConfigAbstractFactory::class, EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => ConfigAbstractFactory::class, EventDispatcher\UpdateGeoLiteDb::class => ConfigAbstractFactory::class, @@ -48,6 +49,9 @@ return [ EventDispatcher\Mercure\NotifyVisitToMercure::class => [ EventDispatcher\CloseDbConnectionEventListenerDelegator::class, ], + EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class => [ + EventDispatcher\CloseDbConnectionEventListenerDelegator::class, + ], EventDispatcher\RabbitMq\NotifyVisitToRabbitMq::class => [ EventDispatcher\CloseDbConnectionEventListenerDelegator::class, ], @@ -82,6 +86,12 @@ return [ 'em', 'Logger_Shlink', ], + EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class => [ + Hub::class, + Mercure\MercureUpdatesGenerator::class, + 'em', + 'Logger_Shlink', + ], EventDispatcher\RabbitMq\NotifyVisitToRabbitMq::class => [ RabbitMqPublishingHelper::class, 'em', diff --git a/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php b/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php index 4ebb8536..46b2f38b 100644 --- a/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php +++ b/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php @@ -4,12 +4,43 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\EventDispatcher\Mercure; +use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; +use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated; +use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface; +use Symfony\Component\Mercure\HubInterface; +use Throwable; class NotifyNewShortUrlToMercure { + public function __construct( + private readonly HubInterface $hub, + private readonly MercureUpdatesGeneratorInterface $updatesGenerator, + private readonly EntityManagerInterface $em, + private readonly LoggerInterface $logger, + ) { + } + public function __invoke(ShortUrlCreated $shortUrlCreated): void { - // TODO: Implement __invoke() method. + $shortUrlId = $shortUrlCreated->shortUrlId; + $shortUrl = $this->em->find(ShortUrl::class, $shortUrlId); + + if ($shortUrl === null) { + $this->logger->warning( + 'Tried to notify Mercure for new short URL with id "{shortUrlId}", but it does not exist.', + ['shortUrlId' => $shortUrlId], + ); + return; + } + + try { + $this->hub->publish($this->updatesGenerator->newShortUrlUpdate($shortUrl)); + } catch (Throwable $e) { + $this->logger->debug('Error while trying to notify mercure hub with new short URL. {e}', [ + 'e' => $e, + ]); + } } }