UpdateOptionsCommand.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Command;
  3. use App\Entity\Profile;
  4. use App\Repository\UserRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Console\Command\Command;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. class UpdateOptionsCommand extends Command
  12. {
  13. public function __construct(private EntityManagerInterface $em, private UserRepository $repoUser)
  14. {
  15. parent::__construct();
  16. }
  17. protected function configure(): void
  18. {
  19. $this
  20. ->setName('app:update:options')
  21. ->setDescription('Commande pour rattacher une entité Profile aux users existants')
  22. ->addOption('force', null, InputOption::VALUE_NONE, 'Forcer la création des profils utilisateur')
  23. ;
  24. }
  25. protected function execute(InputInterface $input, OutputInterface $output): int
  26. {
  27. $io = new SymfonyStyle($input, $output);
  28. if ($input->getOption('force')) {
  29. $users = $this->repoUser->findAll();
  30. foreach($users as $user) {
  31. if ($user->getProfile() === null)
  32. {
  33. $io->note(sprintf('%s : pas de profil', $user->getNomComplet()));
  34. $profil = new Profile();
  35. $profil->setUser($user);
  36. $this->em->persist($profil);
  37. }
  38. }
  39. $this->em->flush();
  40. $io->success(sprintf('La bdd a été modifiée.'));
  41. return Command::SUCCESS;
  42. } else {
  43. $io->note(sprintf('La bdd n\'a pas été modifiée. Utilisez l\'option --force.'));
  44. return Command::SUCCESS;
  45. }
  46. }
  47. }