Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

ListShortcodesCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 81
ccs 45
cts 45
cp 1
rs 10
wmc 6
lcom 1
cbo 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 15 1
B execute() 0 37 4
1
<?php
2
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
5
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter;
6
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait;
7
use Shlinkio\Shlink\Core\Service\ShortUrlService;
8
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Helper\QuestionHelper;
11
use Symfony\Component\Console\Helper\Table;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Question\ConfirmationQuestion;
16
use Zend\I18n\Translator\TranslatorInterface;
17
18
class ListShortcodesCommand extends Command
19
{
20
    use PaginatorUtilsTrait;
21
22
    /**
23
     * @var ShortUrlServiceInterface
24
     */
25
    private $shortUrlService;
26
    /**
27
     * @var TranslatorInterface
28
     */
29
    private $translator;
30
31
    /**
32
     * ListShortcodesCommand constructor.
33
     * @param ShortUrlServiceInterface $shortUrlService
34
     * @param TranslatorInterface $translator
35
     *
36
     * @Inject({ShortUrlService::class, "translator"})
37
     */
38 4
    public function __construct(ShortUrlServiceInterface $shortUrlService, TranslatorInterface $translator)
39
    {
40 4
        $this->shortUrlService = $shortUrlService;
41 4
        $this->translator = $translator;
42 4
        parent::__construct(null);
43 4
    }
44
45 4
    public function configure()
46
    {
47 4
        $this->setName('shortcode:list')
48 4
             ->setDescription($this->translator->translate('List all short URLs'))
49 4
             ->addOption(
50 4
                 'page',
51 4
                 'p',
52 4
                 InputOption::VALUE_OPTIONAL,
53 4
                 sprintf(
54 4
                     $this->translator->translate('The first page to list (%s items per page)'),
55
                     PaginableRepositoryAdapter::ITEMS_PER_PAGE
56 4
                 ),
57
                 1
58 4
             );
59 4
    }
60
61 4
    public function execute(InputInterface $input, OutputInterface $output)
62
    {
63 4
        $page = intval($input->getOption('page'));
64
        /** @var QuestionHelper $helper */
65 4
        $helper = $this->getHelper('question');
66
67
        do {
68 4
            $result = $this->shortUrlService->listShortUrls($page);
69 4
            $page++;
70 4
            $table = new Table($output);
71 4
            $table->setHeaders([
72 4
                $this->translator->translate('Short code'),
73 4
                $this->translator->translate('Original URL'),
74 4
                $this->translator->translate('Date created'),
75 4
                $this->translator->translate('Visits count'),
76 4
            ]);
77
78 4
            foreach ($result as $row) {
79 2
                $table->addRow(array_values($row->jsonSerialize()));
80 4
            }
81 4
            $table->render();
82
83 4
            if ($this->isLastPage($result)) {
0 ignored issues
show
Bug introduced by
It seems like $result defined by $this->shortUrlService->listShortUrls($page) on line 68 can also be of type array<integer,object<Shl...\Core\Entity\ShortUrl>>; however, Shlinkio\Shlink\Common\P...tilsTrait::isLastPage() 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...
84 3
                $continue = false;
85 3
                $output->writeln(
86 3
                    sprintf('<info>%s</info>', $this->translator->translate('You have reached last page'))
87 3
                );
88 3
            } else {
89 2
                $continue = $helper->ask($input, $output, new ConfirmationQuestion(
90 2
                    sprintf('<question>' . $this->translator->translate(
91
                        'Continue with page'
92 2
                    ) . ' <bg=cyan;options=bold>%s</>? (y/N)</question> ', $page),
93
                    false
94 2
                ));
95
            }
96 4
        } while ($continue);
0 ignored issues
show
Bug Best Practice introduced by
The expression $continue of type false|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
97 4
    }
98
}
99