diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 5739228c..35624e51 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -13,6 +13,7 @@ return [ Command\Config\GenerateCharsetCommand::class, Command\Api\GenerateKeyCommand::class, Command\Api\DisableKeyCommand::class, + Command\Api\ListKeysCommand::class, ] ], diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 03e3bd2c..f3257fc6 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -19,6 +19,7 @@ return [ Command\Config\GenerateCharsetCommand::class => AnnotatedFactory::class, Command\Api\GenerateKeyCommand::class => AnnotatedFactory::class, Command\Api\DisableKeyCommand::class => AnnotatedFactory::class, + Command\Api\ListKeysCommand::class => AnnotatedFactory::class, ], ], diff --git a/module/CLI/src/Command/Api/ListKeysCommand.php b/module/CLI/src/Command/Api/ListKeysCommand.php new file mode 100644 index 00000000..e6f70ec0 --- /dev/null +++ b/module/CLI/src/Command/Api/ListKeysCommand.php @@ -0,0 +1,108 @@ +apiKeyService = $apiKeyService; + $this->translator = $translator; + parent::__construct(null); + } + + 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 = []; + + if ($enabledOnly) { + $rowData[] = $key; + } else { + $rowData[] = $row->isEnabled() ? $this->getSuccessString($key) : $this->getErrorString($key); + $rowData[] = $row->isEnabled() ? $this->getSuccessString('+++') : $this->getErrorString('---'); + } + + $rowData[] = isset($expiration) ? $expiration->format(\DateTime::ISO8601) : '-'; + $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); + } +} diff --git a/module/CLI/test/Command/Api/ListKeysCommandTest.php b/module/CLI/test/Command/Api/ListKeysCommandTest.php new file mode 100644 index 00000000..a7f58257 --- /dev/null +++ b/module/CLI/test/Command/Api/ListKeysCommandTest.php @@ -0,0 +1,62 @@ +apiKeyService = $this->prophesize(ApiKeyService::class); + $command = new ListKeysCommand($this->apiKeyService->reveal(), Translator::factory([])); + $app = new Application(); + $app->add($command); + $this->commandTester = new CommandTester($command); + } + + /** + * @test + */ + public function ifEnabledOnlyIsNotProvidedEverythingIsListed() + { + $this->apiKeyService->listKeys(false)->willReturn([ + new ApiKey(), + new ApiKey(), + new ApiKey(), + ])->shouldBeCalledTimes(1); + $this->commandTester->execute([ + 'command' => 'api-key:list', + ]); + } + + /** + * @test + */ + public function ifEnabledOnlyIsProvidedOnlyThoseKeysAreListed() + { + $this->apiKeyService->listKeys(true)->willReturn([ + new ApiKey(), + new ApiKey(), + ])->shouldBeCalledTimes(1); + $this->commandTester->execute([ + 'command' => 'api-key:list', + '--enabledOnly' => true, + ]); + } +}