|
@@ -0,0 +1,60 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Command;
|
|
|
+
|
|
|
+use App\Entity\User;
|
|
|
+use App\Repository\UserRepository;
|
|
|
+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\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
+use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
|
+use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
|
+
|
|
|
+
|
|
|
+ name: 'app:user:create',
|
|
|
+ description: 'Créer un nouvel utilisateur',
|
|
|
+)]
|
|
|
+class UserCreateCommand extends Command
|
|
|
+{
|
|
|
+ public function __construct(
|
|
|
+ protected readonly UserRepository $userRepository,
|
|
|
+ protected readonly EntityManagerInterface $em,
|
|
|
+ protected readonly UserPasswordHasherInterface $passwordHasher,
|
|
|
+ protected readonly ValidatorInterface $validator
|
|
|
+ )
|
|
|
+ {
|
|
|
+ parent::__construct();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
+ {
|
|
|
+ $io = new SymfonyStyle($input, $output);
|
|
|
+
|
|
|
+ $email = $io->ask("Email de l'utilisateur");
|
|
|
+ $plainPassword = $io->askHidden("Mot de passe");
|
|
|
+
|
|
|
+ $user = new User();
|
|
|
+ $user->setEmail($email);
|
|
|
+ $user->setPassword($this->passwordHasher->hashPassword($user, $plainPassword));
|
|
|
+
|
|
|
+ $errors = $this->validator->validate($user);
|
|
|
+ if (count($errors) > 0) {
|
|
|
+ $errorsString = (string) $errors;
|
|
|
+ $io->error($$errorsString);
|
|
|
+ return Command::INVALID;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->em->persist($user);
|
|
|
+ $this->em->flush();
|
|
|
+
|
|
|
+
|
|
|
+ $io->success('Le nouvel utilisateur a été créé');
|
|
|
+
|
|
|
+ return Command::SUCCESS;
|
|
|
+ }
|
|
|
+}
|