|
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\Model\ShortUrlMeta; |
|
11
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; |
|
13
|
|
|
use Shlinkio\Shlink\Rest\Util\RestUtils; |
|
14
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
|
15
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
16
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
17
|
|
|
|
|
18
|
|
|
class EditShortCodeAction extends AbstractRestAction |
|
19
|
|
|
{ |
|
20
|
|
|
protected const ROUTE_PATH = '/short-codes/{shortCode}'; |
|
21
|
|
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ShortUrlServiceInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
private $shortUrlService; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var TranslatorInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $translator; |
|
31
|
|
|
|
|
32
|
3 |
|
public function __construct( |
|
33
|
|
|
ShortUrlServiceInterface $shortUrlService, |
|
34
|
|
|
TranslatorInterface $translator, |
|
35
|
|
|
LoggerInterface $logger = null |
|
36
|
|
|
) { |
|
37
|
3 |
|
parent::__construct($logger); |
|
38
|
3 |
|
$this->shortUrlService = $shortUrlService; |
|
39
|
3 |
|
$this->translator = $translator; |
|
40
|
3 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Process an incoming server request and return a response, optionally delegating |
|
44
|
|
|
* to the next middleware component to create the response. |
|
45
|
|
|
* |
|
46
|
|
|
* @param ServerRequestInterface $request |
|
47
|
|
|
* |
|
48
|
|
|
* @return ResponseInterface |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
3 |
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
52
|
|
|
{ |
|
53
|
3 |
|
$postData = (array) $request->getParsedBody(); |
|
54
|
3 |
|
$shortCode = $request->getAttribute('shortCode', ''); |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
3 |
|
$this->shortUrlService->updateMetadataByShortCode( |
|
58
|
3 |
|
$shortCode, |
|
59
|
3 |
|
ShortUrlMeta::createFromRawData($postData) |
|
60
|
|
|
); |
|
61
|
1 |
|
return new EmptyResponse(); |
|
62
|
2 |
|
} catch (Exception\InvalidShortCodeException $e) { |
|
63
|
1 |
|
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); |
|
64
|
1 |
|
return new JsonResponse([ |
|
65
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
66
|
1 |
|
'message' => \sprintf($this->translator->translate('No URL found for short code "%s"'), $shortCode), |
|
67
|
1 |
|
], self::STATUS_NOT_FOUND); |
|
68
|
1 |
|
} catch (Exception\ValidationException $e) { |
|
69
|
1 |
|
$this->logger->warning('Provided data is invalid.' . PHP_EOL . $e); |
|
70
|
1 |
|
return new JsonResponse([ |
|
71
|
1 |
|
'error' => RestUtils::getRestErrorCodeFromException($e), |
|
72
|
1 |
|
'message' => $this->translator->translate('Provided data is invalid.'), |
|
73
|
1 |
|
], self::STATUS_BAD_REQUEST); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|