|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Rest\Action\ShortCode; |
|
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\CreateShortCodeData; |
|
13
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
|
14
|
|
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; |
|
15
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
16
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
17
|
|
|
use Zend\Diactoros\Uri; |
|
18
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
abstract class AbstractCreateShortCodeAction 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
|
5 |
View Code Duplication |
public function __construct( |
|
|
|
|
|
|
36
|
|
|
UrlShortenerInterface $urlShortener, |
|
37
|
|
|
TranslatorInterface $translator, |
|
38
|
|
|
array $domainConfig, |
|
39
|
|
|
LoggerInterface $logger = null |
|
40
|
|
|
) { |
|
41
|
5 |
|
parent::__construct($logger); |
|
42
|
5 |
|
$this->urlShortener = $urlShortener; |
|
43
|
5 |
|
$this->translator = $translator; |
|
44
|
5 |
|
$this->domainConfig = $domainConfig; |
|
45
|
5 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param Request $request |
|
49
|
|
|
* @return Response |
|
50
|
|
|
* @throws \InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
5 |
|
public function handle(Request $request): Response |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
5 |
|
$shortCodeData = $this->buildUrlToShortCodeData($request); |
|
56
|
4 |
|
$shortCodeMeta = $shortCodeData->getMeta(); |
|
57
|
4 |
|
$longUrl = $shortCodeData->getLongUrl(); |
|
58
|
4 |
|
$customSlug = $shortCodeMeta->getCustomSlug(); |
|
59
|
1 |
|
} catch (InvalidArgumentException $e) { |
|
60
|
1 |
|
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); |
|
61
|
1 |
|
return new JsonResponse([ |
|
62
|
1 |
|
'error' => RestUtils::INVALID_ARGUMENT_ERROR, |
|
63
|
1 |
|
'message' => $e->getMessage(), |
|
64
|
1 |
|
], self::STATUS_BAD_REQUEST); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
4 |
|
$shortCode = $this->urlShortener->urlToShortCode( |
|
69
|
4 |
|
$longUrl, |
|
70
|
4 |
|
$shortCodeData->getTags(), |
|
71
|
4 |
|
$shortCodeMeta->getValidSince(), |
|
72
|
4 |
|
$shortCodeMeta->getValidUntil(), |
|
73
|
4 |
|
$customSlug, |
|
74
|
4 |
|
$shortCodeMeta->getMaxVisits() |
|
75
|
|
|
); |
|
76
|
1 |
|
$shortUrl = (new Uri())->withPath($shortCode) |
|
77
|
1 |
|
->withScheme($this->domainConfig['schema']) |
|
78
|
1 |
|
->withHost($this->domainConfig['hostname']); |
|
79
|
|
|
|
|
80
|
|
|
// TODO Make response to be generated based on Accept header |
|
81
|
1 |
|
return new JsonResponse([ |
|
82
|
1 |
|
'longUrl' => (string) $longUrl, |
|
83
|
1 |
|
'shortUrl' => (string) $shortUrl, |
|
84
|
1 |
|
'shortCode' => $shortCode, |
|
85
|
|
|
]); |
|
86
|
3 |
|
} catch (InvalidUrlException $e) { |
|
87
|
1 |
|
$this->logger->warning('Provided Invalid URL.' . PHP_EOL . $e); |
|
88
|
1 |
|
return new JsonResponse([ |
|
89
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
90
|
1 |
|
'message' => \sprintf( |
|
91
|
1 |
|
$this->translator->translate('Provided URL %s is invalid. Try with a different one.'), |
|
92
|
1 |
|
$longUrl |
|
93
|
|
|
), |
|
94
|
1 |
|
], self::STATUS_BAD_REQUEST); |
|
95
|
2 |
|
} catch (NonUniqueSlugException $e) { |
|
96
|
1 |
|
$this->logger->warning('Provided non-unique slug.' . PHP_EOL . $e); |
|
97
|
1 |
|
return new JsonResponse([ |
|
98
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
99
|
1 |
|
'message' => \sprintf( |
|
100
|
1 |
|
$this->translator->translate('Provided slug %s is already in use. Try with a different one.'), |
|
101
|
1 |
|
$customSlug |
|
102
|
|
|
), |
|
103
|
1 |
|
], self::STATUS_BAD_REQUEST); |
|
104
|
1 |
|
} catch (\Throwable $e) { |
|
105
|
1 |
|
$this->logger->error('Unexpected error creating shortcode.' . PHP_EOL . $e); |
|
106
|
1 |
|
return new JsonResponse([ |
|
107
|
1 |
|
'error' => RestUtils::UNKNOWN_ERROR, |
|
108
|
1 |
|
'message' => $this->translator->translate('Unexpected error occurred'), |
|
109
|
1 |
|
], self::STATUS_INTERNAL_SERVER_ERROR); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param Request $request |
|
115
|
|
|
* @return CreateShortCodeData |
|
116
|
|
|
* @throws InvalidArgumentException |
|
117
|
|
|
*/ |
|
118
|
|
|
abstract protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData; |
|
119
|
|
|
} |
|
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.