Migrated mercure event listeners to use new publishing helper from shlink-common

This commit is contained in:
Alejandro Celaya 2022-07-26 12:17:37 +02:00
parent 791d6b7e57
commit 1b089749c0
8 changed files with 55 additions and 55 deletions

View file

@ -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',

View file

@ -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]);
}

View file

@ -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,

View file

@ -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),
]));
]);
}
}

View file

@ -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
{

View file

@ -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'));

View file

@ -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));

View file

@ -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);
}
}