From 2d83b8d04610ee057714855b143184c3f0cc868a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 1 Nov 2025 11:41:50 +0100 Subject: [PATCH] Make InitialApiKeyCommand invokable --- .../src/Command/Api/InitialApiKeyCommand.php | 32 ++++++++----------- .../Command/Api/InitialApiKeyCommandTest.php | 2 +- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/module/CLI/src/Command/Api/InitialApiKeyCommand.php b/module/CLI/src/Command/Api/InitialApiKeyCommand.php index 66968eb3..4c6698f1 100644 --- a/module/CLI/src/Command/Api/InitialApiKeyCommand.php +++ b/module/CLI/src/Command/Api/InitialApiKeyCommand.php @@ -5,11 +5,15 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI\Command\Api; use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface; +use Symfony\Component\Console\Attribute\Argument; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; +#[AsCommand( + name: InitialApiKeyCommand::NAME, + description: 'Tries to create initial API key' +)] class InitialApiKeyCommand extends Command { public const string NAME = 'api-key:initial'; @@ -19,22 +23,14 @@ class InitialApiKeyCommand extends Command parent::__construct(); } - protected function configure(): void - { - $this - ->setHidden() - ->setName(self::NAME) - ->setDescription('Tries to create initial API key') - ->addArgument('apiKey', InputArgument::REQUIRED, 'The initial API to create'); - } + public function __invoke( + SymfonyStyle $io, + #[Argument('The initial API to create')] string $apiKey + ): int { + $result = $this->apiKeyService->createInitial($apiKey); - protected function execute(InputInterface $input, OutputInterface $output): int - { - $key = $input->getArgument('apiKey'); - $result = $this->apiKeyService->createInitial($key); - - if ($result === null && $output->isVerbose()) { - $output->writeln('Other API keys already exist. Initial API key creation skipped.'); + if ($result === null && $io->isVerbose()) { + $io->writeln('Other API keys already exist. Initial API key creation skipped.'); } return Command::SUCCESS; diff --git a/module/CLI/test/Command/Api/InitialApiKeyCommandTest.php b/module/CLI/test/Command/Api/InitialApiKeyCommandTest.php index e86cf0e5..b2311613 100644 --- a/module/CLI/test/Command/Api/InitialApiKeyCommandTest.php +++ b/module/CLI/test/Command/Api/InitialApiKeyCommandTest.php @@ -35,7 +35,7 @@ class InitialApiKeyCommandTest extends TestCase $this->apiKeyService->expects($this->once())->method('createInitial')->with('the_key')->willReturn($result); $this->commandTester->execute( - ['apiKey' => 'the_key'], + ['api-key' => 'the_key'], ['verbosity' => $verbose ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL], ); $output = $this->commandTester->getDisplay();