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

ListShortUrlsAction::queryToListParams()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
nc 8
nop 1
dl 0
loc 15
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 9.6111
c 2
b 0
f 0
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