Completed
Pull Request — develop (#572)
by Alejandro
06:31
created

ListShortUrlsAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 55
ccs 19
cts 20
cp 0.95
rs 10
c 2
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 6 1
A __construct() 0 8 1
A queryToListParams() 0 15 5
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 8
    public function __construct(
32
        ShortUrlServiceInterface $shortUrlService,
33
        array $domainConfig,
34
        ?LoggerInterface $logger = null
35
    ) {
36 8
        parent::__construct($logger);
37 8
        $this->shortUrlService = $shortUrlService;
38 8
        $this->domainConfig = $domainConfig;
39
    }
40
41
    /**
42
     * @param Request $request
43
     * @return Response
44
     * @throws InvalidArgumentException
45
     */
46 8
    public function handle(Request $request): Response
47
    {
48 8
        $params = $this->queryToListParams($request->getQueryParams());
49 8
        $shortUrls = $this->shortUrlService->listShortUrls(...$params);
50 8
        return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer(
51 8
            $this->domainConfig
52
        ))]);
53
    }
54
55
    /**
56
     * @param array $query
57
     * @return array
58
     */
59 8
    private function queryToListParams(array $query): array
60
    {
61 8
        $dateRange = null;
62 8
        $startDate = isset($query['startDate']) ? Chronos::parse($query['startDate']) : null;
63 8
        $endDate = isset($query['endDate']) ? Chronos::parse($query['endDate']) : null;
64 8
        if ($startDate !== null || $endDate !== null) {
65
            $dateRange = new DateRange($startDate, $endDate);
66
        }
67
68
        return [
69 8
            (int) ($query['page'] ?? 1),
70 8
            $query['searchTerm'] ?? null,
71 8
            $query['tags'] ?? [],
72 8
            $query['orderBy'] ?? null,
73 8
            $dateRange,
74
        ];
75
    }
76
}
77