askForClientName(); if ($clientName === null) { return self::FAILURE; } if (! $this->confirmCreation($clientName)) { $this->warn('Client creation cancelled.'); return self::SUCCESS; } $input = new Input($clientName); /** @var CreateClientView $view */ $view = UsecaseBuilder::using($executor) ->do($usecase) ->from($input) ->with(new CreateClientPresenter()) ->run(); $this->displayCredentials($view); return self::SUCCESS; } private function askForClientName(): ?ClientName { $maxAttempts = 3; for ($attempt = 1; $attempt <= $maxAttempts; $attempt++) { /** @var string|null $nameInput */ $nameInput = $this->ask('Client name (e.g. "Ones Branding website")'); $nameInput = $nameInput !== null ? trim($nameInput) : ''; if ($nameInput === '') { $this->error('Client name cannot be empty.'); continue; } try { return new ClientName($nameInput); } catch (\InvalidArgumentException $e) { $this->error($e->getMessage()); if ($attempt < $maxAttempts) { $this->warn(sprintf('You have %d attempt(s) remaining.', $maxAttempts - $attempt)); } } } $this->error('Maximum number of attempts reached. Aborting.'); return null; } private function confirmCreation(ClientName $name): bool { $this->newLine(); $this->info('Summary:'); $this->table( ['Field', 'Value'], [ ['Name', $name->value], ['Identifier', ''], ['Secret', ''], ['Status', 'Active'], ], ); return $this->confirm('Create this API client?', false); } private function displayCredentials(CreateClientView $view): void { $this->newLine(); $this->info($view->message); $this->newLine(); $this->table( ['Field', 'Value'], [ ['ID', $view->id], ['Name', $view->name], ['Identifier', $view->identifier], ['Secret', $view->secret], ], ); $this->newLine(); $this->warn('WARNING: Store the secret securely, it cannot be retrieved later.'); } }