askForClient($output, $input); if (!$client) { $output->error('Client not found.'); return SCommand::FAILURE; } $output->info("Adding a new user for client: $client->name"); $emailQuestion = new QuestionInput('Enter the user\'s email: '); $emailQuestion->setValidator(function ($input): string { if (empty($input) || !filter_var($input, FILTER_VALIDATE_EMAIL)) { throw new \InvalidArgumentException('Invalid email address.'); } return $input; }); /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); $email = $helper->ask($input, $output, $emailQuestion); /** @var User| null $user */ $user = User::where('email', $email)->first(); if ($user) { $output->yellow('A user with this email already exists. Associating the user with the client.'); $clientUser = new ClientUser(); $clientUser->client_id = $client->id; $clientUser->user_id = $user->id; $clientUser->save(); return SCommand::SUCCESS; } $passwordQuestion = new QuestionInput('Enter the user\'s password: '); $passwordQuestion->setHidden(true); $passwordQuestion->setHiddenFallback(false); $password = $helper->ask($input, $output, $passwordQuestion); $firstNameQuestion = new QuestionInput('Enter the user\'s first name: '); $firstName = $helper->ask($input, $output, $firstNameQuestion); $lastNameQuestion = new QuestionInput('Enter the user\'s last name: '); $lastName = $helper->ask($input, $output, $lastNameQuestion); $createUserCommand = new CreateUser($client, $email, $password, $firstName, $lastName); /** @var User $user */ $user = CommandBus::handle($createUserCommand); $output->green('User added and associated with the client successfully.'); $output->info('User Details:'); $output->out("ID: $user->id"); $output->out("Email: $user->email"); $output->out("Name: $user->first_name $user->last_name"); return SCommand::SUCCESS; } }