mustSuggestArgumentValuesFor('identifier')) { $clients = $this->clientRepository->findAll(Pagination::ofOffsetAndLimit(0, 100)); /** @var list $items */ $items = array_map( static fn (Client $client): Suggestion => new Suggestion( value: $client->identifier->value, description: sprintf('%s (%s)', $client->name->value, $client->isActive ? 'Active' : 'Inactive'), ), $clients->getItems(), ); $suggestions->suggestValues($items); } } public function handle( RegenerateClientSecret $usecase, UsecaseExecutor $executor, ): int { $identifierString = $this->resolveIdentifier(); if ($identifierString === null) { return self::FAILURE; } try { $clientIdentifier = $this->parseClientIdentifier($identifierString); $client = $this->clientRepository->findByIdentifier($clientIdentifier); if ($client === null) { $this->error(sprintf("Client '%s' not found.", $clientIdentifier->value)); return self::FAILURE; } if (! $this->confirmRegeneration($client)) { $this->warn('Secret regeneration cancelled.'); return self::SUCCESS; } $input = new Input($client->id); /** @var RegenerateClientSecretView $view */ $view = UsecaseBuilder::using($executor) ->do($usecase) ->from($input) ->with(new RegenerateClientSecretPresenter()) ->run(); $this->displayResult($client, $view); return self::SUCCESS; } catch (ClientNotFoundException | \InvalidArgumentException $e) { $this->error($e->getMessage()); return self::FAILURE; } } private function resolveIdentifier(): ?string { $raw = $this->argument('identifier'); if ($raw !== null) { return (string) $raw; } $clients = $this->clientRepository->findAll(Pagination::ofOffsetAndLimit(0, 50)); $items = $clients->getItems(); if ($items === []) { $this->error('No clients found in the database.'); return null; } $choices = []; foreach ($items as $client) { $choices[$client->identifier->value] = sprintf( '%s (%s) - %s', $client->name->value, $client->isActive ? 'Active' : 'Inactive', $client->identifier->value, ); } $selectedDisplay = $this->choice('Which client secret would you like to regenerate?', array_values($choices)); return (string) array_search($selectedDisplay, $choices, true); } /** * @throws \InvalidArgumentException */ private function parseClientIdentifier(string $value): ClientIdentifier { try { return new ClientIdentifier($value); } catch (\InvalidArgumentException) { throw new \InvalidArgumentException("Invalid client identifier '{$value}'. Expected a valid client identifier."); } } private function confirmRegeneration(Client $client): bool { $this->newLine(); $this->warn(sprintf("Regenerating the secret will immediately invalidate the existing secret for client '%s'.", $client->name->value)); return $this->confirm('Do you want to proceed?', false); } private function displayResult(Client $client, RegenerateClientSecretView $view): void { $this->newLine(); $this->info($view->message); $this->newLine(); $this->table( ['Field', 'Value'], [ ['ID', $client->id->value], ['Name', $client->name->value], ['Identifier', $view->identifier], ['New Secret', $view->secret], ], ); $this->newLine(); $this->warn('WARNING: Store the new secret securely, it cannot be retrieved later.'); } }