src/EventSubscriber/EasyAdminSubscriber.php line 186

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\CarteCadeau;
  4. use App\Entity\Genres;
  5. use App\Entity\PrixLitteraire;
  6. use App\Entity\Produit;
  7. use App\Entity\Promotion;
  8. use App\Entity\Publics;
  9. use App\Entity\PublicsJeunesse;
  10. use App\Entity\Selection;
  11. use App\Entity\Serie;
  12. use App\Entity\SousGenres;
  13. use App\Entity\Theme;
  14. use App\Entity\Tier;
  15. use App\Entity\Transaction;
  16. use App\Entity\User;
  17. use App\Entity\UserProduits;
  18. use App\Entity\UserWishlists;
  19. use App\Entity\Version;
  20. use App\Exception\BunnyCDNStorageException;
  21. use App\Service\AirTableService;
  22. use App\Service\BunnyManager;
  23. use App\Service\StringManager;
  24. use Doctrine\ORM\EntityManagerInterface;
  25. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  26. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  27. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  28. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  29. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  30. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  31. use Symfony\Component\String\Slugger\AsciiSlugger;
  32. use Psr\Log\LoggerInterface;
  33. class EasyAdminSubscriber implements EventSubscriberInterface
  34. {
  35.   private $em;
  36.   private $bunnyManager;
  37.   private $airTableService;
  38.   private $passwordEncoder;
  39.   private $stringManager;
  40.   private $logger;
  41.   /**
  42.    * @param EntityManagerInterface $em
  43.    * @param BunnyManager $bunnyManager
  44.    * @param AirTableService $airTableService
  45.    * @param UserPasswordHasherInterface $passwordEncoder
  46.    * @param StringManager $stringManager
  47.    * @param LoggerInterface $logger
  48.    */
  49.   public function __construct(
  50.     EntityManagerInterface      $em,
  51.     BunnyManager                $bunnyManager,
  52.     AirTableService             $airTableService,
  53.     UserPasswordHasherInterface $passwordEncoder,
  54.     StringManager               $stringManager,
  55.     LoggerInterface             $logger
  56.   )
  57.   {
  58.     $this->em $em;
  59.     $this->bunnyManager $bunnyManager;
  60.     $this->airTableService $airTableService;
  61.     $this->passwordEncoder $passwordEncoder;
  62.     $this->stringManager $stringManager;
  63.     $this->logger $logger;
  64.   }
  65.   /**
  66.    * @return string[][]
  67.    */
  68.   public static function getSubscribedEvents(): array
  69.   {
  70.     return [
  71.       BeforeEntityPersistedEvent::class => ['add'],
  72.       AfterEntityPersistedEvent::class => ['import'],
  73.       BeforeEntityUpdatedEvent::class => ['update'],
  74.       AfterEntityUpdatedEvent::class => ['importAfter'],
  75.     ];
  76.   }
  77.   /**
  78.    * @param BeforeEntityPersistedEvent $event
  79.    * @return void
  80.    */
  81.   public function add(BeforeEntityPersistedEvent $event): void
  82.   {
  83.     $entity $event->getEntityInstance();
  84.     if ($entity instanceof Tier) {
  85.       $prenom $entity->getPrenom();
  86.       $nom $entity->getNom();
  87.       $slugger = new AsciiSlugger();
  88.       if ($prenom && $nom) {
  89.         $alias $slugger->slug($prenom '-' $nom);
  90.       } elseif ($prenom) {
  91.         $alias $slugger->slug($prenom);
  92.       } elseif ($nom) {
  93.         $alias $slugger->slug($nom);
  94.       } else {
  95.         $alias '';
  96.       }
  97.       $entity->setAlgoliaName(strtolower($alias))
  98.         ->setDateUpdate(new \DateTime())
  99.         ->setDateCreation(new \DateTime());
  100.     } else if ($entity instanceof Selection) {
  101.       $entity->setDateUpdate(new \DateTime)
  102.         ->setDateCreated(new \DateTime);
  103.     } else if ($entity instanceof User) {
  104.       $this->setPassword($entity);
  105.     } else if ($entity instanceof Serie) {
  106.       $entity->setDateCreation(new \DateTime);
  107.       $entity->setSlug($this->stringManager->formatAlias($entity->getTitre()));
  108.     } else if ($entity instanceof Produit) {
  109.       $entity->setDateCreation(new \DateTime);
  110.       $this->airTableService->addProduct($entity);
  111.     } else if ($entity instanceof PrixLitteraire) {
  112.       $entity->setDateUpdate(new \DateTime);
  113.     } else if ($entity instanceof Genres) {
  114.       $entity->setDateUpdate(new \DateTime);
  115.     } else if ($entity instanceof SousGenres) {
  116.       $entity->setDateUpdate(new \DateTime);
  117.     } else if ($entity instanceof Publics) {
  118.       $entity->setDateUpdate(new \DateTime);
  119.     } else if ($entity instanceof PublicsJeunesse) {
  120.       $entity->setDateUpdate(new \DateTime);
  121.     } else if ($entity instanceof Theme) {
  122.       $entity->setDateUpdate(new \DateTime);
  123.     } else if ($entity instanceof Version) {
  124.       $entity->setDateUpdate(new \DateTime);
  125.     } else if ($entity instanceof CarteCadeau) {
  126.       $entity->setDateCreation(new \DateTime);
  127.     } else if ($entity instanceof Promotion) {
  128.       $this->addProductToPromotion($entity);
  129.     }
  130.   }
  131.   /**
  132.    * @param BeforeEntityUpdatedEvent $event
  133.    * @return void
  134.    */
  135.   public function update(BeforeEntityUpdatedEvent $event): void
  136.   {
  137.     $entity $event->getEntityInstance();
  138.     if ($entity instanceof Tier) {
  139.       $entity->setDateUpdate(new \DateTime);
  140.     } else if ($entity instanceof Selection) {
  141.       $entity->setDateUpdate(new \DateTime);
  142.     } else if ($entity instanceof User) {
  143.       $this->setPassword($entity);
  144.     } else if ($entity instanceof Serie) {
  145.       $entity->setDateUpdate(new \DateTime);
  146.       $entity->setSlug($this->stringManager->formatAlias($entity->getTitre()));
  147.     } else if ($entity instanceof Produit) {
  148.       $entity->setDateUpdate(new \DateTime);
  149.       $this->airTableService->editProduct($entity);
  150.     } else if ($entity instanceof PrixLitteraire) {
  151.       $entity->setDateUpdate(new \DateTime);
  152.     } else if ($entity instanceof Genres) {
  153.       $entity->setDateUpdate(new \DateTime);
  154.     } else if ($entity instanceof SousGenres) {
  155.       $entity->setDateUpdate(new \DateTime);
  156.     } else if ($entity instanceof Publics) {
  157.       $entity->setDateUpdate(new \DateTime);
  158.     } else if ($entity instanceof PublicsJeunesse) {
  159.       $entity->setDateUpdate(new \DateTime);
  160.     } else if ($entity instanceof Theme) {
  161.       $entity->setDateUpdate(new \DateTime);
  162.     } else if ($entity instanceof Version) {
  163.       $entity->setDateUpdate(new \DateTime);
  164.     } else if ($entity instanceof Transaction) {
  165.       $this->updateTransaction($entity);
  166.     } elseif ($entity instanceof Promotion) {
  167.       $this->addProductToPromotion($entity);
  168.     }
  169.   }
  170.   /**
  171.    * Import les fichiers d'un produit dans bunny CDN
  172.    * après son enregistrement en base de données.
  173.    *
  174.    * @param AfterEntityPersistedEvent $event
  175.    * @return void
  176.    * @throws BunnyCDNStorageException
  177.    */
  178.   public function import(AfterEntityPersistedEvent $event): void
  179.   {
  180.     $entity $event->getEntityInstance();
  181.     if ($entity instanceof Produit) {
  182.       if ($entity->getImageHdName()) {
  183.         $this->bunnyManager->importFileToBunnyCdn($entity->getImageHdName(), 'hd');
  184.       }
  185.     }
  186.     if ($entity instanceof Tier) {
  187.       if ($entity->getImageName()) {
  188.         $this->bunnyManager->importFileToBunnyCdnTiers($entity->getImageName());
  189.       }
  190.     }
  191.   }
  192.   /**
  193.    * Import les fichiers d'un produit dans bunny CDN
  194.    * après son enregistrement en base de données.
  195.    *
  196.    * @param AfterEntityUpdatedEvent $event
  197.    * @return void
  198.    * @throws BunnyCDNStorageException
  199.    */
  200.   public function importAfter(AfterEntityUpdatedEvent $event): void
  201.   {
  202.     $entity $event->getEntityInstance();
  203.     if ($entity instanceof Produit) {
  204.       if ($entity->getImageHdName()) {
  205.         $this->bunnyManager->importFileToBunnyCdn($entity->getImageHdName(), 'hd');
  206.       }
  207.     }
  208.     if ($entity instanceof Tier) {
  209.       if ($entity->getImageName()) {
  210.         $this->bunnyManager->importFileToBunnyCdnTiers($entity->getImageName());
  211.       }
  212.     }
  213.   }
  214.   public function updateTransaction($entity): void
  215.   {
  216.     if (($entity->getStatus()->getId() == 2) ||
  217.       ($entity->getStatus()->getId() == 3) ||
  218.       ($entity->getStatus()->getId() == 4)) {
  219.       foreach ($entity->getTransactionItems() as $item) {
  220.         if ($item->getProduit() != null) {
  221.           $produit $item->getProduit();
  222.           $uproduit $this->em->getRepository(UserProduits::class)->findOneBy(array(
  223.             'user' => $entity->getClient()->getId(),
  224.             'produit' => $produit->getId()
  225.           ));
  226.           if ($uproduit != null) {
  227.             $this->em->remove($uproduit);
  228.           }
  229.         } else {
  230.           $carte $item->getCartecadeau();
  231.           $carte->setEtat('invalide');
  232.           $this->em->persist($carte);
  233.         }
  234.       }
  235.       $user $entity->getClient();
  236.       $user->setFidelite($user->getFidelite() - $entity->getPointsFidelite());
  237.       $this->em->persist($user);
  238.       $this->em->flush();
  239.     } else if ($entity->getStatus()->getId() == 1) {
  240.       foreach ($entity->getTransactionItems() as $item) {
  241.         if ($item->getProduit() != null) {
  242.           $produit $item->getProduit();
  243.           $uproduit $this->em->getRepository(UserProduits::class)->findOneBy(array(
  244.             'user' => $entity->getClient()->getId(),
  245.             'produit' => $produit->getId()
  246.           ));
  247.           if ($uproduit == null) {
  248.             $uproduit = new UserProduits;
  249.             $user $entity->getClient();
  250.             $uproduit->setDateActivation(new \DateTime)
  251.               ->setProduit($produit)
  252.               ->setUser($user)
  253.               ->setNbEcouteStream(0)
  254.               ->setNbTelechargementPartiel(0)
  255.               ->setNbTelechargementZip(0);
  256.             $this->em->persist($uproduit);
  257.             $productsWishlist $this->em->getRepository(UserWishlists::class)->findOneBy(array('produit' => $produit'user' => $user));
  258.             if ($productsWishlist != null) {
  259.               $this->em->remove($productsWishlist);
  260.             }
  261.           }
  262.         }
  263.       }
  264.       $this->em->flush();
  265.     }
  266.   }
  267.   /**
  268.    * @param User $user
  269.    * @return void
  270.    */
  271.   public function setPassword(User $user): void
  272.   {
  273.     $plainPassword $user->getPlainPassword();
  274.     if ($plainPassword) {
  275.       $user->setPassword(
  276.         $this->passwordEncoder->hashPassword(
  277.           $user,
  278.           $plainPassword
  279.         )
  280.       );
  281.       $this->em->flush();
  282.     }
  283.   }
  284.   /**
  285.    * @param Promotion $entity
  286.    * @return void
  287.    */
  288.   private function addProductToPromotion(Promotion $entity): void
  289.   {
  290.     if (str_contains($entity->getProductStrings(), ';')) {
  291.       $productLists explode(';'$entity->getProductStrings());
  292.     } else {
  293.       $productLists = [$entity->getProductStrings()];
  294.     }
  295.     if ($entity->getProduits()) {
  296.       foreach ($entity->getProduits() as $produit) {
  297.         $entity->removeProduit($produit);
  298.       }
  299.       $this->em->flush();
  300.     }
  301.     foreach ($productLists as $productList) {
  302.       if ($product $this->em->getRepository(Produit::class)->findOneBy(['id' => $productList])) {
  303.         $entity->addProduit($product);
  304.       }
  305.     }
  306.     $this->em->flush();
  307.   }
  308. }