Completed
Push — master ( 30297a...987919 )
by Alejandro
13s
created

ListShortcodesCommand::processOrderBy()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

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