2016-06-12 17:51:30 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2018-09-20 19:55:24 +02:00
|
|
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
2016-06-12 17:51:30 +02:00
|
|
|
|
2018-09-29 12:52:32 +02:00
|
|
|
use Cake\Chronos\Chronos;
|
2016-06-12 17:51:30 +02:00
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
2018-05-01 19:35:12 +02:00
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
|
2019-10-20 10:30:11 +02:00
|
|
|
use Shlinkio\Shlink\Core\Exception\ValidationException;
|
2018-09-20 20:38:51 +02:00
|
|
|
use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
|
2018-05-01 19:35:12 +02:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
|
2016-06-12 17:51:30 +02:00
|
|
|
use Zend\Diactoros\Uri;
|
|
|
|
|
|
2018-09-20 19:55:24 +02:00
|
|
|
class CreateShortUrlAction extends AbstractCreateShortUrlAction
|
2016-06-12 17:51:30 +02:00
|
|
|
{
|
2018-09-20 20:27:34 +02:00
|
|
|
protected const ROUTE_PATH = '/short-urls';
|
2018-05-01 19:35:12 +02:00
|
|
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_POST];
|
2016-06-12 17:51:30 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Request $request
|
2018-09-20 20:38:51 +02:00
|
|
|
* @return CreateShortUrlData
|
2018-05-01 19:35:12 +02:00
|
|
|
* @throws InvalidArgumentException
|
2017-07-07 13:12:45 +02:00
|
|
|
* @throws \InvalidArgumentException
|
2016-06-12 17:51:30 +02:00
|
|
|
*/
|
2018-09-20 20:38:51 +02:00
|
|
|
protected function buildShortUrlData(Request $request): CreateShortUrlData
|
2016-06-12 17:51:30 +02:00
|
|
|
{
|
2017-12-27 16:23:54 +01:00
|
|
|
$postData = (array) $request->getParsedBody();
|
2016-06-12 17:51:30 +02:00
|
|
|
if (! isset($postData['longUrl'])) {
|
2018-11-18 16:28:04 +01:00
|
|
|
throw new InvalidArgumentException('A URL was not provided');
|
2016-06-12 17:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 10:30:11 +02:00
|
|
|
try {
|
|
|
|
|
$meta = ShortUrlMeta::createFromParams(
|
2017-10-21 20:09:30 +02:00
|
|
|
$this->getOptionalDate($postData, 'validSince'),
|
|
|
|
|
$this->getOptionalDate($postData, 'validUntil'),
|
2018-05-01 19:35:12 +02:00
|
|
|
$postData['customSlug'] ?? null,
|
2019-02-03 11:01:38 +01:00
|
|
|
$postData['maxVisits'] ?? null,
|
2019-10-02 20:13:25 +02:00
|
|
|
$postData['findIfExists'] ?? null,
|
|
|
|
|
$postData['domain'] ?? null
|
2019-10-20 10:30:11 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return new CreateShortUrlData(new Uri($postData['longUrl']), (array) ($postData['tags'] ?? []), $meta);
|
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
|
throw new InvalidArgumentException('Provided meta data is not valid', -1, $e);
|
|
|
|
|
}
|
2016-06-12 17:51:30 +02:00
|
|
|
}
|
2017-10-21 12:24:53 +02:00
|
|
|
|
2018-09-29 12:52:32 +02:00
|
|
|
private function getOptionalDate(array $postData, string $fieldName): ?Chronos
|
2017-10-21 12:24:53 +02:00
|
|
|
{
|
2018-09-29 12:52:32 +02:00
|
|
|
return isset($postData[$fieldName]) ? Chronos::parse($postData[$fieldName]) : null;
|
2017-10-21 12:24:53 +02:00
|
|
|
}
|
2016-06-12 17:51:30 +02:00
|
|
|
}
|