From 791d6b7e5773ba3594da75de39c3fbc2dd753560 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 26 Jul 2022 12:07:27 +0200 Subject: [PATCH] Updated to latest common, with unified publishing API --- composer.json | 15 ++++---- .../RabbitMq/NotifyNewShortUrlToRabbitMq.php | 11 +++--- .../RabbitMq/NotifyVisitToRabbitMq.php | 9 +++-- .../RedisPubSub/NotifyNewShortUrlToRedis.php | 11 +++--- .../RedisPubSub/NotifyVisitToRedis.php | 9 +++-- .../NotifyNewShortUrlToRabbitMqTest.php | 16 ++++----- .../RabbitMq/NotifyVisitToRabbitMqTest.php | 34 +++++++++++-------- 7 files changed, 58 insertions(+), 47 deletions(-) diff --git a/composer.json b/composer.json index 197db58b..9add38d7 100644 --- a/composer.json +++ b/composer.json @@ -43,18 +43,17 @@ "php-middleware/request-id": "^4.1", "pugx/shortid-php": "^1.0", "ramsey/uuid": "^4.2", - "shlinkio/shlink-common": "dev-main#4019020 as 4.5", + "shlinkio/shlink-common": "dev-main#b3848ad as 4.5", "shlinkio/shlink-config": "^1.6", "shlinkio/shlink-event-dispatcher": "^2.4", "shlinkio/shlink-importer": "^3.0", "shlinkio/shlink-installer": "dev-develop#f76e9aa as 7.2", "shlinkio/shlink-ip-geolocation": "^2.2", - "symfony/console": "^6.0", - "symfony/filesystem": "^6.0", - "symfony/lock": "^6.0", - "symfony/mercure": "^0.6", - "symfony/process": "^6.0", - "symfony/string": "^6.0" + "symfony/console": "^6.1", + "symfony/filesystem": "^6.1", + "symfony/lock": "^6.1", + "symfony/process": "^6.1", + "symfony/string": "^6.1" }, "require-dev": { "cebe/php-openapi": "^1.7", @@ -71,7 +70,7 @@ "roave/security-advisories": "dev-master", "shlinkio/php-coding-standard": "~2.3.0", "shlinkio/shlink-test-utils": "^3.0.1", - "symfony/var-dumper": "^6.0", + "symfony/var-dumper": "^6.1", "veewee/composer-run-parallel": "^1.1" }, "autoload": { diff --git a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php index 22a7582d..73ed84ca 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMq.php @@ -6,8 +6,9 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RabbitMq; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; -use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface; 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; @@ -17,7 +18,7 @@ use Throwable; class NotifyNewShortUrlToRabbitMq { public function __construct( - private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper, + private readonly PublishingHelperInterface $rabbitMqHelper, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, private readonly DataTransformerInterface $shortUrlTransformer, @@ -43,10 +44,10 @@ class NotifyNewShortUrlToRabbitMq } try { - $this->rabbitMqHelper->publishPayloadInQueue( - ['shortUrl' => $this->shortUrlTransformer->transform($shortUrl)], + $this->rabbitMqHelper->publishUpdate(Update::forTopicAndPayload( Topic::NEW_SHORT_URL->value, - ); + ['shortUrl' => $this->shortUrlTransformer->transform($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/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php index b3e4e8c1..8f8567db 100644 --- a/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php +++ b/module/Core/src/EventDispatcher/RabbitMq/NotifyVisitToRabbitMq.php @@ -6,8 +6,9 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RabbitMq; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; -use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\Topic; @@ -19,7 +20,7 @@ use function Functional\each; class NotifyVisitToRabbitMq { public function __construct( - private readonly RabbitMqPublishingHelperInterface $rabbitMqHelper, + private readonly PublishingHelperInterface $rabbitMqHelper, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, private readonly DataTransformerInterface $orphanVisitTransformer, @@ -48,7 +49,9 @@ class NotifyVisitToRabbitMq $payload = $this->visitToPayload($visit); try { - each($queues, fn (string $queue) => $this->rabbitMqHelper->publishPayloadInQueue($payload, $queue)); + each($queues, fn (string $queue) => $this->rabbitMqHelper->publishUpdate( + Update::forTopicAndPayload($queue, $payload), + )); } catch (Throwable $e) { $this->logger->debug('Error while trying to notify RabbitMQ with new visit. {e}', ['e' => $e]); } diff --git a/module/Core/src/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedis.php b/module/Core/src/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedis.php index b97f0047..638aa88a 100644 --- a/module/Core/src/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedis.php +++ b/module/Core/src/EventDispatcher/RedisPubSub/NotifyNewShortUrlToRedis.php @@ -6,8 +6,9 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RedisPubSub; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; -use Shlinkio\Shlink\Common\Cache\RedisPublishingHelperInterface; 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; @@ -16,7 +17,7 @@ use Throwable; class NotifyNewShortUrlToRedis { public function __construct( - private readonly RedisPublishingHelperInterface $redisHelper, + private readonly PublishingHelperInterface $redisHelper, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, private readonly DataTransformerInterface $shortUrlTransformer, @@ -42,10 +43,10 @@ class NotifyNewShortUrlToRedis } try { - $this->redisHelper->publishPayloadInQueue( - ['shortUrl' => $this->shortUrlTransformer->transform($shortUrl)], + $this->redisHelper->publishUpdate(Update::forTopicAndPayload( Topic::NEW_SHORT_URL->value, - ); + ['shortUrl' => $this->shortUrlTransformer->transform($shortUrl)], + )); } catch (Throwable $e) { $this->logger->debug('Error while trying to notify Redis pub/sub with new short URL. {e}', ['e' => $e]); } diff --git a/module/Core/src/EventDispatcher/RedisPubSub/NotifyVisitToRedis.php b/module/Core/src/EventDispatcher/RedisPubSub/NotifyVisitToRedis.php index 0677c1ed..3df32267 100644 --- a/module/Core/src/EventDispatcher/RedisPubSub/NotifyVisitToRedis.php +++ b/module/Core/src/EventDispatcher/RedisPubSub/NotifyVisitToRedis.php @@ -6,8 +6,9 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RedisPubSub; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; -use Shlinkio\Shlink\Common\Cache\RedisPublishingHelperInterface; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; use Shlinkio\Shlink\Core\EventDispatcher\Topic; @@ -18,7 +19,7 @@ use function Functional\each; class NotifyVisitToRedis { public function __construct( - private readonly RedisPublishingHelperInterface $redisHelper, + private readonly PublishingHelperInterface $redisHelper, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, private readonly DataTransformerInterface $orphanVisitTransformer, @@ -48,7 +49,9 @@ class NotifyVisitToRedis $payload = $this->visitToPayload($visit); try { - each($queues, fn (string $queue) => $this->redisHelper->publishPayloadInQueue($payload, $queue)); + each($queues, fn (string $queue) => $this->redisHelper->publishUpdate( + Update::forTopicAndPayload($queue, $payload), + )); } catch (Throwable $e) { $this->logger->debug('Error while trying to notify Redis pub/sub with new visit. {e}', ['e' => $e]); } diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php index e98e342f..1f23b18b 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyNewShortUrlToRabbitMqTest.php @@ -13,7 +13,8 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Psr\Log\LoggerInterface; use RuntimeException; -use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface; +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\RabbitMq\NotifyNewShortUrlToRabbitMq; @@ -35,7 +36,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase protected function setUp(): void { - $this->helper = $this->prophesize(RabbitMqPublishingHelperInterface::class); + $this->helper = $this->prophesize(PublishingHelperInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class); $this->options = new RabbitMqOptions(['enabled' => true]); @@ -59,7 +60,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase $this->em->find(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); - $this->helper->publishPayloadInQueue(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); } /** @test */ @@ -77,7 +78,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase $find->shouldHaveBeenCalledOnce(); $logWarning->shouldHaveBeenCalledOnce(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); - $this->helper->publishPayloadInQueue(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); } /** @test */ @@ -89,9 +90,8 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase ($this->listener)(new ShortUrlCreated($shortUrlId)); $find->shouldHaveBeenCalledOnce(); - $this->helper->publishPayloadInQueue( - Argument::type('array'), - Topic::NEW_SHORT_URL->value, + $this->helper->publishUpdate( + Argument::that(fn (Update $update) => $update->topic === Topic::NEW_SHORT_URL->value), )->shouldHaveBeenCalledOnce(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); } @@ -104,7 +104,7 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase { $shortUrlId = '123'; $find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(ShortUrl::withLongUrl('')); - $publish = $this->helper->publishPayloadInQueue(Argument::cetera())->willThrow($e); + $publish = $this->helper->publishUpdate(Argument::cetera())->willThrow($e); ($this->listener)(new ShortUrlCreated($shortUrlId)); diff --git a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php index 6d367c17..ada7e551 100644 --- a/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php +++ b/module/Core/test/EventDispatcher/RabbitMq/NotifyVisitToRabbitMqTest.php @@ -14,7 +14,8 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Psr\Log\LoggerInterface; use RuntimeException; -use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelperInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated; @@ -42,7 +43,7 @@ class NotifyVisitToRabbitMqTest extends TestCase protected function setUp(): void { - $this->helper = $this->prophesize(RabbitMqPublishingHelperInterface::class); + $this->helper = $this->prophesize(PublishingHelperInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class); $this->options = new RabbitMqOptions(['enabled' => true, 'legacy_visits_publishing' => true]); @@ -67,7 +68,7 @@ class NotifyVisitToRabbitMqTest extends TestCase $this->em->find(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); - $this->helper->publishPayloadInQueue(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); } /** @test */ @@ -85,7 +86,7 @@ class NotifyVisitToRabbitMqTest extends TestCase $findVisit->shouldHaveBeenCalledOnce(); $logWarning->shouldHaveBeenCalledOnce(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); - $this->helper->publishPayloadInQueue(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); } /** @@ -97,16 +98,15 @@ class NotifyVisitToRabbitMqTest extends TestCase $visitId = '123'; $findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit); $argumentWithExpectedChannels = Argument::that( - static fn (string $channel) => contains($expectedChannels, $channel), + static fn (Update $update) => contains($expectedChannels, $update->topic), ); ($this->listener)(new VisitLocated($visitId)); $findVisit->shouldHaveBeenCalledOnce(); - $this->helper->publishPayloadInQueue( - Argument::type('array'), - $argumentWithExpectedChannels, - )->shouldHaveBeenCalledTimes(count($expectedChannels)); + $this->helper->publishUpdate($argumentWithExpectedChannels)->shouldHaveBeenCalledTimes( + count($expectedChannels), + ); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); } @@ -135,7 +135,7 @@ class NotifyVisitToRabbitMqTest extends TestCase { $visitId = '123'; $findVisit = $this->em->find(Visit::class, $visitId)->willReturn(Visit::forBasePath(Visitor::emptyInstance())); - $publish = $this->helper->publishPayloadInQueue(Argument::cetera())->willThrow($e); + $publish = $this->helper->publishUpdate(Argument::cetera())->willThrow($e); ($this->listener)(new VisitLocated($visitId)); @@ -171,7 +171,7 @@ class NotifyVisitToRabbitMqTest extends TestCase ($this->listener)(new VisitLocated($visitId)); $findVisit->shouldHaveBeenCalledOnce(); - $this->helper->publishPayloadInQueue(Argument::that($assertPayload), Argument::type('string')) + $this->helper->publishUpdate(Argument::that($assertPayload)) ->shouldHaveBeenCalled(); } @@ -180,7 +180,8 @@ class NotifyVisitToRabbitMqTest extends TestCase yield 'non-legacy non-orphan visit' => [ true, $visit = Visit::forValidShortUrl(ShortUrl::withLongUrl(''), Visitor::emptyInstance()), - function (array $payload) use ($visit): bool { + function (Update $update) use ($visit): bool { + $payload = $update->payload; Assert::assertEquals($payload, $visit->jsonSerialize()); Assert::assertArrayNotHasKey('visitedUrl', $payload); Assert::assertArrayNotHasKey('type', $payload); @@ -193,7 +194,8 @@ class NotifyVisitToRabbitMqTest extends TestCase yield 'non-legacy orphan visit' => [ true, Visit::forBasePath(Visitor::emptyInstance()), - function (array $payload): bool { + function (Update $update): bool { + $payload = $update->payload; Assert::assertArrayHasKey('visitedUrl', $payload); Assert::assertArrayHasKey('type', $payload); @@ -203,7 +205,8 @@ class NotifyVisitToRabbitMqTest extends TestCase yield 'legacy non-orphan visit' => [ false, $visit = Visit::forValidShortUrl(ShortUrl::withLongUrl(''), Visitor::emptyInstance()), - function (array $payload) use ($visit): bool { + function (Update $update) use ($visit): bool { + $payload = $update->payload; Assert::assertArrayHasKey('visit', $payload); Assert::assertArrayHasKey('shortUrl', $payload); Assert::assertIsArray($payload['visit']); @@ -217,7 +220,8 @@ class NotifyVisitToRabbitMqTest extends TestCase yield 'legacy orphan visit' => [ false, Visit::forBasePath(Visitor::emptyInstance()), - function (array $payload): bool { + function (Update $update): bool { + $payload = $update->payload; Assert::assertArrayHasKey('visit', $payload); Assert::assertArrayNotHasKey('shortUrl', $payload); Assert::assertIsArray($payload['visit']);