UpdateTagAction.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Rest\Action\Tag;
  4. use Laminas\Diactoros\Response\EmptyResponse;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Shlinkio\Shlink\Core\Exception\ValidationException;
  9. use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
  10. use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
  11. class UpdateTagAction extends AbstractRestAction
  12. {
  13. protected const ROUTE_PATH = '/tags';
  14. protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PUT];
  15. private TagServiceInterface $tagService;
  16. public function __construct(TagServiceInterface $tagService, ?LoggerInterface $logger = null)
  17. {
  18. parent::__construct($logger);
  19. $this->tagService = $tagService;
  20. }
  21. /**
  22. * Process an incoming server request and return a response, optionally delegating
  23. * to the next middleware component to create the response.
  24. *
  25. *
  26. * @throws \InvalidArgumentException
  27. */
  28. public function handle(ServerRequestInterface $request): ResponseInterface
  29. {
  30. $body = $request->getParsedBody();
  31. if (! isset($body['oldName'], $body['newName'])) {
  32. throw ValidationException::fromArray([
  33. 'oldName' => 'oldName is required',
  34. 'newName' => 'newName is required',
  35. ]);
  36. }
  37. $this->tagService->renameTag($body['oldName'], $body['newName']);
  38. return new EmptyResponse();
  39. }
  40. }