EditShortUrlAction.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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 Psr\Log\LoggerInterface;
  8. use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
  9. use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
  10. use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
  11. class EditShortUrlAction extends AbstractRestAction
  12. {
  13. protected const ROUTE_PATH = '/short-urls/{shortCode}';
  14. protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH, 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(ServerRequestInterface $request): ResponseInterface
  22. {
  23. $postData = (array) $request->getParsedBody();
  24. $shortCode = $request->getAttribute('shortCode', '');
  25. $this->shortUrlService->updateMetadataByShortCode($shortCode, ShortUrlMeta::createFromRawData($postData));
  26. return new EmptyResponse();
  27. }
  28. }