mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Avoid selecting domains for every short URL in list
This commit is contained in:
@@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
|||||||
use Laminas\Diactoros\Uri;
|
use Laminas\Diactoros\Uri;
|
||||||
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
||||||
|
|
||||||
use function sprintf;
|
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)
|
$uriWithoutShortCode = (new Uri())->withScheme($this->urlShortenerOptions->schema)
|
||||||
->withHost($this->resolveDomain($shortUrl))
|
->withHost($this->resolveDomain($shortUrlIdentifier))
|
||||||
->withPath($this->basePath)
|
->withPath($this->basePath)
|
||||||
->__toString();
|
->__toString();
|
||||||
|
|
||||||
// The short code needs to be appended to avoid it from being URL-encoded
|
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ declare(strict_types=1);
|
|||||||
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
||||||
|
|
||||||
interface ShortUrlStringifierInterface
|
interface ShortUrlStringifierInterface
|
||||||
{
|
{
|
||||||
public function stringify(ShortUrl $shortUrl): string;
|
public function stringify(ShortUrl|ShortUrlIdentifier $shortUrl): string;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,10 +33,8 @@ final readonly class ShortUrlIdentifier
|
|||||||
|
|
||||||
public static function fromShortUrl(ShortUrl $shortUrl): self
|
public static function fromShortUrl(ShortUrl $shortUrl): self
|
||||||
{
|
{
|
||||||
$domain = $shortUrl->getDomain();
|
$domain = $shortUrl->getDomain()?->authority;
|
||||||
$domainAuthority = $domain?->authority;
|
return new self($shortUrl->getShortCode(), $domain);
|
||||||
|
|
||||||
return new self($shortUrl->getShortCode(), $domainAuthority);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromShortCodeAndDomain(string $shortCode, string|null $domain = null): self
|
public static function fromShortCodeAndDomain(string $shortCode, string|null $domain = null): self
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ final readonly class ShortUrlWithVisitsSummary
|
|||||||
{
|
{
|
||||||
private function __construct(
|
private function __construct(
|
||||||
public ShortUrl $shortUrl,
|
public ShortUrl $shortUrl,
|
||||||
|
private string|null $authority,
|
||||||
private VisitsSummary|null $visitsSummary = null,
|
private VisitsSummary|null $visitsSummary = null,
|
||||||
private string|null $authority = null,
|
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,17 +23,22 @@ final readonly class ShortUrlWithVisitsSummary
|
|||||||
{
|
{
|
||||||
return new self(
|
return new self(
|
||||||
shortUrl: $data['shortUrl'],
|
shortUrl: $data['shortUrl'],
|
||||||
|
authority: $data['authority'] ?? null,
|
||||||
visitsSummary: VisitsSummary::fromTotalAndNonBots(
|
visitsSummary: VisitsSummary::fromTotalAndNonBots(
|
||||||
total: (int) $data['visits'],
|
total: (int) $data['visits'],
|
||||||
nonBots: (int) $data['nonBotVisits'],
|
nonBots: (int) $data['nonBotVisits'],
|
||||||
),
|
),
|
||||||
authority: $data['authority'] ?? null,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromShortUrl(ShortUrl $shortUrl): self
|
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
|
public function toArray(): array
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Transformer;
|
|||||||
|
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
||||||
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
||||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
|
||||||
|
|
||||||
readonly class ShortUrlDataTransformer implements ShortUrlDataTransformerInterface
|
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 [
|
return [
|
||||||
'shortUrl' => $this->stringifier->stringify($shortUrl),
|
'shortUrl' => $this->stringifier->stringify($shortUrlIdentifier),
|
||||||
...$data->toArray(),
|
...$shortUrl->toArray(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,5 +9,5 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
|
|||||||
|
|
||||||
interface ShortUrlDataTransformerInterface
|
interface ShortUrlDataTransformerInterface
|
||||||
{
|
{
|
||||||
public function transform(ShortUrlWithVisitsSummary|ShortUrl $data): array;
|
public function transform(ShortUrlWithVisitsSummary|ShortUrl $shortUrl): array;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user