mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 01:03:13 +08:00
Moved duplicated code in visit listeners to an abstract class
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user