From 08950f64338581e40997661b2d487a19ae8d44c6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 24 Jun 2020 20:21:05 +0200 Subject: [PATCH] Replaced UriInterface by string when creating a short URL --- .../ShortUrl/GenerateShortUrlCommand.php | 23 ++++++++----------- module/Core/src/Model/CreateShortUrlData.php | 19 ++++----------- module/Core/src/Service/UrlShortener.php | 5 +--- .../src/Service/UrlShortenerInterface.php | 3 +-- .../Action/ShortUrl/CreateShortUrlAction.php | 3 +-- .../SingleStepCreateShortUrlAction.php | 2 +- 6 files changed, 18 insertions(+), 37 deletions(-) diff --git a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php index 7369f1f6..06cdd274 100644 --- a/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php +++ b/module/CLI/src/Command/ShortUrl/GenerateShortUrlCommand.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\ShortUrl; -use Laminas\Diactoros\Uri; use Shlinkio\Shlink\CLI\Util\ExitCodes; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; @@ -128,19 +127,15 @@ class GenerateShortUrlCommand extends Command $shortCodeLength = $input->getOption('shortCodeLength') ?? $this->defaultShortCodeLength; try { - $shortUrl = $this->urlShortener->urlToShortCode( - new Uri($longUrl), - $tags, - ShortUrlMeta::fromRawData([ - ShortUrlMetaInputFilter::VALID_SINCE => $input->getOption('validSince'), - ShortUrlMetaInputFilter::VALID_UNTIL => $input->getOption('validUntil'), - ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug, - ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits !== null ? (int) $maxVisits : null, - ShortUrlMetaInputFilter::FIND_IF_EXISTS => $input->getOption('findIfExists'), - ShortUrlMetaInputFilter::DOMAIN => $input->getOption('domain'), - ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $shortCodeLength, - ]), - ); + $shortUrl = $this->urlShortener->urlToShortCode($longUrl, $tags, ShortUrlMeta::fromRawData([ + ShortUrlMetaInputFilter::VALID_SINCE => $input->getOption('validSince'), + ShortUrlMetaInputFilter::VALID_UNTIL => $input->getOption('validUntil'), + ShortUrlMetaInputFilter::CUSTOM_SLUG => $customSlug, + ShortUrlMetaInputFilter::MAX_VISITS => $maxVisits !== null ? (int) $maxVisits : null, + ShortUrlMetaInputFilter::FIND_IF_EXISTS => $input->getOption('findIfExists'), + ShortUrlMetaInputFilter::DOMAIN => $input->getOption('domain'), + ShortUrlMetaInputFilter::SHORT_CODE_LENGTH => $shortCodeLength, + ])); $io->writeln([ sprintf('Processed long URL: %s', $longUrl), diff --git a/module/Core/src/Model/CreateShortUrlData.php b/module/Core/src/Model/CreateShortUrlData.php index 24ed90a6..9b64302d 100644 --- a/module/Core/src/Model/CreateShortUrlData.php +++ b/module/Core/src/Model/CreateShortUrlData.php @@ -4,41 +4,32 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Model; -use Psr\Http\Message\UriInterface; - final class CreateShortUrlData { - private UriInterface $longUrl; + private string $longUrl; private array $tags; private ShortUrlMeta $meta; - public function __construct( - UriInterface $longUrl, - array $tags = [], - ?ShortUrlMeta $meta = null - ) { + public function __construct(string $longUrl, array $tags = [], ?ShortUrlMeta $meta = null) + { $this->longUrl = $longUrl; $this->tags = $tags; $this->meta = $meta ?? ShortUrlMeta::createEmpty(); } - /** - */ - public function getLongUrl(): UriInterface + public function getLongUrl(): string { return $this->longUrl; } /** - * @return array + * @return string[] */ public function getTags(): array { return $this->tags; } - /** - */ public function getMeta(): ShortUrlMeta { return $this->meta; diff --git a/module/Core/src/Service/UrlShortener.php b/module/Core/src/Service/UrlShortener.php index 4544bfc0..7892f959 100644 --- a/module/Core/src/Service/UrlShortener.php +++ b/module/Core/src/Service/UrlShortener.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Service; use Doctrine\ORM\EntityManagerInterface; -use Psr\Http\Message\UriInterface; use Shlinkio\Shlink\Core\Domain\Resolver\DomainResolverInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; @@ -42,10 +41,8 @@ class UrlShortener implements UrlShortenerInterface * @throws InvalidUrlException * @throws Throwable */ - public function urlToShortCode(UriInterface $url, array $tags, ShortUrlMeta $meta): ShortUrl + public function urlToShortCode(string $url, array $tags, ShortUrlMeta $meta): ShortUrl { - $url = (string) $url; - // First, check if a short URL exists for all provided params $existingShortUrl = $this->findExistingShortUrlIfExists($url, $tags, $meta); if ($existingShortUrl !== null) { diff --git a/module/Core/src/Service/UrlShortenerInterface.php b/module/Core/src/Service/UrlShortenerInterface.php index 802eb048..e26530ca 100644 --- a/module/Core/src/Service/UrlShortenerInterface.php +++ b/module/Core/src/Service/UrlShortenerInterface.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Service; -use Psr\Http\Message\UriInterface; use Shlinkio\Shlink\Core\Entity\ShortUrl; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; @@ -17,5 +16,5 @@ interface UrlShortenerInterface * @throws NonUniqueSlugException * @throws InvalidUrlException */ - public function urlToShortCode(UriInterface $url, array $tags, ShortUrlMeta $meta): ShortUrl; + public function urlToShortCode(string $url, array $tags, ShortUrlMeta $meta): ShortUrl; } diff --git a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php index 489d1277..97097808 100644 --- a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Rest\Action\ShortUrl; -use Laminas\Diactoros\Uri; use Psr\Http\Message\ServerRequestInterface as Request; use Shlinkio\Shlink\Core\Exception\ValidationException; use Shlinkio\Shlink\Core\Model\CreateShortUrlData; @@ -28,6 +27,6 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction } $meta = ShortUrlMeta::fromRawData($postData); - return new CreateShortUrlData(new Uri($postData['longUrl']), (array) ($postData['tags'] ?? []), $meta); + return new CreateShortUrlData($postData['longUrl'], (array) ($postData['tags'] ?? []), $meta); } } diff --git a/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php index daeb3d04..0a6a45d4 100644 --- a/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php @@ -46,6 +46,6 @@ class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction ]); } - return new CreateShortUrlData(new Uri($query['longUrl'])); + return new CreateShortUrlData($query['longUrl']); } }