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;
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;
use Shlinkio\Shlink\Core\EventDispatcher\Async\AbstractNotifyNewShortUrlListener;
class NotifyNewShortUrlToMercure
class NotifyNewShortUrlToMercure extends 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
protected function isEnabled(): bool
{
$shortUrlId = $shortUrlCreated->shortUrlId;
$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;
return true;
}
try {
$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]);
}
protected function getRemoteSystemName(): string
{
return 'Mercure';
}
}

View file

@ -7,44 +7,29 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RabbitMq;
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\Async\AbstractNotifyNewShortUrlListener;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
use Throwable;
class NotifyNewShortUrlToRabbitMq
class NotifyNewShortUrlToRabbitMq extends AbstractNotifyNewShortUrlListener
{
public function __construct(
private readonly PublishingHelperInterface $rabbitMqHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
PublishingHelperInterface $rabbitMqHelper,
PublishingUpdatesGeneratorInterface $updatesGenerator,
EntityManagerInterface $em,
LoggerInterface $logger,
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;
return $this->options->isEnabled();
}
$shortUrlId = $shortUrlCreated->shortUrlId;
$shortUrl = $this->em->find(ShortUrl::class, $shortUrlId);
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]);
}
protected function getRemoteSystemName(): string
{
return 'RabbitMQ';
}
}

View file

@ -7,43 +7,28 @@ namespace Shlinkio\Shlink\Core\EventDispatcher\RedisPubSub;
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\Async\AbstractNotifyNewShortUrlListener;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
class NotifyNewShortUrlToRedis
class NotifyNewShortUrlToRedis extends AbstractNotifyNewShortUrlListener
{
public function __construct(
private readonly PublishingHelperInterface $redisHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
PublishingHelperInterface $redisHelper,
PublishingUpdatesGeneratorInterface $updatesGenerator,
EntityManagerInterface $em,
LoggerInterface $logger,
private readonly bool $enabled,
) {
parent::__construct($redisHelper, $updatesGenerator, $em, $logger);
}
public function __invoke(ShortUrlCreated $shortUrlCreated): void
protected function isEnabled(): bool
{
if (! $this->enabled) {
return;
return $this->enabled;
}
$shortUrlId = $shortUrlCreated->shortUrlId;
$shortUrl = $this->em->find(ShortUrl::class, $shortUrlId);
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]);
}
protected function getRemoteSystemName(): string
{
return 'Redis pub/sub';
}
}

View file

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

View file

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