CreateShortUrlAction.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare(strict_types=1);
  3. namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
  4. use Laminas\Diactoros\Uri;
  5. use Psr\Http\Message\ServerRequestInterface as Request;
  6. use Shlinkio\Shlink\Core\Exception\ValidationException;
  7. use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
  8. use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
  9. class CreateShortUrlAction extends AbstractCreateShortUrlAction
  10. {
  11. protected const ROUTE_PATH = '/short-urls';
  12. protected const ROUTE_ALLOWED_METHODS = [self::METHOD_POST];
  13. /**
  14. * @throws ValidationException
  15. */
  16. protected function buildShortUrlData(Request $request): CreateShortUrlData
  17. {
  18. $postData = (array) $request->getParsedBody();
  19. if (! isset($postData['longUrl'])) {
  20. throw ValidationException::fromArray([
  21. 'longUrl' => 'A URL was not provided',
  22. ]);
  23. }
  24. $meta = ShortUrlMeta::createFromParams(
  25. $postData['validSince'] ?? null,
  26. $postData['validUntil'] ?? null,
  27. $postData['customSlug'] ?? null,
  28. $postData['maxVisits'] ?? null,
  29. $postData['findIfExists'] ?? null,
  30. $postData['domain'] ?? null,
  31. );
  32. return new CreateShortUrlData(new Uri($postData['longUrl']), (array) ($postData['tags'] ?? []), $meta);
  33. }
  34. }