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); } if ($input->mustSuggestArgumentValuesFor('status')) { $suggestions->suggestValues([ new Suggestion('activate', 'Activate the client'), new Suggestion('deactivate', 'Deactivate the client'), ]); } } public function handle( SetClientStatus $usecase, FindClient $findClient, UsecaseExecutor $executor, ): int { $identifierString = $this->resolveIdentifier($findClient, $executor); if ($identifierString === null) { return self::FAILURE; } try { $clientIdentifier = $this->parseClientIdentifier($identifierString, $findClient, $executor); $desiredStatus = $this->resolveDesiredStatus($clientIdentifier); $input = new Input($clientIdentifier, $desiredStatus); $view = UsecaseBuilder::using($executor) ->do($usecase) ->from($input) ->with(new SetClientStatusPresenter()) ->run(); $this->info($view->message); return self::SUCCESS; } catch (ClientNotFoundException | \InvalidArgumentException $e) { $this->error($e->getMessage()); return self::FAILURE; } } private function resolveIdentifier(FindClient $findClient, UsecaseExecutor $executor): ?string { $raw = $this->argument('identifier'); if ($raw !== null) { return (string) $raw; } /** @var string|null $answer */ $answer = $this->anticipate( 'Search client (by name, ID, identifier, or slug):', function (string $input) use ($findClient, $executor): array { $trimmed = trim($input); if ($trimmed === '') { return []; } try { /** @var FindClientView $view */ $view = UsecaseBuilder::using($executor) ->do($findClient) ->from(new FindClientInput($trimmed)) ->with(new FindClientPresenter()) ->run(); return array_map( static fn (FoundClientItem $item): string => $item->identifier, $view->clients, ); } catch (\Throwable) { return []; } } ); if ($answer === null || trim($answer) === '') { $this->error('No client specified.'); return null; } return trim($answer); } /** * @throws \InvalidArgumentException */ private function parseClientIdentifier( string $value, FindClient $findClient, UsecaseExecutor $executor, ): ClientIdentifier { try { return new ClientIdentifier($value); } catch (\InvalidArgumentException) { try { /** @var FindClientView $view */ $view = UsecaseBuilder::using($executor) ->do($findClient) ->from(new FindClientInput($value)) ->with(new FindClientPresenter()) ->run(); if (count($view->clients) === 1) { return new ClientIdentifier($view->clients[0]->identifier); } if (count($view->clients) > 1) { foreach ($view->clients as $found) { if ( strcasecmp($found->name, $value) === 0 || strcasecmp($found->id, $value) === 0 ) { return new ClientIdentifier($found->identifier); } } } } catch (\Throwable) { } throw new \InvalidArgumentException("Invalid client identifier '{$value}'. Expected a valid client identifier."); } } private function resolveDesiredStatus(ClientIdentifier $clientIdentifier): bool { $status = $this->argument('status') !== null ? (string) $this->argument('status') : null; return match ($status) { 'activate' => true, 'deactivate' => false, null => $this->resolveToggledStatus($clientIdentifier), default => throw new \InvalidArgumentException("Invalid status '{$status}'. Expected 'activate' or 'deactivate'."), }; } private function resolveToggledStatus(ClientIdentifier $clientIdentifier): bool { $client = $this->clientRepository->findByIdentifier($clientIdentifier) ?? throw ClientNotFoundException::forClient($clientIdentifier->value); return ! $client->isActive; } }