1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\Rest\Action; |
3
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
5
|
|
|
use Interop\Http\ServerMiddleware\DelegateInterface; |
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\InvalidShortCodeException; |
10
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortener; |
11
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
12
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
13
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
14
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
class ResolveUrlAction extends AbstractRestAction |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var UrlShortenerInterface |
20
|
|
|
*/ |
21
|
|
|
private $urlShortener; |
22
|
|
|
/** |
23
|
|
|
* @var TranslatorInterface |
24
|
|
|
*/ |
25
|
|
|
private $translator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ResolveUrlAction constructor. |
29
|
|
|
* @param UrlShortenerInterface|UrlShortener $urlShortener |
30
|
|
|
* @param TranslatorInterface $translator |
31
|
|
|
* @param LoggerInterface $logger |
32
|
|
|
* |
33
|
|
|
* @Inject({UrlShortener::class, "translator"}) |
34
|
|
|
*/ |
35
|
4 |
View Code Duplication |
public function __construct( |
|
|
|
|
36
|
|
|
UrlShortenerInterface $urlShortener, |
37
|
|
|
TranslatorInterface $translator, |
38
|
|
|
LoggerInterface $logger = null |
39
|
|
|
) { |
40
|
4 |
|
parent::__construct($logger); |
41
|
4 |
|
$this->urlShortener = $urlShortener; |
42
|
4 |
|
$this->translator = $translator; |
43
|
4 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Request $request |
47
|
|
|
* @param DelegateInterface $delegate |
48
|
|
|
* @return null|Response |
49
|
|
|
* @throws \InvalidArgumentException |
50
|
|
|
*/ |
51
|
4 |
|
public function process(Request $request, DelegateInterface $delegate) |
52
|
|
|
{ |
53
|
4 |
|
$shortCode = $request->getAttribute('shortCode'); |
54
|
|
|
|
55
|
|
|
try { |
56
|
4 |
|
$longUrl = $this->urlShortener->shortCodeToUrl($shortCode); |
57
|
2 |
View Code Duplication |
if ($longUrl === null) { |
|
|
|
|
58
|
1 |
|
return new JsonResponse([ |
59
|
1 |
|
'error' => RestUtils::INVALID_ARGUMENT_ERROR, |
60
|
1 |
|
'message' => sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode), |
61
|
1 |
|
], self::STATUS_NOT_FOUND); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return new JsonResponse([ |
65
|
1 |
|
'longUrl' => $longUrl, |
66
|
1 |
|
]); |
67
|
2 |
|
} catch (InvalidShortCodeException $e) { |
68
|
1 |
|
$this->logger->warning('Provided short code with invalid format.' . PHP_EOL . $e); |
69
|
1 |
|
return new JsonResponse([ |
70
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
71
|
1 |
|
'message' => sprintf( |
72
|
1 |
|
$this->translator->translate('Provided short code "%s" has an invalid format'), |
73
|
|
|
$shortCode |
74
|
1 |
|
), |
75
|
1 |
|
], self::STATUS_BAD_REQUEST); |
76
|
1 |
|
} catch (\Exception $e) { |
77
|
1 |
|
$this->logger->error('Unexpected error while resolving the URL behind a short code.' . PHP_EOL . $e); |
78
|
1 |
|
return new JsonResponse([ |
79
|
1 |
|
'error' => RestUtils::UNKNOWN_ERROR, |
80
|
1 |
|
'message' => $this->translator->translate('Unexpected error occurred'), |
81
|
1 |
|
], self::STATUS_INTERNAL_SERVER_ERROR); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.