paramName(); $domainOnly = Role::DOMAIN_SPECIFIC->paramName(); $noOrphanVisits = Role::NO_ORPHAN_VISITS->paramName(); $help = <<%command.name% generates a new valid API key. %command.full_name% You can optionally set its name for tracking purposes with --name or -m: %command.full_name% --name Alice You can optionally set its expiration date with --expiration-date or -e: %command.full_name% --expiration-date 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 * Cannot see orphan visits: %command.full_name% --{$noOrphanVisits} * All: %command.full_name% --{$authorOnly} --{$domainOnly}=example.com --{$noOrphanVisits} HELP; $this ->setName(self::NAME) ->setDescription('Generate a new valid API key.') ->addOption( 'name', 'm', InputOption::VALUE_REQUIRED, 'The name by which this API key will be known.', ) ->addOption( 'expiration-date', '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->value), ) ->addOption( $domainOnly, 'd', InputOption::VALUE_REQUIRED, sprintf( 'Adds the "%s" role to the new API key, with the domain provided.', Role::DOMAIN_SPECIFIC->value, ), ) ->addOption( $noOrphanVisits, 'o', InputOption::VALUE_NONE, sprintf('Adds the "%s" role to the new API key.', Role::NO_ORPHAN_VISITS->value), ) ->setHelp($help); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $expirationDate = $input->getOption('expiration-date'); $apiKeyMeta = ApiKeyMeta::fromParams( name: $input->getOption('name'), expirationDate: isset($expirationDate) ? Chronos::parse($expirationDate) : null, roleDefinitions: $this->roleResolver->determineRoles($input), ); $apiKey = $this->apiKeyService->create($apiKeyMeta); $io->success(sprintf('Generated API key: "%s"', $apiKeyMeta->key)); if ($input->isInteractive()) { $io->warning('Save the key in a secure location. You will not be able to get it afterwards.'); } if (! ApiKey::isAdmin($apiKey)) { ShlinkTable::default($io)->render( ['Role name', 'Role metadata'], $apiKey->mapRoles(fn (Role $role, array $meta) => [$role->value, arrayToString($meta, indentSize: 0)]), headerTitle: 'Roles', ); } return ExitCode::EXIT_SUCCESS; } }