Renamed MercureUpdatesGenerator to PublishingUpdatesGenerator to make it general purpose

This commit is contained in:
Alejandro Celaya
2022-07-27 09:38:47 +02:00
parent d3add6d8e4
commit 7e8109caa3
10 changed files with 21 additions and 24 deletions

View File

@@ -9,14 +9,14 @@ 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\Mercure\MercureUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
class NotifyNewShortUrlToMercure
{
public function __construct(
private readonly PublishingHelperInterface $mercureHelper,
private readonly MercureUpdatesGeneratorInterface $updatesGenerator,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
) {

View File

@@ -10,7 +10,7 @@ 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\Mercure\MercureUpdatesGeneratorInterface;
use Shlinkio\Shlink\Core\EventDispatcher\PublishingUpdatesGeneratorInterface;
use Throwable;
use function Functional\each;
@@ -19,7 +19,7 @@ class NotifyVisitToMercure
{
public function __construct(
private readonly PublishingHelperInterface $mercureHelper,
private readonly MercureUpdatesGeneratorInterface $updatesGenerator,
private readonly PublishingUpdatesGeneratorInterface $updatesGenerator,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
) {

View File

@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher;
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Common\UpdatePublishing\Update;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
final class PublishingUpdatesGenerator implements PublishingUpdatesGeneratorInterface
{
public function __construct(
private readonly DataTransformerInterface $shortUrlTransformer,
private readonly DataTransformerInterface $orphanVisitTransformer,
) {
}
public function newVisitUpdate(Visit $visit): Update
{
return Update::forTopicAndPayload(Topic::NEW_VISIT->value, [
'shortUrl' => $this->shortUrlTransformer->transform($visit->getShortUrl()),
'visit' => $visit->jsonSerialize(),
]);
}
public function newOrphanVisitUpdate(Visit $visit): Update
{
return Update::forTopicAndPayload(Topic::NEW_ORPHAN_VISIT->value, [
'visit' => $this->orphanVisitTransformer->transform($visit),
]);
}
public function newShortUrlVisitUpdate(Visit $visit): Update
{
$shortUrl = $visit->getShortUrl();
$topic = Topic::newShortUrlVisit($shortUrl?->getShortCode());
return Update::forTopicAndPayload($topic, [
'shortUrl' => $this->shortUrlTransformer->transform($shortUrl),
'visit' => $visit->jsonSerialize(),
]);
}
public function newShortUrlUpdate(ShortUrl $shortUrl): Update
{
return Update::forTopicAndPayload(Topic::NEW_SHORT_URL->value, [
'shortUrl' => $this->shortUrlTransformer->transform($shortUrl),
]);
}
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\EventDispatcher;
use Shlinkio\Shlink\Common\UpdatePublishing\Update;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Entity\Visit;
interface PublishingUpdatesGeneratorInterface
{
public function newVisitUpdate(Visit $visit): Update;
public function newOrphanVisitUpdate(Visit $visit): Update;
public function newShortUrlVisitUpdate(Visit $visit): Update;
public function newShortUrlUpdate(ShortUrl $shortUrl): Update;
}