Completed
Pull Request — master (#98)
by Alejandro
06:29
created

ListShortcodesAction::dispatch()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 4
nop 2
dl 0
loc 14
ccs 10
cts 10
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Shlinkio\Shlink\Rest\Action;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Interop\Http\ServerMiddleware\DelegateInterface;
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\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 DelegateInterface $delegate
50
     * @return null|Response
51
     * @throws \InvalidArgumentException
52
     */
53 2
    public function process(Request $request, DelegateInterface $delegate)
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, 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
            ], self::STATUS_INTERNAL_SERVER_ERROR);
65
        }
66
    }
67
68
    /**
69
     * @param array $query
70
     * @return array
71
     */
72 2
    public function queryToListParams(array $query)
73
    {
74
        return [
75 2
            isset($query['page']) ? $query['page'] : 1,
76 2
            isset($query['searchTerm']) ? $query['searchTerm'] : null,
77 2
            isset($query['tags']) ? $query['tags'] : [],
78 2
            isset($query['orderBy']) ? $query['orderBy'] : null,
79
        ];
80
    }
81
}
82