src/EventSubscriber/EasyAdminSubscriber.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Booking;
  4. use App\Entity\BookingConstraint;
  5. use App\Entity\User;
  6. use App\Entity\Profile;
  7. use Cocur\Slugify\Slugify;
  8. use App\Entity\HobbySingle;
  9. use App\Entity\ServiceSingle;
  10. use App\Entity\GalleryCategory;
  11. use App\Repository\BookingConstraintRepository;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  15. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  16. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  17. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  18. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  21. class EasyAdminSubscriber implements EventSubscriberInterface
  22. {
  23.     private $encoder;
  24.     public function __construct(UserPasswordHasherInterface $encoderSecurity $securitySessionInterface $sessionBookingConstraintRepository $constraintRepo)
  25.     {
  26.         $this->encoder $encoder;
  27.         $this->locales = ['fr''en''es''de'];
  28.         $this->security $security;
  29.         $this->session $session;
  30.         $this->constraintRepo $constraintRepo;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             BeforeEntityPersistedEvent::class => ['setOptionNew'],
  36.             BeforeEntityUpdatedEvent::class => ['setOptionEdit'],
  37.             AfterEntityPersistedEvent::class => ['afterPersist'],
  38.             AfterEntityUpdatedEvent::class => ['afterUpdate'],
  39.             AfterEntityDeletedEvent::class => ['afterDelete']
  40.         ];        
  41.     }
  42.     /**
  43.      * Permet de gerer un formulaire avant son envoi pour la création
  44.      *
  45.      * @param BeforeEntityPersistedEvent $event
  46.      * @return void
  47.      */
  48.     public function setOptionNew(BeforeEntityPersistedEvent $event)
  49.     {
  50.         $entity $event->getEntityInstance();
  51.         #   USER PASSWORD HASH
  52.         if ($entity instanceof User) {
  53.             $hash $this->encoder->hashPassword($entity$entity->getPassword());
  54.             $entity->setPassword($hash);
  55.         }
  56.         #   PROFILE USER
  57.         if ($entity instanceof Profile) {
  58.             $entity->setUser($this->security->getUser());
  59.         }
  60.         #   ITEM SLUG
  61.         if ($entity instanceof GalleryCategory || $entity instanceof ServiceSingle || $entity instanceof HobbySingle) {
  62.             $slugify = new Slugify();
  63.             if ($entity instanceof GalleryCategory) {
  64.                 foreach ($this->locales as $locale) {
  65.                     $entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getName()));
  66.                 }
  67.             }else {
  68.                 foreach ($this->locales as $locale) {
  69.                     $entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getTitle()));
  70.                 }
  71.             }
  72.             
  73.         }
  74.         #   BOOKING
  75.         if ($entity instanceof Booking) {
  76.             $entity
  77.                 ->setBookingConstraint($this->constraintRepo->findOneBy([]))
  78.                 ->setUniqId(uniqid("R"))
  79.                 ->setApproval(true)
  80.                 ->setRgpd(true)
  81.             ;
  82.         }
  83.         return;
  84.     }
  85.     /**
  86.      * Permet de gerer un formulaire avant son envoi pour la mise à jour
  87.      *
  88.      * @param BeforeEntityUpdatedEvent $event
  89.      */
  90.     public function setOptionEdit(BeforeEntityUpdatedEvent $event)
  91.     {
  92.         $entity $event->getEntityInstance();
  93.         if ($entity instanceof User) {
  94.             $hash $this->encoder->hashPassword($entity$entity->getPassword());
  95.             $entity->setPassword($hash);
  96.         }
  97.         if ($entity instanceof GalleryCategory || $entity instanceof ServiceSingle) {
  98.             $slugify = new Slugify();
  99.             if ($entity instanceof GalleryCategory) {
  100.                 foreach ($this->locales as $locale) {
  101.                     $entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getName()));
  102.                 }
  103.             }else {
  104.                 foreach ($this->locales as $locale) {
  105.                     $entity->translate($locale)->setSlug($slugify->slugify($entity->translate($locale)->getTitle()));
  106.                 }
  107.             }
  108.             
  109.         }
  110.         
  111.         return;
  112.     }
  113.     public function afterPersist(AfterEntityPersistedEvent $event): void
  114.     {
  115.         $this->session->getFlashBag()->add('success''Ajouté avec succes');
  116.     }
  117.     public function afterUpdate(AfterEntityUpdatedEvent $event): void
  118.     {
  119.         $this->session->getFlashBag()->add('success''Mis à jour avec succes');
  120.     }
  121.     public function afterDelete(AfterEntityDeletedEvent $event): void
  122.     {
  123.         $this->session->getFlashBag()->add('success','Supprimé avec succes');
  124.     }
  125. }