1 | <?php |
||
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) |
|
39 | |||
40 | 2 | public function configure() |
|
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 | 2 | $formatMethod = ! $row->isEnabled() |
|
77 | 2 | ? 'getErrorString' |
|
78 | 2 | : ($row->isExpired() ? 'getWarningString' : 'getSuccessString'); |
|
79 | |||
80 | 2 | if ($enabledOnly) { |
|
81 | 1 | $rowData[] = $this->{$formatMethod}($key); |
|
82 | 1 | } else { |
|
83 | 1 | $rowData[] = $this->{$formatMethod}($key); |
|
84 | 1 | $rowData[] = $this->{$formatMethod}($this->getEnabledSymbol($row)); |
|
85 | } |
||
86 | |||
87 | 2 | $rowData[] = isset($expiration) ? $expiration->format(\DateTime::ISO8601) : '-'; |
|
88 | 2 | $table->addRow($rowData); |
|
89 | 2 | } |
|
90 | |||
91 | 2 | $table->render(); |
|
92 | 2 | } |
|
93 | |||
94 | /** |
||
95 | * @param string $string |
||
96 | * @return string |
||
97 | */ |
||
98 | protected function getErrorString($string) |
||
99 | { |
||
100 | return sprintf('<fg=red>%s</>', $string); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $string |
||
105 | * @return string |
||
106 | */ |
||
107 | 2 | protected function getSuccessString($string) |
|
111 | |||
112 | /** |
||
113 | * @param $string |
||
114 | * @return string |
||
115 | */ |
||
116 | protected function getWarningString($string) |
||
117 | { |
||
118 | return sprintf('<comment>%s</comment>', $string); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param ApiKey $apiKey |
||
123 | * @return string |
||
124 | */ |
||
125 | 1 | protected function getEnabledSymbol(ApiKey $apiKey) |
|
129 | } |
||
130 |