SingleStepCreateShortUrlAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 39
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A buildShortUrlData() 0 20 3
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\Service\UrlShortenerInterface;
12
use Shlinkio\Shlink\Core\Validation\ShortUrlMetaInputFilter;
13
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
14
15
class SingleStepCreateShortUrlAction extends AbstractCreateShortUrlAction
16
{
17
    protected const ROUTE_PATH = '/short-urls/shorten';
18
    protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
19
20 3
    private ApiKeyServiceInterface $apiKeyService;
21
22
    public function __construct(
23
        UrlShortenerInterface $urlShortener,
24
        ApiKeyServiceInterface $apiKeyService,
25 3
        array $domainConfig
26 3
    ) {
27 3
        parent::__construct($urlShortener, $domainConfig);
28
        $this->apiKeyService = $apiKeyService;
29
    }
30
31
    /**
32 3
     * @throws ValidationException
33
     */
34 3
    protected function buildShortUrlData(Request $request): CreateShortUrlData
35
    {
36 3
        $query = $request->getQueryParams();
37 1
        $apiKey = $query['apiKey'] ?? '';
38 1
        $longUrl = $query['longUrl'] ?? null;
39
40
        if (! $this->apiKeyService->check($apiKey)) {
41
            throw ValidationException::fromArray([
42 2
                'apiKey' => 'No API key was provided or it is not valid',
43 1
            ]);
44 1
        }
45
46
        if ($longUrl === null) {
47
            throw ValidationException::fromArray([
48 1
                'longUrl' => 'A URL was not provided',
49
            ]);
50
        }
51
52
        return new CreateShortUrlData($longUrl, [], ShortUrlMeta::fromRawData([
53
            ShortUrlMetaInputFilter::API_KEY => $apiKey,
54
        ]));
55
    }
56
}
57