Completed
Push — master ( 5b9784...ff8441 )
by Alejandro
12s
created

ListShortcodesCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 1
rs 9.9332
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
5
6
use Shlinkio\Shlink\Common\Paginator\Adapter\PaginableRepositoryAdapter;
7
use Shlinkio\Shlink\Common\Paginator\Util\PaginatorUtilsTrait;
8
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
9
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
use Zend\I18n\Translator\TranslatorInterface;
16
17
class ListShortcodesCommand extends Command
18
{
19
    use PaginatorUtilsTrait;
20
21
    public const NAME = 'short-code:list';
22
    private const ALIASES = ['shortcode:list'];
23
24
    /**
25
     * @var ShortUrlServiceInterface
26
     */
27
    private $shortUrlService;
28
    /**
29
     * @var TranslatorInterface
30
     */
31
    private $translator;
32
    /**
33
     * @var array
34
     */
35
    private $domainConfig;
36
37 5
    public function __construct(
38
        ShortUrlServiceInterface $shortUrlService,
39
        TranslatorInterface $translator,
40
        array $domainConfig
41
    ) {
42 5
        $this->shortUrlService = $shortUrlService;
43 5
        $this->translator = $translator;
44 5
        parent::__construct();
45 5
        $this->domainConfig = $domainConfig;
46 5
    }
47
48 5
    protected function configure(): void
49
    {
50
        $this
51 5
            ->setName(self::NAME)
52 5
            ->setAliases(self::ALIASES)
53 5
            ->setDescription($this->translator->translate('List all short URLs'))
54 5
            ->addOption(
55 5
                'page',
56 5
                'p',
57 5
                InputOption::VALUE_OPTIONAL,
58 5
                sprintf(
59 5
                    $this->translator->translate('The first page to list (%s items per page)'),
60 5
                    PaginableRepositoryAdapter::ITEMS_PER_PAGE
61
                ),
62 5
                1
63
            )
64 5
            ->addOption(
65 5
                'searchTerm',
66 5
                's',
67 5
                InputOption::VALUE_OPTIONAL,
68 5
                $this->translator->translate(
69 5
                    'A query used to filter results by searching for it on the longUrl and shortCode fields'
70
                )
71
            )
72 5
            ->addOption(
73 5
                'tags',
74 5
                't',
75 5
                InputOption::VALUE_OPTIONAL,
76 5
                $this->translator->translate('A comma-separated list of tags to filter results')
77
            )
78 5
            ->addOption(
79 5
                'orderBy',
80 5
                'o',
81 5
                InputOption::VALUE_OPTIONAL,
82 5
                $this->translator->translate(
83 5
                    'The field from which we want to order by. Pass ASC or DESC separated by a comma'
84
                )
85
            )
86 5
            ->addOption(
87 5
                'showTags',
88 5
                null,
89 5
                InputOption::VALUE_NONE,
90 5
                $this->translator->translate('Whether to display the tags or not')
91
            );
92 5
    }
93
94 5
    protected function execute(InputInterface $input, OutputInterface $output): void
95
    {
96 5
        $io = new SymfonyStyle($input, $output);
97 5
        $page = (int) $input->getOption('page');
98 5
        $searchTerm = $input->getOption('searchTerm');
99 5
        $tags = $input->getOption('tags');
100 5
        $tags = ! empty($tags) ? \explode(',', $tags) : [];
101 5
        $showTags = $input->getOption('showTags');
102 5
        $transformer = new ShortUrlDataTransformer($this->domainConfig);
103
104
        do {
105 5
            $result = $this->shortUrlService->listShortUrls($page, $searchTerm, $tags, $this->processOrderBy($input));
106 5
            $page++;
107
108
            $headers = [
109 5
                $this->translator->translate('Short code'),
110 5
                $this->translator->translate('Short URL'),
111 5
                $this->translator->translate('Long URL'),
112 5
                $this->translator->translate('Date created'),
113 5
                $this->translator->translate('Visits count'),
114
            ];
115 5
            if ($showTags) {
116 1
                $headers[] = $this->translator->translate('Tags');
117
            }
118
119 5
            $rows = [];
120 5
            foreach ($result as $row) {
121 2
                $shortUrl = $transformer->transform($row);
122 2
                if ($showTags) {
123
                    $shortUrl['tags'] = \implode(', ', $shortUrl['tags']);
124
                } else {
125 2
                    unset($shortUrl['tags']);
126
                }
127
128 2
                unset($shortUrl['originalUrl']);
129 2
                $rows[] = \array_values($shortUrl);
130
            }
131 5
            $io->table($headers, $rows);
132
133 5
            if ($this->isLastPage($result)) {
134 3
                $continue = false;
135 3
                $io->success($this->translator->translate('Short codes properly listed'));
136
            } else {
137 2
                $continue = $io->confirm(
138 2
                    \sprintf($this->translator->translate('Continue with page') . ' <options=bold>%s</>?', $page),
139 2
                    false
140
                );
141
            }
142 5
        } while ($continue);
143 5
    }
144
145 5
    private function processOrderBy(InputInterface $input)
146
    {
147 5
        $orderBy = $input->getOption('orderBy');
148 5
        if (empty($orderBy)) {
149 5
            return null;
150
        }
151
152
        $orderBy = \explode(',', $orderBy);
153
        return \count($orderBy) === 1 ? $orderBy[0] : [$orderBy[0] => $orderBy[1]];
154
    }
155
}
156