EditShortUrlAction.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
  4. use Laminas\Diactoros\Response\EmptyResponse;
  5. use Psr\Http\Message\ResponseInterface;
  6. use Psr\Http\Message\ServerRequestInterface;
  7. use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
  8. use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
  9. use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
  10. use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
  11. use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
  12. class EditShortUrlAction extends AbstractRestAction
  13. {
  14. protected const ROUTE_PATH = '/short-urls/{shortCode}';
  15. protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH, self::METHOD_PUT];
  16. private ShortUrlServiceInterface $shortUrlService;
  17. public function __construct(ShortUrlServiceInterface $shortUrlService)
  18. {
  19. $this->shortUrlService = $shortUrlService;
  20. }
  21. public function handle(ServerRequestInterface $request): ResponseInterface
  22. {
  23. $shortUrlEdit = ShortUrlEdit::fromRawData((array) $request->getParsedBody());
  24. $identifier = ShortUrlIdentifier::fromApiRequest($request);
  25. $apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
  26. $this->shortUrlService->updateMetadataByShortCode($identifier, $shortUrlEdit, $apiKey);
  27. return new EmptyResponse();
  28. }
  29. }