Browse Source

Ajout d'une commande pour mettre à jour profils

François Drouhard 3 years ago
parent
commit
abd8a4540e
1 changed files with 71 additions and 0 deletions
  1. 71 0
      src/Command/UpdateOptionsCommand.php

+ 71 - 0
src/Command/UpdateOptionsCommand.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace App\Command;
+
+use App\Entity\Profile;
+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;
+
+#[AsCommand(
+    name: 'app:update:options',
+    description: 'Commande pour rattacher une entité Profile aux users existants',
+)]
+class UpdateOptionsCommand extends Command
+{
+    protected $em;
+    protected $repoUser;
+    
+    public function __construct(EntityManagerInterface $em, UserRepository $repoUser)
+    {
+        $this->em = $em;
+        $this->repoUser = $repoUser;
+        parent::__construct();
+    }
+
+    protected function configure(): void
+    {
+        $this
+            //->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
+            ->addOption('force', null, InputOption::VALUE_NONE, 'Forcer la création des profils utilisateur')
+        ;
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output): int
+    {
+        $io = new SymfonyStyle($input, $output);
+        /*$arg1 = $input->getArgument('arg1');
+
+        if ($arg1) {
+            $io->note(sprintf('You passed an argument: %s', $arg1));
+        }*/
+
+        if ($input->getOption('force')) {
+            $users = $this->repoUser->findAll();
+            foreach($users as $user) {
+                if ($user->getProfile() === null)
+                {
+                    $io->note(sprintf('%s : pas de profil', $user->getNomComplet()));
+                    $profil = new Profile;
+                    $profil->setUser($user);
+                    $this->em->persist($profil);
+                    $this->em->flush();
+                }
+            }
+
+
+            $io->success(sprintf('La bdd a été modifiée.'));
+            return Command::SUCCESS;
+        } else {
+            $io->note(sprintf('La bdd n\'a pas été modifiée. Utilisez l\'option --force.'));
+
+            return Command::SUCCESS;
+        }
+    }
+}