mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Integrated PublishUpdatesGenerator in NotifyVisitToRedis listener
This commit is contained in:
parent
fa5ebb1677
commit
dada6aa3d1
@ -120,10 +120,9 @@ return [
|
|||||||
],
|
],
|
||||||
EventDispatcher\RedisPubSub\NotifyVisitToRedis::class => [
|
EventDispatcher\RedisPubSub\NotifyVisitToRedis::class => [
|
||||||
RedisPublishingHelper::class,
|
RedisPublishingHelper::class,
|
||||||
|
EventDispatcher\PublishingUpdatesGenerator::class,
|
||||||
'em',
|
'em',
|
||||||
'Logger_Shlink',
|
'Logger_Shlink',
|
||||||
Visit\Transformer\OrphanVisitDataTransformer::class,
|
|
||||||
ShortUrl\Transformer\ShortUrlDataTransformer::class,
|
|
||||||
'config.redis.pub_sub_enabled',
|
'config.redis.pub_sub_enabled',
|
||||||
],
|
],
|
||||||
EventDispatcher\RedisPubSub\NotifyNewShortUrlToRedis::class => [
|
EventDispatcher\RedisPubSub\NotifyNewShortUrlToRedis::class => [
|
||||||
|
@ -6,12 +6,11 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RedisPubSub;
|
|||||||
|
|
||||||
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\Common\UpdatePublishing\Update;
|
||||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
|
||||||
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
|
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
use function Functional\each;
|
use function Functional\each;
|
||||||
@ -20,10 +19,9 @@ class NotifyVisitToRedis
|
|||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly PublishingHelperInterface $redisHelper,
|
private readonly PublishingHelperInterface $redisHelper,
|
||||||
|
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
private readonly LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
private readonly DataTransformerInterface $orphanVisitTransformer,
|
|
||||||
private readonly DataTransformerInterface $shortUrlTransformer,
|
|
||||||
private readonly bool $enabled,
|
private readonly bool $enabled,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
@ -45,42 +43,27 @@ class NotifyVisitToRedis
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$queues = $this->determineQueuesToPublishTo($visit);
|
$updates = $this->determineUpdatesForVisit($visit);
|
||||||
$payload = $this->visitToPayload($visit);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
each($queues, fn (string $queue) => $this->redisHelper->publishUpdate(
|
each($updates, fn (Update $update) => $this->redisHelper->publishUpdate($update));
|
||||||
Update::forTopicAndPayload($queue, $payload),
|
|
||||||
));
|
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
$this->logger->debug('Error while trying to notify Redis pub/sub with new visit. {e}', ['e' => $e]);
|
$this->logger->debug('Error while trying to notify Redis pub/sub with new visit. {e}', ['e' => $e]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string[]
|
* @return Update[]
|
||||||
*/
|
*/
|
||||||
private function determineQueuesToPublishTo(Visit $visit): array
|
private function determineUpdatesForVisit(Visit $visit): array
|
||||||
{
|
{
|
||||||
if ($visit->isOrphan()) {
|
if ($visit->isOrphan()) {
|
||||||
return [Topic::NEW_ORPHAN_VISIT->value];
|
return [$this->updatesGenerator->newOrphanVisitUpdate($visit)];
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
Topic::NEW_VISIT->value,
|
$this->updatesGenerator->newShortUrlVisitUpdate($visit),
|
||||||
Topic::newShortUrlVisit($visit->getShortUrl()?->getShortCode()),
|
$this->updatesGenerator->newVisitUpdate($visit),
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
private function visitToPayload(Visit $visit): array
|
|
||||||
{
|
|
||||||
if ($visit->isOrphan()) {
|
|
||||||
return ['visit' => $this->orphanVisitTransformer->transform($visit)];
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
|
||||||
'visit' => $visit->jsonSerialize(),
|
|
||||||
'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user