diff --git a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php index 6659bc0c..36dd9a60 100644 --- a/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php +++ b/module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php @@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Helper; use Laminas\Diactoros\Uri; use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; +use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use function sprintf; @@ -18,19 +19,20 @@ readonly class ShortUrlStringifier implements ShortUrlStringifierInterface ) { } - public function stringify(ShortUrl $shortUrl): string + public function stringify(ShortUrl|ShortUrlIdentifier $shortUrl): string { + $shortUrlIdentifier = $shortUrl instanceof ShortUrl ? ShortUrlIdentifier::fromShortUrl($shortUrl) : $shortUrl; $uriWithoutShortCode = (new Uri())->withScheme($this->urlShortenerOptions->schema) - ->withHost($this->resolveDomain($shortUrl)) + ->withHost($this->resolveDomain($shortUrlIdentifier)) ->withPath($this->basePath) ->__toString(); // The short code needs to be appended to avoid it from being URL-encoded - return sprintf('%s/%s', $uriWithoutShortCode, $shortUrl->getShortCode()); + return sprintf('%s/%s', $uriWithoutShortCode, $shortUrlIdentifier->shortCode); } - private function resolveDomain(ShortUrl $shortUrl): string + private function resolveDomain(ShortUrlIdentifier $shortUrlIdentifier): string { - return $shortUrl->getDomain()?->authority ?? $this->urlShortenerOptions->defaultDomain; + return $shortUrlIdentifier->domain ?? $this->urlShortenerOptions->defaultDomain; } } diff --git a/module/Core/src/ShortUrl/Helper/ShortUrlStringifierInterface.php b/module/Core/src/ShortUrl/Helper/ShortUrlStringifierInterface.php index 0505a694..0a6f6975 100644 --- a/module/Core/src/ShortUrl/Helper/ShortUrlStringifierInterface.php +++ b/module/Core/src/ShortUrl/Helper/ShortUrlStringifierInterface.php @@ -5,8 +5,9 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\ShortUrl\Helper; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; +use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; interface ShortUrlStringifierInterface { - public function stringify(ShortUrl $shortUrl): string; + public function stringify(ShortUrl|ShortUrlIdentifier $shortUrl): string; } diff --git a/module/Core/src/ShortUrl/Model/ShortUrlIdentifier.php b/module/Core/src/ShortUrl/Model/ShortUrlIdentifier.php index ff44ed7f..9b3014f8 100644 --- a/module/Core/src/ShortUrl/Model/ShortUrlIdentifier.php +++ b/module/Core/src/ShortUrl/Model/ShortUrlIdentifier.php @@ -33,10 +33,8 @@ final readonly class ShortUrlIdentifier public static function fromShortUrl(ShortUrl $shortUrl): self { - $domain = $shortUrl->getDomain(); - $domainAuthority = $domain?->authority; - - return new self($shortUrl->getShortCode(), $domainAuthority); + $domain = $shortUrl->getDomain()?->authority; + return new self($shortUrl->getShortCode(), $domain); } public static function fromShortCodeAndDomain(string $shortCode, string|null $domain = null): self diff --git a/module/Core/src/ShortUrl/Model/ShortUrlWithVisitsSummary.php b/module/Core/src/ShortUrl/Model/ShortUrlWithVisitsSummary.php index d5c34b8b..6b824cf8 100644 --- a/module/Core/src/ShortUrl/Model/ShortUrlWithVisitsSummary.php +++ b/module/Core/src/ShortUrl/Model/ShortUrlWithVisitsSummary.php @@ -11,8 +11,8 @@ final readonly class ShortUrlWithVisitsSummary { private function __construct( public ShortUrl $shortUrl, + private string|null $authority, private VisitsSummary|null $visitsSummary = null, - private string|null $authority = null, ) { } @@ -23,17 +23,22 @@ final readonly class ShortUrlWithVisitsSummary { return new self( shortUrl: $data['shortUrl'], + authority: $data['authority'] ?? null, visitsSummary: VisitsSummary::fromTotalAndNonBots( total: (int) $data['visits'], nonBots: (int) $data['nonBotVisits'], ), - authority: $data['authority'] ?? null, ); } public static function fromShortUrl(ShortUrl $shortUrl): self { - return new self($shortUrl); + return new self($shortUrl, authority: $shortUrl->getDomain()?->authority); + } + + public function toIdentifier(): ShortUrlIdentifier + { + return ShortUrlIdentifier::fromShortCodeAndDomain($this->shortUrl->getShortCode(), $this->authority); } public function toArray(): array diff --git a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php index d2bdb73a..d19262e1 100644 --- a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php +++ b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformer.php @@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Transformer; use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface; +use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary; readonly class ShortUrlDataTransformer implements ShortUrlDataTransformerInterface @@ -14,12 +15,14 @@ readonly class ShortUrlDataTransformer implements ShortUrlDataTransformerInterfa { } - public function transform(ShortUrlWithVisitsSummary|ShortUrl $data): array + public function transform(ShortUrlWithVisitsSummary|ShortUrl $shortUrl): array { - $shortUrl = $data instanceof ShortUrlWithVisitsSummary ? $data->shortUrl : $data; + $shortUrlIdentifier = $shortUrl instanceof ShortUrl + ? ShortUrlIdentifier::fromShortUrl($shortUrl) + : $shortUrl->toIdentifier(); return [ - 'shortUrl' => $this->stringifier->stringify($shortUrl), - ...$data->toArray(), + 'shortUrl' => $this->stringifier->stringify($shortUrlIdentifier), + ...$shortUrl->toArray(), ]; } } diff --git a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformerInterface.php b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformerInterface.php index e1101f70..b3103c80 100644 --- a/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformerInterface.php +++ b/module/Core/src/ShortUrl/Transformer/ShortUrlDataTransformerInterface.php @@ -9,5 +9,5 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary; interface ShortUrlDataTransformerInterface { - public function transform(ShortUrlWithVisitsSummary|ShortUrl $data): array; + public function transform(ShortUrlWithVisitsSummary|ShortUrl $shortUrl): array; }