apiKeyService = $apiKeyService; $this->roleResolver = $roleResolver; } protected function configure(): void { $authorOnly = RoleResolverInterface::AUTHOR_ONLY_PARAM; $domainOnly = RoleResolverInterface::DOMAIN_ONLY_PARAM; $help = <<%command.name% generates a new valid API key. %command.full_name% You can optionally set its expiration date with --expirationDate or -e: %command.full_name% --expirationDate 2020-01-01 You can also set roles to the API key: * Can interact with short URLs created with this API key: %command.full_name% --{$authorOnly} * Can interact with short URLs for one domain: %command.full_name% --{$domainOnly}=example.com * Both: %command.full_name% --{$authorOnly} --{$domainOnly}=example.com HELP; $this ->setName(self::NAME) ->setDescription('Generates a new valid API key.') ->addOption( 'expirationDate', 'e', InputOption::VALUE_REQUIRED, 'The date in which the API key should expire. Use any valid PHP format.', ) ->addOption( $authorOnly, 'a', InputOption::VALUE_NONE, sprintf('Adds the "%s" role to the new API key.', Role::AUTHORED_SHORT_URLS), ) ->addOption( $domainOnly, 'd', InputOption::VALUE_REQUIRED, sprintf('Adds the "%s" role to the new API key, with the domain provided.', Role::DOMAIN_SPECIFIC), ) ->setHelp($help); } protected function execute(InputInterface $input, OutputInterface $output): ?int { $expirationDate = $input->getOption('expirationDate'); $apiKey = $this->apiKeyService->create( isset($expirationDate) ? Chronos::parse($expirationDate) : null, ...$this->roleResolver->determineRoles($input), ); $io = new SymfonyStyle($input, $output); $io->success(sprintf('Generated API key: "%s"', $apiKey->toString())); if (! $apiKey->isAdmin()) { ShlinkTable::fromOutput($io)->render( ['Role name', 'Role metadata'], $apiKey->mapRoles(fn (string $name, array $meta) => [$name, arrayToString($meta, 0)]), null, 'Roles', ); } return ExitCodes::EXIT_SUCCESS; } }