AbstractCreateShortUrlAction::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
ccs 8
cts 8
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
6
7
use Laminas\Diactoros\Response\JsonResponse;
8
use Psr\Http\Message\ResponseInterface as Response;
9
use Psr\Http\Message\ServerRequestInterface as Request;
10
use Shlinkio\Shlink\Core\Exception\ValidationException;
11
use Shlinkio\Shlink\Core\Model\CreateShortUrlData;
12
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
13
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
14
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
15
16
abstract class AbstractCreateShortUrlAction extends AbstractRestAction
17
{
18
    private UrlShortenerInterface $urlShortener;
19
    private array $domainConfig;
20
21 10
    public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig)
22
    {
23 10
        $this->urlShortener = $urlShortener;
24 10
        $this->domainConfig = $domainConfig;
25 10
    }
26
27 10
    public function handle(Request $request): Response
28
    {
29 10
        $shortUrlData = $this->buildShortUrlData($request);
30 4
        $longUrl = $shortUrlData->getLongUrl();
31 4
        $tags = $shortUrlData->getTags();
32 4
        $shortUrlMeta = $shortUrlData->getMeta();
33
34 4
        $shortUrl = $this->urlShortener->shorten($longUrl, $tags, $shortUrlMeta);
35 4
        $transformer = new ShortUrlDataTransformer($this->domainConfig);
36
37 4
        return new JsonResponse($transformer->transform($shortUrl));
38
    }
39
40
    /**
41
     * @throws ValidationException
42
     */
43
    abstract protected function buildShortUrlData(Request $request): CreateShortUrlData;
44
}
45