CreateShortUrlAction::buildShortUrlData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 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\ValidationException;
9
use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
10
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
11
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
12
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
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 7
     * @throws ValidationException
21
     */
22 7
    protected function buildShortUrlData(Request $request): CreateShortUrlData
23 7
    {
24 1
        $payload = (array) $request->getParsedBody();
25 1
        if (! isset($payload['longUrl'])) {
26
            throw ValidationException::fromArray([
27
                'longUrl' => 'A URL was not provided',
28
            ]);
29 6
        }
30 3
31
        $payload[ShortUrlMetaInputFilter::API_KEY] = AuthenticationMiddleware::apiKeyFromRequest($request);
32
        $meta = ShortUrlMeta::fromRawData($payload);
33
34
        return new CreateShortUrlData($payload['longUrl'], (array) ($payload['tags'] ?? []), $meta);
35
    }
36
}
37