Completed
Push — master ( 6eef69...27b08f )
by Alejandro
07:50
created

ListShortcodesAction::queryToListParams()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 16
nop 1
dl 0
loc 9
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 30
1
<?php
2
namespace Shlinkio\Shlink\Rest\Action;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Psr\Http\Message\ResponseInterface as Response;
6
use Psr\Http\Message\ServerRequestInterface as Request;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait;
10
use Shlinkio\Shlink\Core\Service\ShortUrlService;
11
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
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
    /**
21
     * @var ShortUrlServiceInterface
22
     */
23
    private $shortUrlService;
24
    /**
25
     * @var TranslatorInterface
26
     */
27
    private $translator;
28
29
    /**
30
     * ListShortcodesAction constructor.
31
     * @param ShortUrlServiceInterface|ShortUrlService $shortUrlService
32
     * @param TranslatorInterface $translator
33
     * @param LoggerInterface $logger
34
     *
35
     * @Inject({ShortUrlService::class, "translator", "Logger_Shlink"})
36
     */
37 2 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
        ShortUrlServiceInterface $shortUrlService,
39
        TranslatorInterface $translator,
40
        LoggerInterface $logger = null
41
    ) {
42 2
        parent::__construct($logger);
43 2
        $this->shortUrlService = $shortUrlService;
44 2
        $this->translator = $translator;
45 2
    }
46
47
    /**
48
     * @param Request $request
49
     * @param Response $response
50
     * @param callable|null $out
51
     * @return null|Response
52
     */
53 2
    public function dispatch(Request $request, Response $response, callable $out = null)
54
    {
55
        try {
56 2
            $params = $this->queryToListParams($request->getQueryParams());
57 2
            $shortUrls = $this->shortUrlService->listShortUrls(...$params);
0 ignored issues
show
Documentation introduced by
$params is of type array<integer,?,{"0":"?"...":"?","2":"?","3":"?"}>, 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...
58 1
            return new JsonResponse(['shortUrls' => $this->serializePaginator($shortUrls)]);
0 ignored issues
show
Bug introduced by
It seems like $shortUrls defined by $this->shortUrlService->listShortUrls(...$params) on line 57 can also be of type array<integer,object<Shl...\Core\Entity\ShortUrl>>; however, Shlinkio\Shlink\Common\P...t::serializePaginator() does only seem to accept object<Zend\Paginator\Paginator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59 1
        } catch (\Exception $e) {
60 1
            $this->logger->error('Unexpected error while listing short URLs.' . PHP_EOL . $e);
61 1
            return new JsonResponse([
62 1
                'error' => RestUtils::UNKNOWN_ERROR,
63 1
                'message' => $this->translator->translate('Unexpected error occurred'),
64 1
            ], 500);
65
        }
66
    }
67
68
    /**
69
     * @param array $query
70
     * @return string
71
     */
72
    public function queryToListParams(array $query)
73
    {
74
        return [
75
            isset($query['page']) ? $query['page'] : 1,
76
            isset($query['searchTerm']) ? $query['searchTerm'] : null,
77
            isset($query['tags']) ? $query['tags'] : [],
78
            isset($query['orderBy']) ? $query['orderBy'] : null,
79
        ];
80
    }
81
}
82