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