1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\CLI\Command\Api; |
3
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
5
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey; |
6
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyService; |
7
|
|
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Zend\I18n\Translator\TranslatorInterface; |
14
|
|
|
|
15
|
|
|
class ListKeysCommand extends Command |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ApiKeyServiceInterface |
19
|
|
|
*/ |
20
|
|
|
private $apiKeyService; |
21
|
|
|
/** |
22
|
|
|
* @var TranslatorInterface |
23
|
|
|
*/ |
24
|
|
|
private $translator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ListKeysCommand constructor. |
28
|
|
|
* @param ApiKeyServiceInterface|ApiKeyService $apiKeyService |
29
|
|
|
* @param TranslatorInterface $translator |
30
|
|
|
* |
31
|
|
|
* @Inject({ApiKeyService::class, "translator"}) |
32
|
|
|
*/ |
33
|
2 |
|
public function __construct(ApiKeyServiceInterface $apiKeyService, TranslatorInterface $translator) |
34
|
|
|
{ |
35
|
2 |
|
$this->apiKeyService = $apiKeyService; |
36
|
2 |
|
$this->translator = $translator; |
37
|
2 |
|
parent::__construct(null); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
2 |
|
public function configure() |
41
|
|
|
{ |
42
|
2 |
|
$this->setName('api-key:list') |
43
|
2 |
|
->setDescription($this->translator->translate('Lists all the available API keys.')) |
44
|
2 |
|
->addOption( |
45
|
2 |
|
'enabledOnly', |
46
|
2 |
|
null, |
47
|
2 |
|
InputOption::VALUE_NONE, |
48
|
2 |
|
$this->translator->translate('Tells if only enabled API keys should be returned.') |
49
|
2 |
|
); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
2 |
|
public function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
2 |
|
$enabledOnly = $input->getOption('enabledOnly'); |
55
|
2 |
|
$list = $this->apiKeyService->listKeys($enabledOnly); |
56
|
|
|
|
57
|
2 |
|
$table = new Table($output); |
58
|
2 |
|
if ($enabledOnly) { |
59
|
1 |
|
$table->setHeaders([ |
60
|
1 |
|
$this->translator->translate('Key'), |
61
|
1 |
|
$this->translator->translate('Expiration date'), |
62
|
1 |
|
]); |
63
|
1 |
|
} else { |
64
|
1 |
|
$table->setHeaders([ |
65
|
1 |
|
$this->translator->translate('Key'), |
66
|
1 |
|
$this->translator->translate('Is enabled'), |
67
|
1 |
|
$this->translator->translate('Expiration date'), |
68
|
1 |
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var ApiKey $row */ |
72
|
2 |
|
foreach ($list as $row) { |
73
|
2 |
|
$key = $row->getKey(); |
74
|
2 |
|
$expiration = $row->getExpirationDate(); |
75
|
2 |
|
$rowData = []; |
76
|
|
|
|
77
|
2 |
|
if ($enabledOnly) { |
78
|
1 |
|
$rowData[] = $key; |
79
|
1 |
|
} else { |
80
|
1 |
|
$rowData[] = $row->isEnabled() ? $this->getSuccessString($key) : $this->getErrorString($key); |
81
|
1 |
|
$rowData[] = $row->isEnabled() ? $this->getSuccessString('+++') : $this->getErrorString('---'); |
82
|
|
|
} |
83
|
|
|
|
84
|
2 |
|
$rowData[] = isset($expiration) ? $expiration->format(\DateTime::ISO8601) : '-'; |
85
|
2 |
|
$table->addRow($rowData); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
$table->render(); |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $string |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function getErrorString($string) |
96
|
|
|
{ |
97
|
|
|
return sprintf('<fg=red>%s</>', $string); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $string |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
1 |
|
protected function getSuccessString($string) |
105
|
|
|
{ |
106
|
1 |
|
return sprintf('<info>%s</info>', $string); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|