Added support for legacy and new publishing of visits in RabbitMQ

This commit is contained in:
Alejandro Celaya
2022-07-25 12:08:22 +02:00
parent 53b937be63
commit 6ce2049935
9 changed files with 70 additions and 18 deletions

View File

@@ -33,6 +33,8 @@ enum EnvVars: string
case RABBITMQ_USER = 'RABBITMQ_USER';
case RABBITMQ_PASSWORD = 'RABBITMQ_PASSWORD';
case RABBITMQ_VHOST = 'RABBITMQ_VHOST';
/** @deprecated */
case RABBITMQ_LEGACY_VISITS_PUBLISHING = 'RABBITMQ_LEGACY_VISITS_PUBLISHING';
case DEFAULT_INVALID_SHORT_URL_REDIRECT = 'DEFAULT_INVALID_SHORT_URL_REDIRECT';
case DEFAULT_REGULAR_404_REDIRECT = 'DEFAULT_REGULAR_404_REDIRECT';
case DEFAULT_BASE_URL_REDIRECT = 'DEFAULT_BASE_URL_REDIRECT';

View File

@@ -11,6 +11,7 @@ use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\EventDispatcher\Event\ShortUrlCreated;
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
use Throwable;
class NotifyNewShortUrlToRabbitMq
@@ -20,13 +21,13 @@ class NotifyNewShortUrlToRabbitMq
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
private readonly DataTransformerInterface $shortUrlTransformer,
private readonly bool $isEnabled,
private readonly RabbitMqOptions $options,
) {
}
public function __invoke(ShortUrlCreated $shortUrlCreated): void
{
if (! $this->isEnabled) {
if (! $this->options->isEnabled()) {
return;
}

View File

@@ -11,6 +11,7 @@ use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\EventDispatcher\Event\VisitLocated;
use Shlinkio\Shlink\Core\EventDispatcher\Topic;
use Shlinkio\Shlink\Core\Options\RabbitMqOptions;
use Throwable;
class NotifyVisitToRabbitMq
@@ -20,14 +21,14 @@ class NotifyVisitToRabbitMq
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger,
private readonly DataTransformerInterface $orphanVisitTransformer,
private readonly DataTransformerInterface $shortUrlTransformer, // @phpstan-ignore-line
private readonly bool $isEnabled,
private readonly DataTransformerInterface $shortUrlTransformer,
private readonly RabbitMqOptions $options,
) {
}
public function __invoke(VisitLocated $shortUrlLocated): void
{
if (! $this->isEnabled) {
if (! $this->options->isEnabled()) {
return;
}
@@ -70,14 +71,15 @@ class NotifyVisitToRabbitMq
private function visitToPayload(Visit $visit): array
{
// FIXME This was defined incorrectly.
// According to the spec, both the visit and the short URL it belongs to, should be published.
// The shape should be ['visit' => [...], 'shortUrl' => ?[...]]
// However, this would be a breaking change, so we need a flag that determines the shape of the payload.
// This was defined incorrectly.
// According to the spec, both the visit and the short URL it belongs to, should be published.
// The shape should be ['visit' => [...], 'shortUrl' => ?[...]]
// However, this would be a breaking change, so we need a flag that determines the shape of the payload.
if ($this->options->legacyVisitsPublishing()) {
return ! $visit->isOrphan() ? $visit->jsonSerialize() : $this->orphanVisitTransformer->transform($visit);
}
return ! $visit->isOrphan() ? $visit->jsonSerialize() : $this->orphanVisitTransformer->transform($visit);
if ($visit->isOrphan()) { // @phpstan-ignore-line
if ($visit->isOrphan()) {
return ['visit' => $this->orphanVisitTransformer->transform($visit)];
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Core\Options;
use Laminas\Stdlib\AbstractOptions;
class RabbitMqOptions extends AbstractOptions
{
protected $__strictMode__ = false; // phpcs:ignore
private bool $enabled = false;
/** @deprecated */
private bool $legacyVisitsPublishing = false;
public function isEnabled(): bool
{
return $this->enabled;
}
protected function setEnabled(bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
/** @deprecated */
public function legacyVisitsPublishing(): bool
{
return $this->legacyVisitsPublishing;
}
/** @deprecated */
protected function setLegacyVisitsPublishing(bool $legacyVisitsPublishing): self
{
$this->legacyVisitsPublishing = $legacyVisitsPublishing;
return $this;
}
}