|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl; |
|
6
|
|
|
|
|
7
|
|
|
use Cake\Chronos\Chronos; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
11
|
|
|
use Psr\Log\LoggerInterface; |
|
12
|
|
|
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait; |
|
13
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange; |
|
14
|
|
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface; |
|
15
|
|
|
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer; |
|
16
|
|
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction; |
|
17
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
18
|
|
|
|
|
19
|
|
|
class ListShortUrlsAction extends AbstractRestAction |
|
20
|
|
|
{ |
|
21
|
|
|
use PaginatorUtilsTrait; |
|
22
|
|
|
|
|
23
|
|
|
protected const ROUTE_PATH = '/short-urls'; |
|
24
|
|
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET]; |
|
25
|
|
|
|
|
26
|
|
|
/** @var ShortUrlServiceInterface */ |
|
27
|
|
|
private $shortUrlService; |
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
private $domainConfig; |
|
30
|
|
|
|
|
31
|
11 |
|
public function __construct( |
|
32
|
|
|
ShortUrlServiceInterface $shortUrlService, |
|
33
|
|
|
array $domainConfig, |
|
34
|
|
|
?LoggerInterface $logger = null |
|
35
|
|
|
) { |
|
36
|
11 |
|
parent::__construct($logger); |
|
37
|
11 |
|
$this->shortUrlService = $shortUrlService; |
|
38
|
11 |
|
$this->domainConfig = $domainConfig; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @return Response |
|
44
|
|
|
* @throws InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
11 |
|
public function handle(Request $request): Response |
|
47
|
|
|
{ |
|
48
|
11 |
|
$params = $this->queryToListParams($request->getQueryParams()); |
|
49
|
11 |
|
$shortUrls = $this->shortUrlService->listShortUrls(...$params); |
|
50
|
11 |
|
return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer( |
|
51
|
11 |
|
$this->domainConfig |
|
52
|
|
|
))]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param array $query |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
11 |
|
private function queryToListParams(array $query): array |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
11 |
|
(int) ($query['page'] ?? 1), |
|
63
|
11 |
|
$query['searchTerm'] ?? null, |
|
64
|
11 |
|
$query['tags'] ?? [], |
|
65
|
11 |
|
$query['orderBy'] ?? null, |
|
66
|
11 |
|
$this->determineDateRangeFromQuery($query), |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
11 |
|
private function determineDateRangeFromQuery(array $query): DateRange |
|
71
|
|
|
{ |
|
72
|
11 |
|
return new DateRange( |
|
73
|
11 |
|
isset($query['startDate']) ? Chronos::parse($query['startDate']) : null, |
|
74
|
11 |
|
isset($query['endDate']) ? Chronos::parse($query['endDate']) : null |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|