diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index d2e3d08f..4a9dc32d 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -113,9 +113,9 @@ return [ ], EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => [ RabbitMqPublishingHelper::class, + EventDispatcher\PublishingUpdatesGenerator::class, 'em', 'Logger_Shlink', - ShortUrl\Transformer\ShortUrlDataTransformer::class, Options\RabbitMqOptions::class, ], EventDispatcher\RedisPubSub\NotifyVisitToRedis::class => [ diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php index 73ed84ca..ad562d2a 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php @@ -6,12 +6,10 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RabbitMq; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; -use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; -use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated; -use Shlinkio\Shlink\Core\EventDispatcher\Topic; +use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface; use Shlinkio\Shlink\Core\Options\RabbitMqOptions; use Throwable; @@ -19,9 +17,9 @@ class NotifyNewShortUrlToRabbitMq { public function __construct( private readonly PublishingHelperInterface $rabbitMqHelper, + private readonly PublishingUpdatesGeneratorInterface $updatesGenerator, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, - private readonly DataTransformerInterface $shortUrlTransformer, private readonly RabbitMqOptions $options, ) { } @@ -44,10 +42,7 @@ class NotifyNewShortUrlToRabbitMq } try { - $this->rabbitMqHelper->publishUpdate(Update::forTopicAndPayload( - Topic::NEW_SHORT_URL->value, - ['shortUrl' => $this->shortUrlTransformer->transform($shortUrl)], - )); + $this->rabbitMqHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl)); } catch (Throwable $e) { $this->logger->debug('Error while trying to notify RabbitMQ with new short URL. {e}', ['e' => $e]); } diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php index 1f23b18b..ba735d6a 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php @@ -17,11 +17,10 @@ use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated; +use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface; use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq; use Shlinkio\Shlink\Core\EventDispatcher\Topic; use Shlinkio\Shlink\Core\Options\RabbitMqOptions; -use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; -use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer; use Throwable; class NotifyNewShortUrlToRabbitMqTest extends TestCase @@ -30,6 +29,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase private NotifyNewShortUrlToRabbitMq $listener; private ObjectProphecy $helper; + private ObjectProphecy $updatesGenerator; private ObjectProphecy $em; private ObjectProphecy $logger; private RabbitMqOptions $options; @@ -37,15 +37,16 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase protected function setUp(): void { $this->helper = $this->prophesize(PublishingHelperInterface::class); + $this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class); $this->options = new RabbitMqOptions(['enabled' => true]); $this->listener = new NotifyNewShortUrlToRabbitMq( $this->helper->reveal(), + $this->updatesGenerator->reveal(), $this->em->reveal(), $this->logger->reveal(), - new ShortUrlDataTransformer(new ShortUrlStringifier([])), $this->options, ); } @@ -85,14 +86,17 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase public function expectedChannelIsNotified(): void { $shortUrlId = '123'; + $update = Update::forTopicAndPayload(Topic::NEW_SHORT_URL->value, []); $find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(ShortUrl::withLongUrl('')); + $generateUpdate = $this->updatesGenerator->newShortUrlUpdate(Argument::type(ShortUrl::class))->willReturn( + $update, + ); ($this->listener)(new ShortUrlCreated($shortUrlId)); $find->shouldHaveBeenCalledOnce(); - $this->helper->publishUpdate( - Argument::that(fn (Update $update) => $update->topic === Topic::NEW_SHORT_URL->value), - )->shouldHaveBeenCalledOnce(); + $generateUpdate->shouldHaveBeenCalledOnce(); + $this->helper->publishUpdate($update)->shouldHaveBeenCalledOnce(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); }