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
|
5 |
|
public function __construct(ShortUrlServiceInterface $shortUrlService, TranslatorInterface $translator) |
39
|
|
|
{ |
40
|
5 |
|
$this->shortUrlService = $shortUrlService; |
41
|
5 |
|
$this->translator = $translator; |
42
|
5 |
|
parent::__construct(null); |
43
|
5 |
|
} |
44
|
|
|
|
45
|
5 |
|
public function configure() |
46
|
|
|
{ |
47
|
5 |
|
$this->setName('shortcode:list') |
48
|
5 |
|
->setDescription($this->translator->translate('List all short URLs')) |
49
|
5 |
|
->addOption( |
50
|
5 |
|
'page', |
51
|
5 |
|
'p', |
52
|
5 |
|
InputOption::VALUE_OPTIONAL, |
53
|
5 |
|
sprintf( |
54
|
5 |
|
$this->translator->translate('The first page to list (%s items per page)'), |
55
|
|
|
PaginableRepositoryAdapter::ITEMS_PER_PAGE |
56
|
5 |
|
), |
57
|
|
|
1 |
58
|
5 |
|
) |
59
|
5 |
|
->addOption( |
60
|
5 |
|
'tags', |
61
|
5 |
|
't', |
62
|
5 |
|
InputOption::VALUE_NONE, |
63
|
5 |
|
$this->translator->translate('Whether to display the tags or not') |
64
|
5 |
|
); |
65
|
5 |
|
} |
66
|
|
|
|
67
|
5 |
|
public function execute(InputInterface $input, OutputInterface $output) |
68
|
|
|
{ |
69
|
5 |
|
$page = intval($input->getOption('page')); |
70
|
5 |
|
$showTags = $input->getOption('tags'); |
71
|
|
|
|
72
|
|
|
/** @var QuestionHelper $helper */ |
73
|
5 |
|
$helper = $this->getHelper('question'); |
74
|
|
|
|
75
|
|
|
do { |
76
|
5 |
|
$result = $this->shortUrlService->listShortUrls($page); |
77
|
5 |
|
$page++; |
78
|
5 |
|
$table = new Table($output); |
79
|
|
|
|
80
|
|
|
$headers = [ |
81
|
5 |
|
$this->translator->translate('Short code'), |
82
|
5 |
|
$this->translator->translate('Original URL'), |
83
|
5 |
|
$this->translator->translate('Date created'), |
84
|
5 |
|
$this->translator->translate('Visits count'), |
85
|
5 |
|
]; |
86
|
5 |
|
if ($showTags) { |
87
|
1 |
|
$headers[] = $this->translator->translate('Tags'); |
88
|
1 |
|
} |
89
|
5 |
|
$table->setHeaders($headers); |
90
|
|
|
|
91
|
5 |
|
foreach ($result as $row) { |
92
|
2 |
|
$shortUrl = $row->jsonSerialize(); |
93
|
2 |
|
if ($showTags) { |
94
|
|
|
$shortUrl['tags'] = []; |
95
|
|
|
foreach ($row->getTags() as $tag) { |
96
|
|
|
$shortUrl['tags'][] = $tag->getName(); |
97
|
|
|
} |
98
|
|
|
$shortUrl['tags'] = implode(', ', $shortUrl['tags']); |
99
|
|
|
} else { |
100
|
2 |
|
unset($shortUrl['tags']); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
$table->addRow(array_values($shortUrl)); |
104
|
5 |
|
} |
105
|
5 |
|
$table->render(); |
106
|
|
|
|
107
|
5 |
|
if ($this->isLastPage($result)) { |
|
|
|
|
108
|
4 |
|
$continue = false; |
109
|
4 |
|
$output->writeln( |
110
|
4 |
|
sprintf('<info>%s</info>', $this->translator->translate('You have reached last page')) |
111
|
4 |
|
); |
112
|
4 |
|
} else { |
113
|
2 |
|
$continue = $helper->ask($input, $output, new ConfirmationQuestion( |
114
|
2 |
|
sprintf('<question>' . $this->translator->translate( |
115
|
|
|
'Continue with page' |
116
|
2 |
|
) . ' <bg=cyan;options=bold>%s</>? (y/N)</question> ', $page), |
117
|
|
|
false |
118
|
2 |
|
)); |
119
|
|
|
} |
120
|
5 |
|
} while ($continue); |
|
|
|
|
121
|
5 |
|
} |
122
|
|
|
} |
123
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.