Completed
Pull Request — master (#148)
by Alejandro
04:08
created

ListShortCodesAction::queryToListParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 9.6666
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\Rest\Action\ShortCode;
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\Rest\Action\AbstractRestAction;
12
use Shlinkio\Shlink\Rest\Util\RestUtils;
13
use Zend\Diactoros\Response\JsonResponse;
14
use Zend\I18n\Translator\TranslatorInterface;
15
16
class ListShortCodesAction extends AbstractRestAction
17
{
18
    use PaginatorUtilsTrait;
19
20
    protected const ROUTE_PATH = '/short-codes';
21
    protected const ROUTE_ALLOWED_METHODS = [self::METHOD_GET];
22
23
    /**
24
     * @var ShortUrlServiceInterface
25
     */
26
    private $shortUrlService;
27
    /**
28
     * @var TranslatorInterface
29
     */
30
    private $translator;
31
32 2
    public function __construct(
33
        ShortUrlServiceInterface $shortUrlService,
34
        TranslatorInterface $translator,
35
        LoggerInterface $logger = null
36
    ) {
37 2
        parent::__construct($logger);
38 2
        $this->shortUrlService = $shortUrlService;
39 2
        $this->translator = $translator;
40 2
    }
41
42
    /**
43
     * @param Request $request
44
     * @return Response
45
     * @throws \InvalidArgumentException
46
     */
47 2
    public function handle(Request $request): Response
48
    {
49
        try {
50 2
            $params = $this->queryToListParams($request->getQueryParams());
51 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...
52 1
            return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls)]);
53 1
        } catch (\Exception $e) {
54 1
            $this->logger->error('Unexpected error while listing short URLs.' . PHP_EOL . $e);
55 1
            return new JsonResponse([
56 1
                'error' => RestUtils::UNKNOWN_ERROR,
57 1
                'message' => $this->translator->translate('Unexpected error occurred'),
58 1
            ], self::STATUS_INTERNAL_SERVER_ERROR);
59
        }
60
    }
61
62
    /**
63
     * @param array $query
64
     * @return array
65
     */
66 2
    private function queryToListParams(array $query): array
67
    {
68
        return [
69 2
            $query['page'] ?? 1,
70 2
            $query['searchTerm'] ?? null,
71 2
            $query['tags'] ?? [],
72 2
            $query['orderBy'] ?? null,
73
        ];
74
    }
75
}
76