From 80fe3a73e236c5b910e49a8c456ce0163a2d05bc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 20 Sep 2018 20:38:51 +0200 Subject: [PATCH] More classes renamed and fixes for usage of the short code concept in place of short URL --- ...ortCodeData.php => CreateShortUrlData.php} | 2 +- .../ShortUrl/AbstractCreateShortUrlAction.php | 24 +++++++++---------- .../Action/ShortUrl/CreateShortUrlAction.php | 8 +++---- .../SingleStepCreateShortUrlAction.php | 9 ++++--- .../Rest/src/Action/Visit/GetVisitsAction.php | 4 ++-- module/Rest/src/Util/RestUtils.php | 1 + 6 files changed, 24 insertions(+), 24 deletions(-) rename module/Core/src/Model/{CreateShortCodeData.php => CreateShortUrlData.php} (96%) diff --git a/module/Core/src/Model/CreateShortCodeData.php b/module/Core/src/Model/CreateShortUrlData.php similarity index 96% rename from module/Core/src/Model/CreateShortCodeData.php rename to module/Core/src/Model/CreateShortUrlData.php index 880c32c2..a1d9d018 100644 --- a/module/Core/src/Model/CreateShortCodeData.php +++ b/module/Core/src/Model/CreateShortUrlData.php @@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Core\Model; use Psr\Http\Message\UriInterface; -final class CreateShortCodeData +final class CreateShortUrlData { /** * @var UriInterface diff --git a/module/Rest/src/Action/ShortUrl/AbstractCreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/AbstractCreateShortUrlAction.php index fbf1817a..db5d9818 100644 --- a/module/Rest/src/Action/ShortUrl/AbstractCreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/AbstractCreateShortUrlAction.php @@ -9,7 +9,7 @@ use Psr\Log\LoggerInterface; use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; -use Shlinkio\Shlink\Core\Model\CreateShortCodeData; +use Shlinkio\Shlink\Core\Model\CreateShortUrlData; use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Rest\Action\AbstractRestAction; @@ -52,10 +52,10 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction public function handle(Request $request): Response { try { - $shortCodeData = $this->buildUrlToShortCodeData($request); - $shortCodeMeta = $shortCodeData->getMeta(); - $longUrl = $shortCodeData->getLongUrl(); - $customSlug = $shortCodeMeta->getCustomSlug(); + $shortUrlData = $this->buildShortUrlData($request); + $shortUrlMeta = $shortUrlData->getMeta(); + $longUrl = $shortUrlData->getLongUrl(); + $customSlug = $shortUrlMeta->getCustomSlug(); } catch (InvalidArgumentException $e) { $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); return new JsonResponse([ @@ -67,11 +67,11 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction try { $shortUrl = $this->urlShortener->urlToShortCode( $longUrl, - $shortCodeData->getTags(), - $shortCodeMeta->getValidSince(), - $shortCodeMeta->getValidUntil(), + $shortUrlData->getTags(), + $shortUrlMeta->getValidSince(), + $shortUrlMeta->getValidUntil(), $customSlug, - $shortCodeMeta->getMaxVisits() + $shortUrlMeta->getMaxVisits() ); $transformer = new ShortUrlDataTransformer($this->domainConfig); @@ -95,7 +95,7 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction ), ], self::STATUS_BAD_REQUEST); } catch (\Throwable $e) { - $this->logger->error('Unexpected error creating shortcode.' . PHP_EOL . $e); + $this->logger->error('Unexpected error creating short url.' . PHP_EOL . $e); return new JsonResponse([ 'error' => RestUtils::UNKNOWN_ERROR, 'message' => $this->translator->translate('Unexpected error occurred'), @@ -105,8 +105,8 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction /** * @param Request $request - * @return CreateShortCodeData + * @return CreateShortUrlData * @throws InvalidArgumentException */ - abstract protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData; + abstract protected function buildShortUrlData(Request $request): CreateShortUrlData; } diff --git a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php index 13c92994..696c47b3 100644 --- a/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php @@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl; use Psr\Http\Message\ServerRequestInterface as Request; use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; -use Shlinkio\Shlink\Core\Model\CreateShortCodeData; +use Shlinkio\Shlink\Core\Model\CreateShortUrlData; use Shlinkio\Shlink\Core\Model\ShortUrlMeta; use Shlinkio\Shlink\Rest\Action\ShortUrl\AbstractCreateShortUrlAction; use Zend\Diactoros\Uri; @@ -17,18 +17,18 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction /** * @param Request $request - * @return CreateShortCodeData + * @return CreateShortUrlData * @throws InvalidArgumentException * @throws \InvalidArgumentException */ - protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData + protected function buildShortUrlData(Request $request): CreateShortUrlData { $postData = (array) $request->getParsedBody(); if (! isset($postData['longUrl'])) { throw new InvalidArgumentException($this->translator->translate('A URL was not provided')); } - return new CreateShortCodeData( + return new CreateShortUrlData( new Uri($postData['longUrl']), (array) ($postData['tags'] ?? []), ShortUrlMeta::createFromParams( diff --git a/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php b/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php index 399e6393..38057f5c 100644 --- a/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php +++ b/module/Rest/src/Action/ShortUrl/SingleStepCreateShortUrlAction.php @@ -6,9 +6,8 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Log\LoggerInterface; use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; -use Shlinkio\Shlink\Core\Model\CreateShortCodeData; +use Shlinkio\Shlink\Core\Model\CreateShortUrlData; use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; -use Shlinkio\Shlink\Rest\Action\ShortUrl\AbstractCreateShortUrlAction; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Zend\Diactoros\Uri; use Zend\I18n\Translator\TranslatorInterface; @@ -36,11 +35,11 @@ class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction /** * @param Request $request - * @return CreateShortCodeData + * @return CreateShortUrlData * @throws \InvalidArgumentException * @throws InvalidArgumentException */ - protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData + protected function buildShortUrlData(Request $request): CreateShortUrlData { $query = $request->getQueryParams(); @@ -56,6 +55,6 @@ class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction throw new InvalidArgumentException($this->translator->translate('A URL was not provided')); } - return new CreateShortCodeData(new Uri($query['longUrl'])); + return new CreateShortUrlData(new Uri($query['longUrl'])); } } diff --git a/module/Rest/src/Action/Visit/GetVisitsAction.php b/module/Rest/src/Action/Visit/GetVisitsAction.php index 96010ce0..3d5890cc 100644 --- a/module/Rest/src/Action/Visit/GetVisitsAction.php +++ b/module/Rest/src/Action/Visit/GetVisitsAction.php @@ -16,7 +16,7 @@ use Zend\I18n\Translator\TranslatorInterface; class GetVisitsAction extends AbstractRestAction { - protected const ROUTE_PATH = '/short-codes/{shortCode}/visits'; + protected const ROUTE_PATH = '/short-urls/{shortCode}/visits'; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; /** @@ -58,7 +58,7 @@ class GetVisitsAction extends AbstractRestAction ], ]); } catch (InvalidArgumentException $e) { - $this->logger->warning('Provided nonexistent shortcode' . PHP_EOL . $e); + $this->logger->warning('Provided nonexistent short code' . PHP_EOL . $e); return new JsonResponse([ 'error' => RestUtils::getRestErrorCodeFromException($e), 'message' => sprintf( diff --git a/module/Rest/src/Util/RestUtils.php b/module/Rest/src/Util/RestUtils.php index c783f6cb..35b6cbc9 100644 --- a/module/Rest/src/Util/RestUtils.php +++ b/module/Rest/src/Util/RestUtils.php @@ -10,6 +10,7 @@ use Shlinkio\Shlink\Rest\Exception as Rest; class RestUtils { public const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE'; + // FIXME Should be INVALID_SHORT_URL_DELETION public const INVALID_SHORTCODE_DELETION_ERROR = 'INVALID_SHORTCODE_DELETION'; public const INVALID_URL_ERROR = 'INVALID_URL'; public const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT';