EditShortUrlTagsAction.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
  4. use Laminas\Diactoros\Response\JsonResponse;
  5. use Psr\Http\Message\ResponseInterface as Response;
  6. use Psr\Http\Message\ServerRequestInterface as Request;
  7. use Psr\Log\LoggerInterface;
  8. use Shlinkio\Shlink\Core\Exception\ValidationException;
  9. use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
  10. use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
  11. class EditShortUrlTagsAction extends AbstractRestAction
  12. {
  13. protected const ROUTE_PATH = '/short-urls/{shortCode}/tags';
  14. protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT];
  15. private ShortUrlServiceInterface $shortUrlService;
  16. public function __construct(ShortUrlServiceInterface $shortUrlService, ?LoggerInterface $logger = null)
  17. {
  18. parent::__construct($logger);
  19. $this->shortUrlService = $shortUrlService;
  20. }
  21. public function handle(Request $request): Response
  22. {
  23. $shortCode = $request->getAttribute('shortCode');
  24. $bodyParams = $request->getParsedBody();
  25. if (! isset($bodyParams['tags'])) {
  26. throw ValidationException::fromArray([
  27. 'tags' => 'List of tags has to be provided',
  28. ]);
  29. }
  30. $tags = $bodyParams['tags'];
  31. $shortUrl = $this->shortUrlService->setTagsByShortCode($shortCode, $tags);
  32. return new JsonResponse(['tags' => $shortUrl->getTags()->toArray()]);
  33. }
  34. }