add app:create-user

This commit is contained in:
dece 2024-08-31 23:16:56 +02:00
parent 45ea878f17
commit 3d355039fe

View file

@ -0,0 +1,51 @@
<?php
namespace App\Command;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
#[AsCommand(
name: 'app:create-user',
description: 'Create a new user from the command-line.',
)]
class CreateUserCommand extends Command
{
public function __construct(
protected EntityManagerInterface $em,
protected UserPasswordHasherInterface $passwordHasher,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('email', InputArgument::REQUIRED, 'User email address, used to login')
->addArgument('password', InputArgument::REQUIRED, 'User password')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$email = $input->getArgument('email');
$password = $input->getArgument('password');
$user = new User();
$user->setEmail($email);
$user->setPassword($this->passwordHasher->hashPassword($user, $password));
$this->em->persist($user);
$this->em->flush();
$io->success('User created.');
return Command::SUCCESS;
}
}