From 1b089749c0bd713be4b138a741fbbb6de3705715 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 26 Jul 2022 12:17:37 +0200 Subject: [PATCH] Migrated mercure event listeners to use new publishing helper from shlink-common --- .../Core/config/event_dispatcher.config.php | 6 ++--- .../Mercure/NotifyNewShortUrlToMercure.php | 6 ++--- .../Mercure/NotifyVisitToMercure.php | 11 +++++---- .../src/Mercure/MercureUpdatesGenerator.php | 24 +++++++++---------- .../MercureUpdatesGeneratorInterface.php | 2 +- .../NotifyNewShortUrlToMercureTest.php | 21 ++++++++-------- .../Mercure/NotifyVisitToMercureTest.php | 24 +++++++++---------- .../Mercure/MercureUpdatesGeneratorTest.php | 16 ++++++------- 8 files changed, 55 insertions(+), 55 deletions(-) diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index 908c8183..4ac8f365 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -8,10 +8,10 @@ use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Psr\EventDispatcher\EventDispatcherInterface; use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdater; use Shlinkio\Shlink\Common\Cache\RedisPublishingHelper; +use Shlinkio\Shlink\Common\Mercure\MercureHubPublishingHelper; use Shlinkio\Shlink\Common\RabbitMq\RabbitMqPublishingHelper; use Shlinkio\Shlink\IpGeolocation\GeoLite2\DbUpdater; use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface; -use Symfony\Component\Mercure\Hub; return [ @@ -92,13 +92,13 @@ return [ Options\AppOptions::class, ], EventDispatcher\Mercure\NotifyVisitToMercure::class => [ - Hub::class, + MercureHubPublishingHelper::class, Mercure\MercureUpdatesGenerator::class, 'em', 'Logger_Shlink', ], EventDispatcher\Mercure\NotifyNewShortUrlToMercure::class => [ - Hub::class, + MercureHubPublishingHelper::class, Mercure\MercureUpdatesGenerator::class, 'em', 'Logger_Shlink', diff --git a/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php b/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php index 8e93d88b..15147403 100644 --- a/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php +++ b/module/Core/src/EventDispatcher/Mercure/NotifyNewShortUrlToMercure.php @@ -6,16 +6,16 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\Mercure; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; 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 PublishingHelperInterface $mercureHelper, private readonly MercureUpdatesGeneratorInterface $updatesGenerator, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, @@ -36,7 +36,7 @@ class NotifyNewShortUrlToMercure } try { - $this->hub->publish($this->updatesGenerator->newShortUrlUpdate($shortUrl)); + $this->mercureHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl)); } catch (Throwable $e) { $this->logger->debug('Error while trying to notify mercure hub with new short URL. {e}', ['e' => $e]); } diff --git a/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php b/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php index f9610021..6eab680c 100644 --- a/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php +++ b/module/Core/src/EventDispatcher/Mercure/NotifyVisitToMercure.php @@ -6,11 +6,11 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\Mercure; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; +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\Mercure\MercureUpdatesGeneratorInterface; -use Symfony\Component\Mercure\HubInterface; -use Symfony\Component\Mercure\Update; use Throwable; use function Functional\each; @@ -18,7 +18,7 @@ use function Functional\each; class NotifyVisitToMercure { public function __construct( - private readonly HubInterface $hub, + private readonly PublishingHelperInterface $mercureHelper, private readonly MercureUpdatesGeneratorInterface $updatesGenerator, private readonly EntityManagerInterface $em, private readonly LoggerInterface $logger, @@ -39,7 +39,10 @@ class NotifyVisitToMercure } try { - each($this->determineUpdatesForVisit($visit), fn (Update $update) => $this->hub->publish($update)); + each( + $this->determineUpdatesForVisit($visit), + fn (Update $update) => $this->mercureHelper->publishUpdate($update), + ); } catch (Throwable $e) { $this->logger->debug('Error while trying to notify mercure hub with new visit. {e}', [ 'e' => $e, diff --git a/module/Core/src/Mercure/MercureUpdatesGenerator.php b/module/Core/src/Mercure/MercureUpdatesGenerator.php index 0f01faa2..055c4fb6 100644 --- a/module/Core/src/Mercure/MercureUpdatesGenerator.php +++ b/module/Core/src/Mercure/MercureUpdatesGenerator.php @@ -5,12 +5,10 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Mercure; use Shlinkio\Shlink\Common\Rest\DataTransformerInterface; +use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\EventDispatcher\Topic; -use Symfony\Component\Mercure\Update; - -use function Shlinkio\Shlink\Common\json_encode; final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface { @@ -22,17 +20,17 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface public function newVisitUpdate(Visit $visit): Update { - return new Update(Topic::NEW_VISIT->value, json_encode([ + return Update::forTopicAndPayload(Topic::NEW_VISIT->value, [ 'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()), - 'visit' => $visit, - ])); + 'visit' => $visit->jsonSerialize(), + ]); } public function newOrphanVisitUpdate(Visit $visit): Update { - return new Update(Topic::NEW_ORPHAN_VISIT->value, json_encode([ + return Update::forTopicAndPayload(Topic::NEW_ORPHAN_VISIT->value, [ 'visit' => $this->orphanVisitTransformer->transform($visit), - ])); + ]); } public function newShortUrlVisitUpdate(Visit $visit): Update @@ -40,16 +38,16 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface $shortUrl = $visit->getShortUrl(); $topic = Topic::newShortUrlVisit($shortUrl?->getShortCode()); - return new Update($topic, json_encode([ + return Update::forTopicAndPayload($topic, [ 'shortUrl' => $this->shortUrlTransformer->transform($shortUrl), - 'visit' => $visit, - ])); + 'visit' => $visit->jsonSerialize(), + ]); } public function newShortUrlUpdate(ShortUrl $shortUrl): Update { - return new Update(Topic::NEW_SHORT_URL->value, json_encode([ + return Update::forTopicAndPayload(Topic::NEW_SHORT_URL->value, [ 'shortUrl' => $this->shortUrlTransformer->transform($shortUrl), - ])); + ]); } } diff --git a/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php b/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php index ee0cd593..732b6954 100644 --- a/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php +++ b/module/Core/src/Mercure/MercureUpdatesGeneratorInterface.php @@ -4,9 +4,9 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Mercure; +use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; -use Symfony\Component\Mercure\Update; interface MercureUpdatesGeneratorInterface { diff --git a/module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php b/module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php index 6bc2d527..d360a15c 100644 --- a/module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php +++ b/module/Core/test/EventDispatcher/Mercure/NotifyNewShortUrlToMercureTest.php @@ -11,32 +11,32 @@ use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Psr\Log\LoggerInterface; +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\Mercure\NotifyNewShortUrlToMercure; use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface; -use Symfony\Component\Mercure\HubInterface; -use Symfony\Component\Mercure\Update; class NotifyNewShortUrlToMercureTest extends TestCase { use ProphecyTrait; private NotifyNewShortUrlToMercure $listener; - private ObjectProphecy $hub; + private ObjectProphecy $helper; private ObjectProphecy $updatesGenerator; private ObjectProphecy $em; private ObjectProphecy $logger; protected function setUp(): void { - $this->hub = $this->prophesize(HubInterface::class); + $this->helper = $this->prophesize(PublishingHelperInterface::class); $this->updatesGenerator = $this->prophesize(MercureUpdatesGeneratorInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class); $this->listener = new NotifyNewShortUrlToMercure( - $this->hub->reveal(), + $this->helper->reveal(), $this->updatesGenerator->reveal(), $this->em->reveal(), $this->logger->reveal(), @@ -55,7 +55,7 @@ class NotifyNewShortUrlToMercureTest extends TestCase 'Tried to notify Mercure for new short URL with id "{shortUrlId}", but it does not exist.', ['shortUrlId' => '123'], )->shouldHaveBeenCalledOnce(); - $this->hub->publish(Argument::cetera())->shouldNotHaveBeenCalled(); + $this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); $this->updatesGenerator->newShortUrlUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); } @@ -64,17 +64,16 @@ class NotifyNewShortUrlToMercureTest extends TestCase public function expectedNotificationIsPublished(): void { $shortUrl = ShortUrl::withLongUrl(''); - $update = new Update([]); + $update = Update::forTopicAndPayload('', []); $find = $this->em->find(ShortUrl::class, '123')->willReturn($shortUrl); $newUpdate = $this->updatesGenerator->newShortUrlUpdate($shortUrl)->willReturn($update); - $publish = $this->hub->publish($update)->willReturn(''); ($this->listener)(new ShortUrlCreated('123')); $find->shouldHaveBeenCalledOnce(); $newUpdate->shouldHaveBeenCalledOnce(); - $publish->shouldHaveBeenCalledOnce(); + $this->helper->publishUpdate($update)->shouldHaveBeenCalledOnce(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->debug(Argument::cetera())->shouldNotHaveBeenCalled(); } @@ -83,12 +82,12 @@ class NotifyNewShortUrlToMercureTest extends TestCase public function messageIsPrintedIfPublishingFails(): void { $shortUrl = ShortUrl::withLongUrl(''); - $update = new Update([]); + $update = Update::forTopicAndPayload('', []); $e = new Exception('Error'); $find = $this->em->find(ShortUrl::class, '123')->willReturn($shortUrl); $newUpdate = $this->updatesGenerator->newShortUrlUpdate($shortUrl)->willReturn($update); - $publish = $this->hub->publish($update)->willThrow($e); + $publish = $this->helper->publishUpdate($update)->willThrow($e); ($this->listener)(new ShortUrlCreated('123')); diff --git a/module/Core/test/EventDispatcher/Mercure/NotifyVisitToMercureTest.php b/module/Core/test/EventDispatcher/Mercure/NotifyVisitToMercureTest.php index bdcb72a8..a0ec417c 100644 --- a/module/Core/test/EventDispatcher/Mercure/NotifyVisitToMercureTest.php +++ b/module/Core/test/EventDispatcher/Mercure/NotifyVisitToMercureTest.php @@ -11,6 +11,8 @@ use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; use Psr\Log\LoggerInterface; use RuntimeException; +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; @@ -18,28 +20,26 @@ use Shlinkio\Shlink\Core\EventDispatcher\Mercure\NotifyVisitToMercure; use Shlinkio\Shlink\Core\Mercure\MercureUpdatesGeneratorInterface; use Shlinkio\Shlink\Core\Model\Visitor; use Shlinkio\Shlink\Core\Visit\Model\VisitType; -use Symfony\Component\Mercure\HubInterface; -use Symfony\Component\Mercure\Update; class NotifyVisitToMercureTest extends TestCase { use ProphecyTrait; private NotifyVisitToMercure $listener; - private ObjectProphecy $hub; + private ObjectProphecy $helper; private ObjectProphecy $updatesGenerator; private ObjectProphecy $em; private ObjectProphecy $logger; public function setUp(): void { - $this->hub = $this->prophesize(HubInterface::class); + $this->helper = $this->prophesize(PublishingHelperInterface::class); $this->updatesGenerator = $this->prophesize(MercureUpdatesGeneratorInterface::class); $this->em = $this->prophesize(EntityManagerInterface::class); $this->logger = $this->prophesize(LoggerInterface::class); $this->listener = new NotifyVisitToMercure( - $this->hub->reveal(), + $this->helper->reveal(), $this->updatesGenerator->reveal(), $this->em->reveal(), $this->logger->reveal(), @@ -61,7 +61,7 @@ class NotifyVisitToMercureTest extends TestCase ); $buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate(Argument::type(Visit::class)); $buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate(Argument::type(Visit::class)); - $publish = $this->hub->publish(Argument::type(Update::class)); + $publish = $this->helper->publishUpdate(Argument::type(Update::class)); ($this->listener)(new VisitLocated($visitId)); @@ -79,7 +79,7 @@ class NotifyVisitToMercureTest extends TestCase { $visitId = '123'; $visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()); - $update = new Update('', ''); + $update = Update::forTopicAndPayload('', []); $findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit); $logWarning = $this->logger->warning(Argument::cetera()); @@ -87,7 +87,7 @@ class NotifyVisitToMercureTest extends TestCase $buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate($visit)->willReturn($update); $buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate($visit)->willReturn($update); $buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate($visit)->willReturn($update); - $publish = $this->hub->publish($update); + $publish = $this->helper->publishUpdate($update); ($this->listener)(new VisitLocated($visitId)); @@ -105,7 +105,7 @@ class NotifyVisitToMercureTest extends TestCase { $visitId = '123'; $visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance()); - $update = new Update('', ''); + $update = Update::forTopicAndPayload('', []); $e = new RuntimeException('Error'); $findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit); @@ -116,7 +116,7 @@ class NotifyVisitToMercureTest extends TestCase $buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate($visit)->willReturn($update); $buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate($visit)->willReturn($update); $buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate($visit)->willReturn($update); - $publish = $this->hub->publish($update)->willThrow($e); + $publish = $this->helper->publishUpdate($update)->willThrow($e); ($this->listener)(new VisitLocated($visitId)); @@ -136,7 +136,7 @@ class NotifyVisitToMercureTest extends TestCase public function notificationsAreSentForOrphanVisits(Visit $visit): void { $visitId = '123'; - $update = new Update('', ''); + $update = Update::forTopicAndPayload('', []); $findVisit = $this->em->find(Visit::class, $visitId)->willReturn($visit); $logWarning = $this->logger->warning(Argument::cetera()); @@ -144,7 +144,7 @@ class NotifyVisitToMercureTest extends TestCase $buildNewShortUrlVisitUpdate = $this->updatesGenerator->newShortUrlVisitUpdate($visit)->willReturn($update); $buildNewOrphanVisitUpdate = $this->updatesGenerator->newOrphanVisitUpdate($visit)->willReturn($update); $buildNewVisitUpdate = $this->updatesGenerator->newVisitUpdate($visit)->willReturn($update); - $publish = $this->hub->publish($update); + $publish = $this->helper->publishUpdate($update); ($this->listener)(new VisitLocated($visitId)); diff --git a/module/Core/test/Mercure/MercureUpdatesGeneratorTest.php b/module/Core/test/Mercure/MercureUpdatesGeneratorTest.php index d3521f10..5e5910d7 100644 --- a/module/Core/test/Mercure/MercureUpdatesGeneratorTest.php +++ b/module/Core/test/Mercure/MercureUpdatesGeneratorTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace ShlinkioTest\Shlink\Core\Mercure; use PHPUnit\Framework\TestCase; +use Shlinkio\Shlink\Common\UpdatePublishing\Update; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Entity\Visit; use Shlinkio\Shlink\Core\EventDispatcher\Topic; @@ -16,8 +17,6 @@ use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Core\Visit\Model\VisitType; use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer; -use function Shlinkio\Shlink\Common\json_decode; - class MercureUpdatesGeneratorTest extends TestCase { private MercureUpdatesGenerator $generator; @@ -43,9 +42,10 @@ class MercureUpdatesGeneratorTest extends TestCase ])); $visit = Visit::forValidShortUrl($shortUrl, Visitor::emptyInstance()); + /** @var Update $update */ $update = $this->generator->{$method}($visit); - self::assertEquals([$expectedTopic], $update->getTopics()); + self::assertEquals($expectedTopic, $update->topic); self::assertEquals([ 'shortUrl' => [ 'shortCode' => $shortUrl->getShortCode(), @@ -71,7 +71,7 @@ class MercureUpdatesGeneratorTest extends TestCase 'date' => $visit->getDate()->toAtomString(), 'potentialBot' => false, ], - ], json_decode($update->getData())); + ], $update->payload); } public function provideMethod(): iterable @@ -88,7 +88,7 @@ class MercureUpdatesGeneratorTest extends TestCase { $update = $this->generator->newOrphanVisitUpdate($orphanVisit); - self::assertEquals(['https://shlink.io/new-orphan-visit'], $update->getTopics()); + self::assertEquals('https://shlink.io/new-orphan-visit', $update->topic); self::assertEquals([ 'visit' => [ 'referer' => '', @@ -99,7 +99,7 @@ class MercureUpdatesGeneratorTest extends TestCase 'visitedUrl' => $orphanVisit->visitedUrl(), 'type' => $orphanVisit->type()->value, ], - ], json_decode($update->getData())); + ], $update->payload); } public function provideOrphanVisits(): iterable @@ -122,7 +122,7 @@ class MercureUpdatesGeneratorTest extends TestCase $update = $this->generator->newShortUrlUpdate($shortUrl); - self::assertEquals([Topic::NEW_SHORT_URL->value], $update->getTopics()); + self::assertEquals(Topic::NEW_SHORT_URL->value, $update->topic); self::assertEquals(['shortUrl' => [ 'shortCode' => $shortUrl->getShortCode(), 'shortUrl' => 'http:/' . $shortUrl->getShortCode(), @@ -139,6 +139,6 @@ class MercureUpdatesGeneratorTest extends TestCase 'title' => $shortUrl->title(), 'crawlable' => false, 'forwardQuery' => true, - ],], json_decode($update->getData())); + ]], $update->payload); } }