src/EventSubscriber/EasyAdminParasolSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Parasol;
  4. use App\Entity\Rayon;
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use App\Service\ParasolService;
  11. class EasyAdminParasolSubscriber implements EventSubscriberInterface
  12. {
  13.     private $entityManager;
  14.     private $ps;
  15.     public function __construct(EntityManagerInterface $entityManager ParasolService $ps )
  16.     {
  17.         $this->entityManager $entityManager;
  18.         $this->ps $ps;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             BeforeEntityPersistedEvent::class => ['addParasol'],
  24.             BeforeEntityUpdatedEvent::class => ['updateParasol'], //surtout utile lors d'un reset de mot passe plutôt qu'un réel update, car l'update va de nouveau encrypter le mot de passe DEJA encrypté ...
  25.         ];
  26.     }
  27.     public function updateParasol(BeforeEntityUpdatedEvent $event)
  28.     {
  29.         $entity $event->getEntityInstance();
  30.         if (!($entity instanceof Parasol)) {
  31.             return;
  32.         }
  33.         $this->setFrontOrder($entity);
  34.     }
  35.     public function addParasol(BeforeEntityPersistedEvent $event)
  36.     {
  37.         $entity $event->getEntityInstance();
  38.         if (!($entity instanceof Parasol)) {
  39.             return;
  40.         }
  41.         $this->setFrontOrder($entity);
  42.     }
  43.     /**
  44.      * @param Parasol $entity
  45.      */
  46.     public function setFrontOrder(Parasol $entity): void
  47.     {
  48.         if ( $entity->getRayon() != null ) {
  49.             $res $this->ps->countLastPosition($entity->getRayon()->getId());
  50.             $entity->setFrontOrder($res[0][1]+1);
  51.         }
  52.         $this->entityManager->persist($entity);
  53.         $this->entityManager->flush();
  54.     }
  55. }