diff --git a/module/CLI/src/Command/Api/ListKeysCommand.php b/module/CLI/src/Command/Api/ListKeysCommand.php index d672025f..3b0c511c 100644 --- a/module/CLI/src/Command/Api/ListKeysCommand.php +++ b/module/CLI/src/Command/Api/ListKeysCommand.php @@ -50,74 +50,72 @@ class ListKeysCommand extends Command $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'), - ]); - } + $table->setHeaders(array_filter([ + $this->translator->translate('Key'), + ! $enabledOnly ? $this->translator->translate('Is enabled') : null, + $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'); + $formatMethod = $this->determineFormatMethod($row); - if ($enabledOnly) { - $rowData[] = $this->{$formatMethod}($key); - } else { - $rowData[] = $this->{$formatMethod}($key); - $rowData[] = $this->{$formatMethod}($this->getEnabledSymbol($row)); + $rowData[] = $formatMethod($key); + if (! $enabledOnly) { + $rowData[] = $formatMethod($this->getEnabledSymbol($row)); } - $rowData[] = isset($expiration) ? $expiration->format(\DateTime::ATOM) : '-'; + $rowData[] = $expiration !== null ? $expiration->format(\DateTime::ATOM) : '-'; $table->addRow($rowData); } $table->render(); } - /** - * @param string $string - * @return string - */ - protected function getErrorString($string) + private function determineFormatMethod(ApiKey $apiKey): callable { - return sprintf('%s', $string); + if (! $apiKey->isEnabled()) { + return [$this, 'getErrorString']; + } + + return $apiKey->isExpired() ? [$this, 'getWarningString'] : [$this, 'getSuccessString']; } /** - * @param string $string + * @param string $value * @return string */ - protected function getSuccessString($string) + private function getErrorString(string $value): string { - return sprintf('%s', $string); + return sprintf('%s', $value); } /** - * @param $string + * @param string $value * @return string */ - protected function getWarningString($string) + private function getSuccessString(string $value): string { - return sprintf('%s', $string); + return sprintf('%s', $value); + } + + /** + * @param string $value + * @return string + */ + private function getWarningString(string $value): string + { + return sprintf('%s', $value); } /** * @param ApiKey $apiKey * @return string */ - protected function getEnabledSymbol(ApiKey $apiKey) + private function getEnabledSymbol(ApiKey $apiKey): string { return ! $apiKey->isEnabled() || $apiKey->isExpired() ? '---' : '+++'; } diff --git a/module/Rest/src/Entity/ApiKey.php b/module/Rest/src/Entity/ApiKey.php index 4ef72f92..284cc302 100644 --- a/module/Rest/src/Entity/ApiKey.php +++ b/module/Rest/src/Entity/ApiKey.php @@ -25,7 +25,7 @@ class ApiKey extends AbstractEntity */ protected $key; /** - * @var \DateTime + * @var \DateTime|null * @ORM\Column(name="expiration_date", nullable=true, type="datetime") */ protected $expirationDate; @@ -60,7 +60,7 @@ class ApiKey extends AbstractEntity } /** - * @return \DateTime + * @return \DateTime|null */ public function getExpirationDate() {