|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Rest\Action\ShortCode; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Exception; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrl\DeleteShortUrlServiceInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; |
|
12
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
13
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
|
14
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
15
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
16
|
|
|
|
|
17
|
|
|
class DeleteShortCodeAction extends AbstractRestAction |
|
18
|
|
|
{ |
|
19
|
|
|
protected const ROUTE_PATH = '/short-codes/{shortCode}'; |
|
20
|
|
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_DELETE]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var DeleteShortUrlServiceInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $deleteShortUrlService; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var TranslatorInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $translator; |
|
30
|
|
|
|
|
31
|
3 |
|
public function __construct( |
|
32
|
|
|
DeleteShortUrlServiceInterface $deleteShortUrlService, |
|
33
|
|
|
TranslatorInterface $translator, |
|
34
|
|
|
LoggerInterface $logger = null |
|
35
|
|
|
) { |
|
36
|
3 |
|
parent::__construct($logger); |
|
37
|
3 |
|
$this->deleteShortUrlService = $deleteShortUrlService; |
|
38
|
3 |
|
$this->translator = $translator; |
|
39
|
3 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Handle the request and return a response. |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
45
|
|
|
{ |
|
46
|
3 |
|
$shortCode = $request->getAttribute('shortCode', ''); |
|
47
|
|
|
|
|
48
|
|
|
try { |
|
49
|
3 |
|
$this->deleteShortUrlService->deleteByShortCode($shortCode); |
|
50
|
1 |
|
return new EmptyResponse(); |
|
51
|
2 |
|
} catch (Exception\InvalidShortCodeException $e) { |
|
52
|
1 |
|
$this->logger->warning( |
|
53
|
1 |
|
\sprintf('Provided short code %s does not belong to any URL.', $shortCode) . PHP_EOL . $e |
|
54
|
|
|
); |
|
55
|
1 |
|
return new JsonResponse([ |
|
56
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
57
|
1 |
|
'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode), |
|
58
|
1 |
|
], self::STATUS_NOT_FOUND); |
|
59
|
1 |
|
} catch (Exception\DeleteShortUrlException $e) { |
|
60
|
1 |
|
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); |
|
61
|
1 |
|
$messagePlaceholder = $this->translator->translate( |
|
62
|
1 |
|
'It is not possible to delete URL with short code "%s" because it has reached more than "%s" visits.' |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
1 |
|
return new JsonResponse([ |
|
66
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
67
|
1 |
|
'message' => \sprintf($messagePlaceholder, $shortCode, $e->getVisitsThreshold()), |
|
68
|
1 |
|
], self::STATUS_BAD_REQUEST); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|