mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Renamed rest actions to use the short-url concept instead of the short-code concept
This commit is contained in:
47
module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php
Normal file
47
module/Rest/src/Action/ShortUrl/CreateShortUrlAction.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
|
||||
use Shlinkio\Shlink\Core\Model\CreateShortCodeData;
|
||||
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
||||
use Shlinkio\Shlink\Rest\Action\ShortUrl\AbstractCreateShortUrlAction;
|
||||
use Zend\Diactoros\Uri;
|
||||
|
||||
class CreateShortUrlAction extends AbstractCreateShortUrlAction
|
||||
{
|
||||
protected const ROUTE_PATH = '/short-codes';
|
||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_POST];
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return CreateShortCodeData
|
||||
* @throws InvalidArgumentException
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
protected function buildUrlToShortCodeData(Request $request): CreateShortCodeData
|
||||
{
|
||||
$postData = (array) $request->getParsedBody();
|
||||
if (! isset($postData['longUrl'])) {
|
||||
throw new InvalidArgumentException($this->translator->translate('A URL was not provided'));
|
||||
}
|
||||
|
||||
return new CreateShortCodeData(
|
||||
new Uri($postData['longUrl']),
|
||||
(array) ($postData['tags'] ?? []),
|
||||
ShortUrlMeta::createFromParams(
|
||||
$this->getOptionalDate($postData, 'validSince'),
|
||||
$this->getOptionalDate($postData, 'validUntil'),
|
||||
$postData['customSlug'] ?? null,
|
||||
isset($postData['maxVisits']) ? (int) $postData['maxVisits'] : null
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private function getOptionalDate(array $postData, string $fieldName)
|
||||
{
|
||||
return isset($postData[$fieldName]) ? new \DateTime($postData[$fieldName]) : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user