More classes renamed and fixes for usage of the short code concept in place of short URL

This commit is contained in:
Alejandro Celaya 2018-09-20 20:38:51 +02:00
parent 7ab993b764
commit 80fe3a73e2
6 changed files with 24 additions and 24 deletions

View File

@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Core\Model;
use Psr\Http\Message\UriInterface; use Psr\Http\Message\UriInterface;
final class CreateShortCodeData final class CreateShortUrlData
{ {
/** /**
* @var UriInterface * @var UriInterface

View File

@ -9,7 +9,7 @@ use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; 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\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
@ -52,10 +52,10 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
public function handle(Request $request): Response public function handle(Request $request): Response
{ {
try { try {
$shortCodeData = $this->buildUrlToShortCodeData($request); $shortUrlData = $this->buildShortUrlData($request);
$shortCodeMeta = $shortCodeData->getMeta(); $shortUrlMeta = $shortUrlData->getMeta();
$longUrl = $shortCodeData->getLongUrl(); $longUrl = $shortUrlData->getLongUrl();
$customSlug = $shortCodeMeta->getCustomSlug(); $customSlug = $shortUrlMeta->getCustomSlug();
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); $this->logger->warning('Provided data is invalid.' . PHP_EOL . $e);
return new JsonResponse([ return new JsonResponse([
@ -67,11 +67,11 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
try { try {
$shortUrl = $this->urlShortener->urlToShortCode( $shortUrl = $this->urlShortener->urlToShortCode(
$longUrl, $longUrl,
$shortCodeData->getTags(), $shortUrlData->getTags(),
$shortCodeMeta->getValidSince(), $shortUrlMeta->getValidSince(),
$shortCodeMeta->getValidUntil(), $shortUrlMeta->getValidUntil(),
$customSlug, $customSlug,
$shortCodeMeta->getMaxVisits() $shortUrlMeta->getMaxVisits()
); );
$transformer = new ShortUrlDataTransformer($this->domainConfig); $transformer = new ShortUrlDataTransformer($this->domainConfig);
@ -95,7 +95,7 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
), ),
], self::STATUS_BAD_REQUEST); ], self::STATUS_BAD_REQUEST);
} catch (\Throwable $e) { } 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([ return new JsonResponse([
'error' => RestUtils::UNKNOWN_ERROR, 'error' => RestUtils::UNKNOWN_ERROR,
'message' => $this->translator->translate('Unexpected error occurred'), 'message' => $this->translator->translate('Unexpected error occurred'),
@ -105,8 +105,8 @@ abstract class AbstractCreateShortUrlAction extends AbstractRestAction
/** /**
* @param Request $request * @param Request $request
* @return CreateShortCodeData * @return CreateShortUrlData
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
abstract protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData; abstract protected function buildShortUrlData(Request $request): CreateShortUrlData;
} }

View File

@ -5,7 +5,7 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; 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\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Rest\Action\ShortUrl\AbstractCreateShortUrlAction; use Shlinkio\Shlink\Rest\Action\ShortUrl\AbstractCreateShortUrlAction;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
@ -17,18 +17,18 @@ class CreateShortUrlAction extends AbstractCreateShortUrlAction
/** /**
* @param Request $request * @param Request $request
* @return CreateShortCodeData * @return CreateShortUrlData
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData protected function buildShortUrlData(Request $request): CreateShortUrlData
{ {
$postData = (array) $request->getParsedBody(); $postData = (array) $request->getParsedBody();
if (! isset($postData['longUrl'])) { if (! isset($postData['longUrl'])) {
throw new InvalidArgumentException($this->translator->translate('A URL was not provided')); throw new InvalidArgumentException($this->translator->translate('A URL was not provided'));
} }
return new CreateShortCodeData( return new CreateShortUrlData(
new Uri($postData['longUrl']), new Uri($postData['longUrl']),
(array) ($postData['tags'] ?? []), (array) ($postData['tags'] ?? []),
ShortUrlMeta::createFromParams( ShortUrlMeta::createFromParams(

View File

@ -6,9 +6,8 @@ namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; 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\Core\Service\UrlShortenerInterface;
use Shlinkio\Shlink\Rest\Action\ShortUrl\AbstractCreateShortUrlAction;
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
use Zend\Diactoros\Uri; use Zend\Diactoros\Uri;
use Zend\I18n\Translator\TranslatorInterface; use Zend\I18n\Translator\TranslatorInterface;
@ -36,11 +35,11 @@ class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
/** /**
* @param Request $request * @param Request $request
* @return CreateShortCodeData * @return CreateShortUrlData
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* @throws InvalidArgumentException * @throws InvalidArgumentException
*/ */
protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData protected function buildShortUrlData(Request $request): CreateShortUrlData
{ {
$query = $request->getQueryParams(); $query = $request->getQueryParams();
@ -56,6 +55,6 @@ class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
throw new InvalidArgumentException($this->translator->translate('A URL was not provided')); 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']));
} }
} }

View File

@ -16,7 +16,7 @@ use Zend\I18n\Translator\TranslatorInterface;
class GetVisitsAction extends AbstractRestAction 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]; protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
/** /**
@ -58,7 +58,7 @@ class GetVisitsAction extends AbstractRestAction
], ],
]); ]);
} catch (InvalidArgumentException $e) { } 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([ return new JsonResponse([
'error' => RestUtils::getRestErrorCodeFromException($e), 'error' => RestUtils::getRestErrorCodeFromException($e),
'message' => sprintf( 'message' => sprintf(

View File

@ -10,6 +10,7 @@ use Shlinkio\Shlink\Rest\Exception as Rest;
class RestUtils class RestUtils
{ {
public const INVALID_SHORTCODE_ERROR = 'INVALID_SHORTCODE'; 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_SHORTCODE_DELETION_ERROR = 'INVALID_SHORTCODE_DELETION';
public const INVALID_URL_ERROR = 'INVALID_URL'; public const INVALID_URL_ERROR = 'INVALID_URL';
public const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT'; public const INVALID_ARGUMENT_ERROR = 'INVALID_ARGUMENT';