mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-12 01:54:41 +08:00
Extracted logic to stringify ShortUrls to its own service
This commit is contained in:
36
module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php
Normal file
36
module/Core/src/ShortUrl/Helper/ShortUrlStringifier.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
||||
|
||||
use Laminas\Diactoros\Uri;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
|
||||
class ShortUrlStringifier implements ShortUrlStringifierInterface
|
||||
{
|
||||
private array $domainConfig;
|
||||
|
||||
public function __construct(array $domainConfig)
|
||||
{
|
||||
$this->domainConfig = $domainConfig;
|
||||
}
|
||||
|
||||
public function stringify(ShortUrl $shortUrl): string
|
||||
{
|
||||
return (new Uri())->withPath($shortUrl->getShortCode())
|
||||
->withScheme($this->domainConfig['schema'] ?? 'http')
|
||||
->withHost($this->resolveDomain($shortUrl))
|
||||
->__toString();
|
||||
}
|
||||
|
||||
private function resolveDomain(ShortUrl $shortUrl): string
|
||||
{
|
||||
$domain = $shortUrl->getDomain();
|
||||
if ($domain === null) {
|
||||
return $this->domainConfig['hostname'] ?? '';
|
||||
}
|
||||
|
||||
return $domain->getAuthority();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
|
||||
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
|
||||
interface ShortUrlStringifierInterface
|
||||
{
|
||||
public function stringify(ShortUrl $shortUrl): string;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\ShortUrl\Transformer;
|
||||
|
||||
use Shlinkio\Shlink\Common\Rest\DataTransformerInterface;
|
||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
||||
|
||||
use function Functional\invoke;
|
||||
use function Functional\invoke_if;
|
||||
|
||||
class ShortUrlDataTransformer implements DataTransformerInterface
|
||||
{
|
||||
private ShortUrlStringifierInterface $stringifier;
|
||||
|
||||
public function __construct(ShortUrlStringifierInterface $stringifier)
|
||||
{
|
||||
$this->stringifier = $stringifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ShortUrl $shortUrl
|
||||
*/
|
||||
public function transform($shortUrl): array // phpcs:ignore
|
||||
{
|
||||
return [
|
||||
'shortCode' => $shortUrl->getShortCode(),
|
||||
'shortUrl' => $this->stringifier->stringify($shortUrl),
|
||||
'longUrl' => $shortUrl->getLongUrl(),
|
||||
'dateCreated' => $shortUrl->getDateCreated()->toAtomString(),
|
||||
'visitsCount' => $shortUrl->getVisitsCount(),
|
||||
'tags' => invoke($shortUrl->getTags(), '__toString'),
|
||||
'meta' => $this->buildMeta($shortUrl),
|
||||
'domain' => $shortUrl->getDomain(),
|
||||
];
|
||||
}
|
||||
|
||||
private function buildMeta(ShortUrl $shortUrl): array
|
||||
{
|
||||
$validSince = $shortUrl->getValidSince();
|
||||
$validUntil = $shortUrl->getValidUntil();
|
||||
$maxVisits = $shortUrl->getMaxVisits();
|
||||
|
||||
return [
|
||||
'validSince' => invoke_if($validSince, 'toAtomString'),
|
||||
'validUntil' => invoke_if($validUntil, 'toAtomString'),
|
||||
'maxVisits' => $maxVisits,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user