Completed
Push — master ( 115ca0...3479bb )
by Alejandro
28s queued 13s
created

CreateShortUrlAction::getOptionalDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 2
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