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
|
|
|
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
2019-11-21 20:05:06 +01:00
|
|
|
* @throws ValidationException
|
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'])) {
|
2019-11-21 20:05:06 +01:00
|
|
|
throw ValidationException::fromArray([
|
|
|
|
|
'longUrl' => 'A URL was not provided',
|
|
|
|
|
]);
|
2016-06-12 17:51:30 +02:00
|
|
|
}
|
|
|
|
|
|
2020-01-26 08:42:51 +01:00
|
|
|
$meta = ShortUrlMeta::fromRawData($postData);
|
2020-06-24 20:21:05 +02:00
|
|
|
return new CreateShortUrlData($postData['longUrl'], (array) ($postData['tags'] ?? []), $meta);
|
2016-06-12 17:51:30 +02:00
|
|
|
}
|
|
|
|
|
}
|