AppUsersActivateCommand.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Command;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\Console\Attribute\AsCommand;
  6. use Symfony\Component\Console\Command\Command;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. use Symfony\Component\Console\Question\ConfirmationQuestion;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. #[AsCommand(
  12. name: 'app:users:activate',
  13. description: 'Activer tous les users',
  14. )]
  15. class AppUsersActivateCommand extends Command
  16. {
  17. public function __construct(private UserRepository $repoUser, private EntityManagerInterface $em)
  18. {
  19. parent::__construct();
  20. }
  21. protected function execute(InputInterface $input, OutputInterface $output) :int
  22. {
  23. $io = new SymfonyStyle($input, $output);
  24. $question = new ConfirmationQuestion('Activer tous les utilisateurs ?', false);
  25. if (! $io->askQuestion($question))
  26. {
  27. $io->warning("Pas d'activation des users.");
  28. return COMMAND::INVALID;
  29. }
  30. $users = $this->repoUser->findAll();
  31. foreach ($users as $user)
  32. {
  33. $user->setActivated(true);
  34. }
  35. $this->em->flush();
  36. $io->success('Les utilisateurs sont activés');
  37. return COMMAND::SUCCESS;
  38. }
  39. }