apiKeyService = $apiKeyService; $this->translator = $translator; parent::__construct(); } public function configure() { $this->setName('api-key:list') ->setDescription($this->translator->translate('Lists all the available API keys.')) ->addOption( 'enabledOnly', null, InputOption::VALUE_NONE, $this->translator->translate('Tells if only enabled API keys should be returned.') ); } public function execute(InputInterface $input, OutputInterface $output) { $enabledOnly = $input->getOption('enabledOnly'); $list = $this->apiKeyService->listKeys($enabledOnly); $table = new Table($output); if ($enabledOnly) { $table->setHeaders([ $this->translator->translate('Key'), $this->translator->translate('Expiration date'), ]); } else { $table->setHeaders([ $this->translator->translate('Key'), $this->translator->translate('Is enabled'), $this->translator->translate('Expiration date'), ]); } /** @var ApiKey $row */ foreach ($list as $row) { $key = $row->getKey(); $expiration = $row->getExpirationDate(); $rowData = []; $formatMethod = ! $row->isEnabled() ? 'getErrorString' : ($row->isExpired() ? 'getWarningString' : 'getSuccessString'); if ($enabledOnly) { $rowData[] = $this->{$formatMethod}($key); } else { $rowData[] = $this->{$formatMethod}($key); $rowData[] = $this->{$formatMethod}($this->getEnabledSymbol($row)); } $rowData[] = isset($expiration) ? $expiration->format(\DateTime::ATOM) : '-'; $table->addRow($rowData); } $table->render(); } /** * @param string $string * @return string */ protected function getErrorString($string) { return sprintf('%s', $string); } /** * @param string $string * @return string */ protected function getSuccessString($string) { return sprintf('%s', $string); } /** * @param $string * @return string */ protected function getWarningString($string) { return sprintf('%s', $string); } /** * @param ApiKey $apiKey * @return string */ protected function getEnabledSymbol(ApiKey $apiKey) { return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++'; } }