Integrated PublishUpdatesGenerator in NotifyNewShortUrlToRabbitMq listener

This commit is contained in:
Alejandro Celaya 2022-07-27 10:18:28 +02:00
parent 7e8109caa3
commit 3c042c4011
3 changed files with 14 additions and 15 deletions

View file

@ -113,9 +113,9 @@ return [
], ],
EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => [ EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => [
RabbitMqPublishingHelper::class, RabbitMqPublishingHelper::class,
EventDispatcher\PublishingUpdatesGenerator::class,
'em', 'em',
'Logger_Shlink', 'Logger_Shlink',
ShortUrl\Transformer\ShortUrlDataTransformer::class,
Options\RabbitMqOptions::class, Options\RabbitMqOptions::class,
], ],
EventDispatcher\RedisPubSub\NotifyVisitToRedis::class => [ EventDispatcher\RedisPubSub\NotifyVisitToRedis::class => [

View file

@ -6,12 +6,10 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RabbitMq;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface;
use Shlinkio\Shlink\Common\UpdatePublishing\Update;
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 Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\Options\RabbitMqOptions; use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
use Throwable; use Throwable;
@ -19,9 +17,9 @@ class NotifyNewShortUrlToRabbitMq
{ {
public function __construct( public function __construct(
private readonly PublishingHelperInterface $rabbitMqHelper, private readonly PublishingHelperInterface $rabbitMqHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em, private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger, private readonly LoggerInterface $logger,
private readonly DataTransformerInterface $shortUrlTransformer,
private readonly RabbitMqOptions $options, private readonly RabbitMqOptions $options,
) { ) {
} }
@ -44,10 +42,7 @@ class NotifyNewShortUrlToRabbitMq
} }
try { try {
$this->rabbitMqHelper->publishUpdate(Update::forTopicAndPayload( $this->rabbitMqHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl));
Topic::NEW_SHORT_URL->value,
['shortUrl' => $this->shortUrlTransformer->transform($shortUrl)],
));
} 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

@ -17,11 +17,10 @@ use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface;
use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Common\UpdatePublishing\Update;
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\PublishingUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq; use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq;
use Shlinkio\Shlink\Core\EventDispatcher\Topic; use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Shlinkio\Shlink\Core\Options\RabbitMqOptions; use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
use Throwable; use Throwable;
class NotifyNewShortUrlToRabbitMqTest extends TestCase class NotifyNewShortUrlToRabbitMqTest extends TestCase
@ -30,6 +29,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
private NotifyNewShortUrlToRabbitMq $listener; private NotifyNewShortUrlToRabbitMq $listener;
private ObjectProphecy $helper; private ObjectProphecy $helper;
private ObjectProphecy $updatesGenerator;
private ObjectProphecy $em; private ObjectProphecy $em;
private ObjectProphecy $logger; private ObjectProphecy $logger;
private RabbitMqOptions $options; private RabbitMqOptions $options;
@ -37,15 +37,16 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
$this->helper = $this->prophesize(PublishingHelperInterface::class); $this->helper = $this->prophesize(PublishingHelperInterface::class);
$this->updatesGenerator = $this->prophesize(PublishingUpdatesGeneratorInterface::class);
$this->em = $this->prophesize(EntityManagerInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class);
$this->logger = $this->prophesize(LoggerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class);
$this->options = new RabbitMqOptions(['enabled' => true]); $this->options = new RabbitMqOptions(['enabled' => true]);
$this->listener = new NotifyNewShortUrlToRabbitMq( $this->listener = new NotifyNewShortUrlToRabbitMq(
$this->helper->reveal(), $this->helper->reveal(),
$this->updatesGenerator->reveal(),
$this->em->reveal(), $this->em->reveal(),
$this->logger->reveal(), $this->logger->reveal(),
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
$this->options, $this->options,
); );
} }
@ -85,14 +86,17 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
public function expectedChannelIsNotified(): void public function expectedChannelIsNotified(): void
{ {
$shortUrlId = '123'; $shortUrlId = '123';
$update = Update::forTopicAndPayload(Topic::NEW_SHORT_URL->value, []);
$find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(ShortUrl::withLongUrl('')); $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)); ($this->listener)(new ShortUrlCreated($shortUrlId));
$find->shouldHaveBeenCalledOnce(); $find->shouldHaveBeenCalledOnce();
$this->helper->publishUpdate( $generateUpdate->shouldHaveBeenCalledOnce();
Argument::that(fn (Update $update) => $update->topic === Topic::NEW_SHORT_URL->value), $this->helper->publishUpdate($update)->shouldHaveBeenCalledOnce();
)->shouldHaveBeenCalledOnce();
$this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled();
} }