ldc-arcadia-api/src/Presentation/Console/Commands/RegenerateClientSecretCommand.php

168 lines
4.9 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Presentation\Console\Commands;
use Application\Security\Exceptions\ClientNotFoundException;
use Application\Security\Models\Entities\Client;
use Application\Security\Models\ValueObjects\ClientIdentifier;
use Application\Security\Usecases\RegenerateClientSecret\Input;
use Application\Security\Usecases\RegenerateClientSecret\RegenerateClientSecret;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Presentation\Console\Presenters\Clients\RegenerateClientSecretPresenter;
use Presentation\Console\Presenters\Clients\RegenerateClientSecretView;
use Presentation\Console\Services\ClientConsoleResolver;
use Shared\DDD\Usecase\Builder\UsecaseBuilder;
use Shared\DDD\Usecase\Builder\UsecaseExecutor;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\search;
final class RegenerateClientSecretCommand extends Command implements PromptsForMissingInput
{
/**
* @var string
*/
protected $signature = 'client:regenerate-secret
{identifier : The identifier of the client}';
/**
* @var string
*/
protected $description = 'Regenerate the secret of an API client';
public function __construct(
private readonly ClientConsoleResolver $clientResolver,
) {
parent::__construct();
}
/**
* @return array<string, \Closure(): (int|string)>
*/
#[\Override]
protected function promptForMissingArgumentsUsing(): array
{
return [
'identifier' => fn (): int | string => search(
label: 'Search client (by name, ID, identifier, or slug):',
placeholder: 'Type client name, identifier or slug...',
options: fn (string $value): array => $this->clientResolver->searchOptions($value),
scroll: 10,
),
];
}
#[\Override]
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
parent::complete($input, $suggestions);
if ($input->mustSuggestArgumentValuesFor('identifier')) {
$this->clientResolver->complete($suggestions);
}
}
public function handle(
RegenerateClientSecret $usecase,
UsecaseExecutor $executor,
): int {
$client = $this->resolveClient();
if ($client === null) {
return self::FAILURE;
}
if (! $this->confirmRegeneration($client)) {
$this->warn('Secret regeneration cancelled.');
return self::SUCCESS;
}
try {
$input = $this->getInput($client);
$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 getInput(Client $client): Input
{
return new Input($client->id);
}
private function resolveClient(): ?Client
{
$raw = $this->argument('identifier');
if (! is_string($raw) || trim($raw) === '') {
$this->error('No client specified.');
return null;
}
$inputString = trim($raw);
$client = $this->clientResolver->find($inputString);
if ($client !== null) {
return $client;
}
try {
new ClientIdentifier($inputString);
$this->error("Client '{$inputString}' not found.");
} catch (\InvalidArgumentException) {
$this->error("Invalid client identifier '{$inputString}'. Expected a valid client identifier.");
}
return null;
}
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 confirm(
label: 'Do you want to proceed?',
default: 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.');
}
}