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)) { |
|
|
|
|
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); |
|
|
|
|
97
|
4 |
|
} |
98
|
|
|
} |
99
|
|
|
|
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.