Moved duplicated code in visit listeners to an abstract class

This commit is contained in:
Alejandro Celaya
2022-07-27 18:18:36 +02:00
parent 26037327f9
commit e36c4d397c
8 changed files with 133 additions and 155 deletions

View File

@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher\Async;
abstract class AbstractAsyncListener
{
abstract protected function isEnabled(): bool;
abstract protected function getRemoteSystemName(): string;
}

View File

@@ -12,10 +12,10 @@ use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
abstract class AbstractNotifyNewShortUrlListener
abstract class AbstractNotifyNewShortUrlListener extends AbstractAsyncListener
{
public function __construct(
private readonly PublishingHelperInterface $mercureHelper,
private readonly PublishingHelperInterface $publishingHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
@@ -41,7 +41,7 @@ abstract class AbstractNotifyNewShortUrlListener
}
try {
$this->mercureHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl));
$this->publishingHelper->publishUpdate($this->updatesGenerator->newShortUrlUpdate($shortUrl));
} catch (Throwable $e) {
$this->logger->debug(
'Error while trying to notify {name} with new short URL. {e}',
@@ -49,8 +49,4 @@ abstract class AbstractNotifyNewShortUrlListener
);
}
}
abstract protected function isEnabled(): bool;
abstract protected function getRemoteSystemName(): string;
}

View File

@@ -0,0 +1,72 @@
<?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\Common\UpdatePublishing\Update;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
use function Functional\each;
abstract class AbstractNotifyVisitListener extends AbstractAsyncListener
{
public function __construct(
private readonly PublishingHelperInterface $publishingHelper,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
) {
}
public function __invoke(VisitLocated $visitLocated): void
{
if (! $this->isEnabled()) {
return;
}
$visitId = $visitLocated->visitId;
$visit = $this->em->find(Visit::class, $visitId);
$name = $this->getRemoteSystemName();
if ($visit === null) {
$this->logger->warning(
'Tried to notify {name} for visit with id "{visitId}", but it does not exist.',
['visitId' => $visitId, 'name' => $name],
);
return;
}
$updates = $this->determineUpdatesForVisit($visit);
try {
each($updates, fn (Update $update) => $this->publishingHelper->publishUpdate($update));
} catch (Throwable $e) {
$this->logger->debug(
'Error while trying to notify {name} with new visit. {e}',
['e' => $e, 'name' => $name],
);
}
}
/**
* @return Update[]
*/
protected function determineUpdatesForVisit(Visit $visit): array
{
if ($visit->isOrphan()) {
return [$this->updatesGenerator->newOrphanVisitUpdate($visit)];
}
return [
$this->updatesGenerator->newShortUrlVisitUpdate($visit),
$this->updatesGenerator->newVisitUpdate($visit),
];
}
}