|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl; |
|
5
|
|
|
|
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException; |
|
9
|
|
|
use Shlinkio\Shlink\Core\Model\CreateShortUrlData; |
|
10
|
|
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface; |
|
11
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; |
|
12
|
|
|
use Zend\Diactoros\Uri; |
|
13
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
|
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
|
|
|
/** |
|
21
|
|
|
* @var ApiKeyServiceInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $apiKeyService; |
|
24
|
|
|
|
|
25
|
4 |
|
public function __construct( |
|
26
|
|
|
UrlShortenerInterface $urlShortener, |
|
27
|
|
|
TranslatorInterface $translator, |
|
28
|
|
|
ApiKeyServiceInterface $apiKeyService, |
|
29
|
|
|
array $domainConfig, |
|
30
|
|
|
LoggerInterface $logger = null |
|
31
|
|
|
) { |
|
32
|
4 |
|
parent::__construct($urlShortener, $translator, $domainConfig, $logger); |
|
33
|
4 |
|
$this->apiKeyService = $apiKeyService; |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Request $request |
|
38
|
|
|
* @return CreateShortUrlData |
|
39
|
|
|
* @throws \InvalidArgumentException |
|
40
|
|
|
* @throws InvalidArgumentException |
|
41
|
|
|
*/ |
|
42
|
4 |
|
protected function buildShortUrlData(Request $request): CreateShortUrlData |
|
43
|
|
|
{ |
|
44
|
4 |
|
$query = $request->getQueryParams(); |
|
45
|
|
|
|
|
46
|
|
|
// Check provided API key |
|
47
|
4 |
|
$apiKey = $this->apiKeyService->getByKey($query['apiKey'] ?? ''); |
|
48
|
4 |
|
if ($apiKey === null || ! $apiKey->isValid()) { |
|
49
|
2 |
|
throw new InvalidArgumentException( |
|
50
|
2 |
|
$this->translator->translate('No API key was provided or it is not valid') |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
if (! isset($query['longUrl'])) { |
|
55
|
1 |
|
throw new InvalidArgumentException($this->translator->translate('A URL was not provided')); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
return new CreateShortUrlData(new Uri($query['longUrl'])); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|