src/EventSubscriber/CombiSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Combi;
  4. use App\Entity\CombiVariable;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  12. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  13. use Flasher\SweetAlert\Prime\SweetAlertFactory;
  14. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  15. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  16. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  20. use Drupal\Core\Cache\CacheableRedirectResponse;
  21. use Symfony\Component\HttpKernel\Event\RequestEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. //use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  24. class CombiSubscriber implements EventSubscriberInterface
  25. {
  26.     private $entityManager;
  27.     private ManagerRegistry $manager;
  28.     public function __construct(
  29.     
  30.         ManagerRegistry $manager,
  31.         EntityManagerInterface $entityManager
  32.     ) {
  33.         
  34.         $this->manager $manager;
  35.         $this->entityManager $entityManager
  36.     }
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             AfterEntityPersistedEvent::class => ['afterAdd']
  41.           
  42.         ];
  43.     }
  44.     public function afterAdd(AfterEntityPersistedEvent $event)
  45.     {
  46.        
  47.         $entity $event->getEntityInstance();
  48.         if ($entity instanceof Combi) {
  49.         $combis=$entity->getProjet()->getCombis();
  50.         $count=0;
  51.         foreach($combis as $combi){
  52.              if(strtolower($combi->getTitle()) == strtolower($entity->getTitle())){
  53.                 $count++;
  54.             }
  55.           }
  56.         if ($count 1) {
  57.             $this->entityManager->remove($entity);
  58.          }
  59.             $this->entityManager->flush();
  60.         
  61.          }
  62.          } 
  63.          
  64.    
  65.     
  66.     }