Completed
Pull Request — master (#213)
by Alejandro
04:40
created

ListShortUrlsAction::handle()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 4
nop 1
dl 0
loc 16
ccs 11
cts 11
cp 1
crap 2
rs 9.7333
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
5
6
use Psr\Http\Message\ResponseInterface as Response;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Log\LoggerInterface;
9
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait;
10
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
11
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
12
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
13
use Shlinkio\Shlink\Rest\Util\RestUtils;
14
use Zend\Diactoros\Response\JsonResponse;
15
use Zend\I18n\Translator\TranslatorInterface;
16
17
class ListShortUrlsAction extends AbstractRestAction
18
{
19
    use PaginatorUtilsTrait;
20
21
    protected const ROUTE_PATH = '/short-urls';
22
    protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
23
24
    /**
25
     * @var ShortUrlServiceInterface
26
     */
27
    private $shortUrlService;
28
    /**
29
     * @var TranslatorInterface
30
     */
31
    private $translator;
32
    /**
33
     * @var array
34
     */
35
    private $domainConfig;
36
37 2
    public function __construct(
38
        ShortUrlServiceInterface $shortUrlService,
39
        TranslatorInterface $translator,
40
        array $domainConfig,
41
        LoggerInterface $logger = null
42
    ) {
43 2
        parent::__construct($logger);
44 2
        $this->shortUrlService = $shortUrlService;
45 2
        $this->translator = $translator;
46 2
        $this->domainConfig = $domainConfig;
47 2
    }
48
49
    /**
50
     * @param Request $request
51
     * @return Response
52
     * @throws \InvalidArgumentException
53
     */
54 2
    public function handle(Request $request): Response
55
    {
56
        try {
57 2
            $params = $this->queryToListParams($request->getQueryParams());
58 2
            $shortUrls = $this->shortUrlService->listShortUrls(...$params);
0 ignored issues
show
Documentation introduced by
$params is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59 1
            return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls, new ShortUrlDataTransformer(
60 1
                $this->domainConfig
61
            ))]);
62 1
        } catch (\Exception $e) {
63 1
            $this->logger->error('Unexpected error while listing short URLs.' . PHP_EOL . $e);
64 1
            return new JsonResponse([
65 1
                'error' => RestUtils::UNKNOWN_ERROR,
66 1
                'message' => $this->translator->translate('Unexpected error occurred'),
67 1
            ], self::STATUS_INTERNAL_SERVER_ERROR);
68
        }
69
    }
70
71
    /**
72
     * @param array $query
73
     * @return array
74
     */
75 2
    private function queryToListParams(array $query): array
76
    {
77
        return [
78 2
            (int) ($query['page'] ?? 1),
79 2
            $query['searchTerm'] ?? null,
80 2
            $query['tags'] ?? [],
81 2
            $query['orderBy'] ?? null,
82
        ];
83
    }
84
}
85