Fixed new short URL event payload to RabbitMQ, and started to add logic for Mercure

This commit is contained in:
Alejandro Celaya 2022-07-24 12:37:57 +02:00
parent 4d1af867a4
commit 97d24d76d8
7 changed files with 38 additions and 7 deletions

View file

@ -87,6 +87,7 @@ return [
'em',
'Logger_Shlink',
Visit\Transformer\OrphanVisitDataTransformer::class,
ShortUrl\Transformer\ShortUrlDataTransformer::class,
'config.rabbitmq.enabled',
],
EventDispatcher\RabbitMq\NotifyNewShortUrlToRabbitMq::class => [

View file

@ -18,10 +18,10 @@ use function Functional\each;
class NotifyVisitToMercure
{
public function __construct(
private HubInterface $hub,
private MercureUpdatesGeneratorInterface $updatesGenerator,
private EntityManagerInterface $em,
private LoggerInterface $logger,
private readonly HubInterface $hub,
private readonly MercureUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
) {
}

View file

@ -43,7 +43,7 @@ class NotifyNewShortUrlToRabbitMq
try {
$this->rabbitMqHelper->publishPayloadInQueue(
$this->shortUrlTransformer->transform($shortUrl),
['shortUrl' => $this->shortUrlTransformer->transform($shortUrl)],
Topic::NEW_SHORT_URL->value,
);
} catch (Throwable $e) {

View file

@ -20,6 +20,7 @@ class NotifyVisitToRabbitMq
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
private readonly DataTransformerInterface $orphanVisitTransformer,
private readonly DataTransformerInterface $shortUrlTransformer, // @phpstan-ignore-line
private readonly bool $isEnabled,
) {
}
@ -69,6 +70,20 @@ class NotifyVisitToRabbitMq
private function visitToPayload(Visit $visit): array
{
// FIXME This was defined incorrectly.
// According to the spec, both the visit and the short URL it belongs to, should be published.
// The shape should be ['visit' => [...], 'shortUrl' => ?[...]]
// However, this would be a breaking change, so we need a flag that determines the shape of the payload.
return ! $visit->isOrphan() ? $visit->jsonSerialize() : $this->orphanVisitTransformer->transform($visit);
if ($visit->isOrphan()) { // @phpstan-ignore-line
return ['visit' => $this->orphanVisitTransformer->transform($visit)];
}
return [
'visit' => $visit->jsonSerialize(),
'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()),
];
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Mercure;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Symfony\Component\Mercure\Update;
@ -14,8 +15,8 @@ use function Shlinkio\Shlink\Common\json_encode;
final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface
{
public function __construct(
private DataTransformerInterface $shortUrlTransformer,
private DataTransformerInterface $orphanVisitTransformer,
private readonly DataTransformerInterface $shortUrlTransformer,
private readonly DataTransformerInterface $orphanVisitTransformer,
) {
}
@ -44,4 +45,11 @@ final class MercureUpdatesGenerator implements MercureUpdatesGeneratorInterface
'visit' => $visit,
]));
}
public function newShortUrlUpdate(ShortUrl $shortUrl): Update
{
return new Update(Topic::NEW_SHORT_URL->value, json_encode([
'shortUrl' => $this->shortUrlTransformer->transform($shortUrl),
]));
}
}

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Mercure;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
use Symfony\Component\Mercure\Update;
@ -14,4 +15,6 @@ interface MercureUpdatesGeneratorInterface
public function newOrphanVisitUpdate(Visit $visit): Update;
public function newShortUrlVisitUpdate(Visit $visit): Update;
public function newShortUrlUpdate(ShortUrl $shortUrl): Update;
}

View file

@ -20,6 +20,8 @@ use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\Core\EventDispatcher\RabbitMq\NotifyVisitToRabbitMq;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Core\Visit\Transformer\OrphanVisitDataTransformer;
use Throwable;
@ -46,6 +48,7 @@ class NotifyVisitToRabbitMqTest extends TestCase
$this->em->reveal(),
$this->logger->reveal(),
new OrphanVisitDataTransformer(),
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
true,
);
}
@ -58,6 +61,7 @@ class NotifyVisitToRabbitMqTest extends TestCase
$this->em->reveal(),
$this->logger->reveal(),
new OrphanVisitDataTransformer(),
new ShortUrlDataTransformer(new ShortUrlStringifier([])),
false,
);