CreateShortUrlAction   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 10
dl 0
loc 21
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A buildShortUrlData() 0 13 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