|
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
|
|
|
|