Completed
Pull Request — master (#148)
by Alejandro
03:40
created

ListShortCodesAction::handle()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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