Files
php-auth/src/Cli/Commands/User/ResetPassword.php
2026-01-29 22:34:15 -05:00

79 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Siteworxpro\App\Cli\Commands\User;
use Siteworxpro\App\Cli\ClimateOutput;
use Siteworxpro\App\Cli\Commands\Command;
use Siteworxpro\App\CommandBus\Commands\SendPasswordReset;
use Siteworxpro\App\Mailer\Message;
use Siteworxpro\App\Services\Facades\CommandBus;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Question\Question as QuestionInput;
#[AsCommand('user:reset-password', 'Reset a user\'s password')]
class ResetPassword extends Command
{
protected function configure(): void
{
$this->addOption(
'send-email',
's',
InputOption::VALUE_NONE,
description: 'Send password reset email to the user'
);
}
public function __invoke($input, $output): int
{
$client = $this->askForClient($output, $input);
if (!$client) {
$output->error('Client not found.');
return \Symfony\Component\Console\Command\Command::FAILURE;
}
$user = $this->askForUser($client, $output, $input);
if (!$user) {
$output->error('User not found for the specified client.');
return \Symfony\Component\Console\Command\Command::FAILURE;
}
if ($input->getOption('send-email')) {
/** @var Message $message */
$message = CommandBus::handle(new SendPasswordReset($user, $client));
$output->info('Password reset email sent to the user.');
$output->info('Email Subject: ' . $message->getSubject());
$output->info('Email Body: ' . $message->getBody());
return \Symfony\Component\Console\Command\Command::SUCCESS;
}
/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');
$question = new QuestionInput('Enter the new password: ');
$question->setValidator(function ($input): string {
if (empty($input) || strlen($input) < 8) {
throw new \InvalidArgumentException('Password must be at least 8 characters long.');
}
return $input;
});
$question->setHidden(true);
$question->setHiddenFallback(false);
$newPassword = $helper->ask($input, $output, $question);
$user->password = $newPassword;
$user->save();
$output->info('Password has been reset successfully.');
return \Symfony\Component\Console\Command\Command::SUCCESS;
}
}