src/Command/DeleteSiteCommand.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Command;
  3. use App\Entity\User;
  4. use App\Repository\AffectationRepository;
  5. use App\Repository\CustomerRepository;
  6. use App\Repository\SiteRepository;
  7. use App\Services\Email\SendEmail;
  8. use App\Services\Exception\WriterException;
  9. use App\Services\SiteService;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Exception;
  12. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  13. use Symfony\Component\Console\Attribute\AsCommand;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. use Symfony\Component\Console\Style\SymfonyStyle;
  20. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  21. use Symfony\Component\Mailer\MailerInterface;
  22. use Symfony\Component\Mime\Address;
  23. #[AsCommand(
  24.     name'app:delete:entity-site',
  25.     description'this command will delete sites',
  26. )]
  27. class DeleteSiteCommand extends Command
  28. {
  29.     public function __construct(
  30.         private readonly SiteRepository        $siteRepository,
  31.         private readonly SiteService           $siteService,
  32.         private readonly AffectationRepository $affectationRepository,
  33.         private MailerInterface $mailer,
  34.         private ParameterBagInterface $params,
  35.         private SendEmail $sendEmail,
  36.         private ?EntityManagerInterface  $em null,
  37.         private readonly CustomerRepository        $customerRepository,
  38.     )
  39.     {
  40.         parent::__construct();
  41.     }
  42.     protected function execute(InputInterface $inputOutputInterface $output): int
  43.     {
  44.         $io = new SymfonyStyle($input$output);
  45.         $io->info('START');
  46.         $sites $this->siteRepository->getAllSitesByCustomer(411);
  47.         if (count($sites) > 0) {
  48.             $sites array_column($sites'id');
  49.             foreach ($sites as $id) {
  50.                 try {
  51.                     $all_site array_reverse(array_column($this->customerRepository->withRecursiveGetFilsOfEntity($id), 'id'));
  52.                     foreach ($all_site as $site){
  53.                         $this->siteRepository->deleteSite($site);
  54.                     }
  55.                 } catch (Exception $e) {
  56.                     $from = new Address($this->params->get('mailer_from'), $this->params->get('mailer_from_name'));
  57.                     $to = new Address('contact@novalia.online''contact_test');
  58.                     $subject 'Erreur de suppression du site';
  59.                     $template 'exception-delete-site.html.twig';
  60.                     $context = [
  61.                         'id_site' => $id,
  62.                         'error' => $e->getMessage()
  63.                     ];
  64.                     $this->sendEmail->sendExceptionEmail($from$to$subject$template$context);
  65.                 }
  66.             }
  67.         }
  68.         $io->success('Done!');
  69.         return Command::SUCCESS;
  70.     }
  71. }