2016-06-12 22:31:28 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-07-19 18:07:59 +03:00
|
|
|
namespace Shlinkio\Shlink\Rest\Action;
|
2016-06-12 22:31:28 +03:00
|
|
|
|
2017-03-25 12:04:48 +03:00
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
2016-06-12 22:31:28 +03:00
|
|
|
use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2016-08-08 13:33:58 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-10-12 12:28:45 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
2016-07-19 19:01:39 +03:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
2016-07-19 18:07:59 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils;
|
2016-06-12 22:31:28 +03:00
|
|
|
use Zend\Diactoros\Response\JsonResponse;
|
2016-07-21 17:41:16 +03:00
|
|
|
use Zend\I18n\Translator\TranslatorInterface;
|
2016-06-12 22:31:28 +03:00
|
|
|
|
2016-07-27 21:22:50 +03:00
|
|
|
class ResolveUrlAction extends AbstractRestAction
|
2016-06-12 22:31:28 +03:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var UrlShortenerInterface
|
|
|
|
*/
|
|
|
|
private $urlShortener;
|
2016-07-21 17:41:16 +03:00
|
|
|
/**
|
|
|
|
* @var TranslatorInterface
|
|
|
|
*/
|
|
|
|
private $translator;
|
2016-06-12 22:31:28 +03:00
|
|
|
|
2016-08-08 13:33:58 +03:00
|
|
|
public function __construct(
|
|
|
|
UrlShortenerInterface $urlShortener,
|
|
|
|
TranslatorInterface $translator,
|
|
|
|
LoggerInterface $logger = null
|
|
|
|
) {
|
|
|
|
parent::__construct($logger);
|
2016-06-12 22:31:28 +03:00
|
|
|
$this->urlShortener = $urlShortener;
|
2016-07-21 17:41:16 +03:00
|
|
|
$this->translator = $translator;
|
2016-06-12 22:31:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Request $request
|
2017-03-25 12:04:48 +03:00
|
|
|
* @param DelegateInterface $delegate
|
2016-06-12 22:31:28 +03:00
|
|
|
* @return null|Response
|
2017-07-07 14:12:45 +03:00
|
|
|
* @throws \InvalidArgumentException
|
2016-06-12 22:31:28 +03:00
|
|
|
*/
|
2017-04-13 10:45:31 +03:00
|
|
|
public function process(Request $request, DelegateInterface $delegate)
|
2016-06-12 22:31:28 +03:00
|
|
|
{
|
|
|
|
$shortCode = $request->getAttribute('shortCode');
|
|
|
|
|
|
|
|
try {
|
|
|
|
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode);
|
|
|
|
return new JsonResponse([
|
|
|
|
'longUrl' => $longUrl,
|
|
|
|
]);
|
|
|
|
} catch (InvalidShortCodeException $e) {
|
2016-08-08 13:33:58 +03:00
|
|
|
$this->logger->warning('Provided short code with invalid format.' . PHP_EOL . $e);
|
2016-06-12 22:31:28 +03:00
|
|
|
return new JsonResponse([
|
|
|
|
'error' => RestUtils::getRestErrorCodeFromException($e),
|
2016-07-21 17:41:16 +03:00
|
|
|
'message' => sprintf(
|
|
|
|
$this->translator->translate('Provided short code "%s" has an invalid format'),
|
|
|
|
$shortCode
|
|
|
|
),
|
2017-03-25 12:04:48 +03:00
|
|
|
], self::STATUS_BAD_REQUEST);
|
2017-10-12 12:28:45 +03:00
|
|
|
} catch (EntityDoesNotExistException $e) {
|
|
|
|
$this->logger->warning('Provided short code couldn\'t be found.' . PHP_EOL . $e);
|
|
|
|
return new JsonResponse([
|
|
|
|
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
|
|
|
|
'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode),
|
|
|
|
], self::STATUS_NOT_FOUND);
|
2016-06-12 22:31:28 +03:00
|
|
|
} catch (\Exception $e) {
|
2016-08-08 13:33:58 +03:00
|
|
|
$this->logger->error('Unexpected error while resolving the URL behind a short code.' . PHP_EOL . $e);
|
2016-06-12 22:31:28 +03:00
|
|
|
return new JsonResponse([
|
|
|
|
'error' => RestUtils::UNKNOWN_ERROR,
|
2016-07-21 17:41:16 +03:00
|
|
|
'message' => $this->translator->translate('Unexpected error occurred'),
|
2017-03-25 12:04:48 +03:00
|
|
|
], self::STATUS_INTERNAL_SERVER_ERROR);
|
2016-06-12 22:31:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|