src/EventSubscriber/rankingSubscriber.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Commune;
  4. use App\Entity\Province;
  5. use App\Entity\Ranking;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  9. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  11. use mysql_xdevapi\Exception;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class rankingSubscriber implements EventSubscriberInterface
  14. {
  15.     private $entityManager;
  16.     public function __construct(EntityManagerInterface $entityManager)
  17.     {
  18.         $this->entityManager $entityManager;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             BeforeEntityPersistedEvent::class => ['setCommunes'],
  24.             BeforeEntityUpdatedEvent::class => ['updateCommune']
  25.         ];
  26.     }
  27.     public function setCommunes(BeforeEntityPersistedEvent $event)
  28.     {
  29.         try{
  30.             $entity $event->getEntityInstance();
  31.             if (( $entity instanceof Ranking )) {
  32.                 if (count($entity->getProvince()) >= 1) {
  33.                     $provinces $entity->getProvince();
  34.                     $communees $entity->getCommune();
  35.                     $ids = [];
  36.                     $comm=[];
  37.                     if (count($communees) >= 1) {
  38.                         foreach ($communees as $c) {
  39.                             $commune $this->entityManager->getRepository(Commune::class)->find($c);
  40.                             array_push($comm$commune);
  41.                         }
  42.                     }
  43.                     foreach ($provinces as $p) {
  44.                         $communes $this->entityManager->getRepository(Commune::class)->findBy(["idProvince" => $p->getId()]);
  45.                         foreach ($communes as $c) {
  46.                             $commune $this->entityManager->getRepository(Commune::class)->find($c);
  47.                             array_push($ids$commune);
  48.                         }
  49.                         $result array_unique(array_merge($ids$comm));
  50.                         $entity->setCommune($result);
  51.                         $this->entityManager->persist($entity);
  52.                         $this->entityManager->flush();
  53.                     }
  54.                 }
  55.             }
  56.         } catch (\Doctrine\ORM\ORMException $e){
  57.             return $e;
  58.         }
  59.     }
  60.     public function updateCommune(BeforeEntityUpdatedEvent $event)
  61.     {
  62.         try {
  63.             $entity $event->getEntityInstance();
  64.             if (($entity instanceof Ranking)) {
  65.                 if (count($entity->getProvince()) >= 1) {
  66.                     $provinces $entity->getProvince();
  67.                     $communees $entity->getCommune();
  68.                     $ids = [];
  69.                     $comm=[];
  70.                     if (count($communees) >= 1) {
  71.                         foreach ($communees as $c) {
  72.                             $commune $this->entityManager->getRepository(Commune::class)->find($c);
  73.                             array_push($comm$commune);
  74.                         }
  75.                     }
  76.                     foreach ($provinces as $p) {
  77.                         $communes $this->entityManager->getRepository(Commune::class)->findBy(["idProvince" => $p->getId()]);
  78.                         foreach ($communes as $c) {
  79.                             $commune $this->entityManager->getRepository(Commune::class)->find($c);
  80.                             array_push($ids$commune);
  81.                         }
  82.                         //  $result = array_merge($ids, $comm);
  83.                         $result =array_unique(array_merge($ids$comm), SORT_REGULAR);
  84.                         $entity->setCommune($result);
  85.                         $this->entityManager->persist($entity);
  86.                         $this->entityManager->flush();
  87.                     }
  88.                 }
  89.             }
  90.         } catch (\Doctrine\ORM\ORMException $e) {
  91.             return $e;
  92.         }
  93.     }
  94. }