Moved duplicated code in short URL listeners to an abstract class

This commit is contained in:
Alejandro Celaya 2022-07-27 18:06:47 +02:00
parent da6aa1d697
commit 26037327f9
6 changed files with 97 additions and 95 deletions

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher\Async;
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\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
abstract class AbstractNotifyNewShortUrlListener
{
public function __construct(
private readonly PublishingHelperInterface $mercureHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
) {
}
public function __invoke(ShortUrlCreated $shortUrlCreated): void
{
if (! $this->isEnabled()) {
return;
}
$shortUrlId = $shortUrlCreated->shortUrlId;
$shortUrl = $this->em->find(ShortUrl::class, $shortUrlId);
$name = $this->getRemoteSystemName();
if ($shortUrl === null) {
$this->logger->warning(
'Tried to notify {name} for new short URL with id "{shortUrlId}", but it does not exist.',
['shortUrlId' => $shortUrlId, 'name' => $name],
);
return;
}
try {
$this->mercureHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl));
} catch (Throwable $e) {
$this->logger->debug(
'Error while trying to notify {name} with new short URL. {e}',
['e' => $e, 'name' => $name],
);
}
}
abstract protected function isEnabled(): bool;
abstract protected function getRemoteSystemName(): string;
}

View file

@ -4,41 +4,17 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher\Mercure; namespace Shlinkio\Shlink\Core\EventDispatcher\Mercure;
use Doctrine\ORM\EntityManagerInterface; use Shlinkio\Shlink\Core\EventDispatcher\Async\AbstractNotifyNewShortUrlListener;
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\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
class NotifyNewShortUrlToMercure class NotifyNewShortUrlToMercure extends AbstractNotifyNewShortUrlListener
{ {
public function __construct( protected function isEnabled(): bool
private readonly PublishingHelperInterface $mercureHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
) {
}
public function __invoke(ShortUrlCreated $shortUrlCreated): void
{ {
$shortUrlId = $shortUrlCreated->shortUrlId; return true;
$shortUrl = $this->em->find(ShortUrl::class, $shortUrlId);
if ($shortUrl === null) {
$this->logger->warning(
'Tried to notify Mercure for new short URL with id "{shortUrlId}", but it does not exist.',
['shortUrlId' => $shortUrlId],
);
return;
} }
try { protected function getRemoteSystemName(): string
$this->mercureHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl)); {
} catch (Throwable $e) { return 'Mercure';
$this->logger->debug('Error while trying to notify mercure hub with new short URL. {e}', ['e' => $e]);
}
} }
} }

View file

@ -7,44 +7,29 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RabbitMq;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface; use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\EventDispatcher\Async\AbstractNotifyNewShortUrlListener;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface; use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\Options\RabbitMqOptions; use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
use Throwable;
class NotifyNewShortUrlToRabbitMq class NotifyNewShortUrlToRabbitMq extends AbstractNotifyNewShortUrlListener
{ {
public function __construct( public function __construct(
private readonly PublishingHelperInterface $rabbitMqHelper, PublishingHelperInterface $rabbitMqHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator, PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em, EntityManagerInterface $em,
private readonly LoggerInterface $logger, LoggerInterface $logger,
private readonly RabbitMqOptions $options, private readonly RabbitMqOptions $options,
) { ) {
parent::__construct($rabbitMqHelper, $updatesGenerator, $em, $logger);
} }
public function __invoke(ShortUrlCreated $shortUrlCreated): void protected function isEnabled(): bool
{ {
if (! $this->options->isEnabled()) { return $this->options->isEnabled();
return;
} }
$shortUrlId = $shortUrlCreated->shortUrlId; protected function getRemoteSystemName(): string
$shortUrl = $this->em->find(ShortUrl::class, $shortUrlId); {
return 'RabbitMQ';
if ($shortUrl === null) {
$this->logger->warning(
'Tried to notify RabbitMQ for new short URL with id "{shortUrlId}", but it does not exist.',
['shortUrlId' => $shortUrlId],
);
return;
}
try {
$this->rabbitMqHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl));
} catch (Throwable $e) {
$this->logger->debug('Error while trying to notify RabbitMQ with new short URL. {e}', ['e' => $e]);
}
} }
} }

View file

@ -7,43 +7,28 @@ 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\UpdatePublishing\PublishingHelperInterface; use Shlinkio\Shlink\Common\UpdatePublishing\PublishingHelperInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\EventDispatcher\Async\AbstractNotifyNewShortUrlListener;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface; use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
class NotifyNewShortUrlToRedis class NotifyNewShortUrlToRedis extends AbstractNotifyNewShortUrlListener
{ {
public function __construct( public function __construct(
private readonly PublishingHelperInterface $redisHelper, PublishingHelperInterface $redisHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator, PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em, EntityManagerInterface $em,
private readonly LoggerInterface $logger, LoggerInterface $logger,
private readonly bool $enabled, private readonly bool $enabled,
) { ) {
parent::__construct($redisHelper, $updatesGenerator, $em, $logger);
} }
public function __invoke(ShortUrlCreated $shortUrlCreated): void protected function isEnabled(): bool
{ {
if (! $this->enabled) { return $this->enabled;
return;
} }
$shortUrlId = $shortUrlCreated->shortUrlId; protected function getRemoteSystemName(): string
$shortUrl = $this->em->find(ShortUrl::class, $shortUrlId); {
return 'Redis pub/sub';
if ($shortUrl === null) {
$this->logger->warning(
'Tried to notify Redis pub/sub for new short URL with id "{shortUrlId}", but it does not exist.',
['shortUrlId' => $shortUrlId],
);
return;
}
try {
$this->redisHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl));
} catch (Throwable $e) {
$this->logger->debug('Error while trying to notify Redis pub/sub with new short URL. {e}', ['e' => $e]);
}
} }
} }

View file

@ -52,8 +52,8 @@ class NotifyNewShortUrlToMercureTest extends TestCase
$find->shouldHaveBeenCalledOnce(); $find->shouldHaveBeenCalledOnce();
$this->logger->warning( $this->logger->warning(
'Tried to notify Mercure for new short URL with id "{shortUrlId}", but it does not exist.', 'Tried to notify {name} for new short URL with id "{shortUrlId}", but it does not exist.',
['shortUrlId' => '123'], ['shortUrlId' => '123', 'name' => 'Mercure'],
)->shouldHaveBeenCalledOnce(); )->shouldHaveBeenCalledOnce();
$this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); $this->helper->publishUpdate(Argument::cetera())->shouldNotHaveBeenCalled();
$this->updatesGenerator->newShortUrlUpdate(Argument::cetera())->shouldNotHaveBeenCalled(); $this->updatesGenerator->newShortUrlUpdate(Argument::cetera())->shouldNotHaveBeenCalled();
@ -96,8 +96,8 @@ class NotifyNewShortUrlToMercureTest extends TestCase
$publish->shouldHaveBeenCalledOnce(); $publish->shouldHaveBeenCalledOnce();
$this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled(); $this->logger->warning(Argument::cetera())->shouldNotHaveBeenCalled();
$this->logger->debug( $this->logger->debug(
'Error while trying to notify mercure hub with new short URL. {e}', 'Error while trying to notify {name} with new short URL. {e}',
['e' => $e], ['e' => $e, 'name' => 'Mercure'],
)->shouldHaveBeenCalledOnce(); )->shouldHaveBeenCalledOnce();
} }
} }

View file

@ -70,8 +70,8 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
$shortUrlId = '123'; $shortUrlId = '123';
$find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(null); $find = $this->em->find(ShortUrl::class, $shortUrlId)->willReturn(null);
$logWarning = $this->logger->warning( $logWarning = $this->logger->warning(
'Tried to notify RabbitMQ for new short URL with id "{shortUrlId}", but it does not exist.', 'Tried to notify {name} for new short URL with id "{shortUrlId}", but it does not exist.',
['shortUrlId' => $shortUrlId], ['shortUrlId' => $shortUrlId, 'name' => 'RabbitMQ'],
); );
($this->listener)(new ShortUrlCreated($shortUrlId)); ($this->listener)(new ShortUrlCreated($shortUrlId));
@ -117,8 +117,8 @@ class NotifyNewShortUrlToRabbitMqTest extends TestCase
($this->listener)(new ShortUrlCreated($shortUrlId)); ($this->listener)(new ShortUrlCreated($shortUrlId));
$this->logger->debug( $this->logger->debug(
'Error while trying to notify RabbitMQ with new short URL. {e}', 'Error while trying to notify {name} with new short URL. {e}',
['e' => $e], ['e' => $e, 'name' => 'RabbitMQ'],
)->shouldHaveBeenCalledOnce(); )->shouldHaveBeenCalledOnce();
$find->shouldHaveBeenCalledOnce(); $find->shouldHaveBeenCalledOnce();
$generateUpdate->shouldHaveBeenCalledOnce(); $generateUpdate->shouldHaveBeenCalledOnce();