|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException; |
|
12
|
|
|
use Shlinkio\Shlink\Core\Model\CreateShortUrlData; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; |
|
15
|
|
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; |
|
16
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
17
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
18
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractCreateShortUrlAction extends AbstractRestAction |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var UrlShortenerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $urlShortener; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $domainConfig; |
|
30
|
|
|
/** |
|
31
|
|
|
* @var TranslatorInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $translator; |
|
34
|
|
|
|
|
35
|
9 |
|
public function __construct( |
|
36
|
|
|
UrlShortenerInterface $urlShortener, |
|
37
|
|
|
TranslatorInterface $translator, |
|
38
|
|
|
array $domainConfig, |
|
39
|
|
|
LoggerInterface $logger = null |
|
40
|
|
|
) { |
|
41
|
9 |
|
parent::__construct($logger); |
|
42
|
9 |
|
$this->urlShortener = $urlShortener; |
|
43
|
9 |
|
$this->translator = $translator; |
|
44
|
9 |
|
$this->domainConfig = $domainConfig; |
|
45
|
9 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Request $request |
|
49
|
|
|
* @return Response |
|
50
|
|
|
* @throws \InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public function handle(Request $request): Response |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
9 |
|
$shortUrlData = $this->buildShortUrlData($request); |
|
56
|
5 |
|
$shortUrlMeta = $shortUrlData->getMeta(); |
|
57
|
5 |
|
$longUrl = $shortUrlData->getLongUrl(); |
|
58
|
5 |
|
$customSlug = $shortUrlMeta->getCustomSlug(); |
|
59
|
4 |
|
} catch (InvalidArgumentException $e) { |
|
60
|
4 |
|
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); |
|
61
|
4 |
|
return new JsonResponse([ |
|
62
|
4 |
|
'error' => RestUtils::INVALID_ARGUMENT_ERROR, |
|
63
|
4 |
|
'message' => $e->getMessage(), |
|
64
|
4 |
|
], self::STATUS_BAD_REQUEST); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
5 |
|
$shortUrl = $this->urlShortener->urlToShortCode( |
|
69
|
5 |
|
$longUrl, |
|
70
|
5 |
|
$shortUrlData->getTags(), |
|
71
|
5 |
|
$shortUrlMeta->getValidSince(), |
|
72
|
5 |
|
$shortUrlMeta->getValidUntil(), |
|
73
|
5 |
|
$customSlug, |
|
74
|
5 |
|
$shortUrlMeta->getMaxVisits() |
|
75
|
|
|
); |
|
76
|
2 |
|
$transformer = new ShortUrlDataTransformer($this->domainConfig); |
|
77
|
|
|
|
|
78
|
2 |
|
return new JsonResponse($transformer->transform($shortUrl)); |
|
79
|
3 |
|
} catch (InvalidUrlException $e) { |
|
80
|
1 |
|
$this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e); |
|
81
|
1 |
|
return new JsonResponse([ |
|
82
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
83
|
1 |
|
'message' => \sprintf( |
|
84
|
1 |
|
$this->translator->translate('Provided URL %s is invalid. Try with a different one.'), |
|
85
|
1 |
|
$longUrl |
|
86
|
|
|
), |
|
87
|
1 |
|
], self::STATUS_BAD_REQUEST); |
|
88
|
2 |
|
} catch (NonUniqueSlugException $e) { |
|
89
|
1 |
|
$this->logger->warning('Provided non-unique slug.' . PHP_EOL . $e); |
|
90
|
1 |
|
return new JsonResponse([ |
|
91
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
92
|
1 |
|
'message' => \sprintf( |
|
93
|
1 |
|
$this->translator->translate('Provided slug %s is already in use. Try with a different one.'), |
|
94
|
1 |
|
$customSlug |
|
95
|
|
|
), |
|
96
|
1 |
|
], self::STATUS_BAD_REQUEST); |
|
97
|
1 |
|
} catch (\Throwable $e) { |
|
98
|
1 |
|
$this->logger->error('Unexpected error creating short url.' . PHP_EOL . $e); |
|
99
|
1 |
|
return new JsonResponse([ |
|
100
|
1 |
|
'error' => RestUtils::UNKNOWN_ERROR, |
|
101
|
1 |
|
'message' => $this->translator->translate('Unexpected error occurred'), |
|
102
|
1 |
|
], self::STATUS_INTERNAL_SERVER_ERROR); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param Request $request |
|
108
|
|
|
* @return CreateShortUrlData |
|
109
|
|
|
* @throws InvalidArgumentException |
|
110
|
|
|
*/ |
|
111
|
|
|
abstract protected function buildShortUrlData(Request $request): CreateShortUrlData; |
|
112
|
|
|
} |
|
113
|
|
|
|