src/EventSubscriber/EasyAdminSubscriber.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Classes\Tools;
  4. use App\Entity\Presta\PsCustomer;
  5. use App\Entity\Presta\PsLoyalty;
  6. use App\Entity\User;
  7. use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
  8. use Doctrine\ORM\EntityManager;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  16. class EasyAdminSubscriber extends AbstractController implements EventSubscriberInterface
  17. {
  18.     private $entityManager;
  19.     private $passwordEncoder;
  20.     public function __construct(EntityManagerInterface $entityManagerUserPasswordEncoderInterface $passwordEncoder)
  21.     {
  22.         $this->entityManager $entityManager;
  23.         $this->passwordEncoder $passwordEncoder;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             AfterEntityUpdatedEvent::class => ['addPoints'],
  29.             BeforeEntityPersistedEvent::class => ['addUser'],
  30.             BeforeEntityUpdatedEvent::class => ['updateUser'], //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é ...
  31.         ];
  32.     }
  33.     public function addPoints(AfterEntityUpdatedEvent $event){
  34.         $idCustomer $event->getEntityInstance();
  35.         if (($idCustomer instanceof PsCustomer)) {
  36.             //$idCustomer_ = (int)$idCustomer->getIdCustomer();
  37.             $idCustomer_ = (int)$idCustomer->getEnovaId();
  38.             $aktywny $idCustomer->getAktywnyCms();
  39.             $idEnova_ = (int)$idCustomer->getEnovaId();
  40.             if ($idEnova_ != and $aktywny !== false) {
  41.                 
  42.                 $tools = new Tools();
  43.                 $points_ = (int)($tools->getPointsPremium($idEnova_));
  44.                 $entityManager $this->getDoctrine()->getManager('presta');
  45.                 $ly $entityManager->getRepository('Presta:PsLoyalty');
  46.                 $po $ly->findOneBy([ 'idCustomer'=>$idCustomer_ 'fvEnovaNr' => 'BILANS']);
  47.                 if ($po == null) {
  48.                     $loyalty = new PsLoyalty();
  49.                     $loyalty->setIdCartRule(0);
  50.                     $loyalty->setIdOrder(0);
  51.                     $loyalty->setIdLoyaltyState(2);
  52.                     $loyalty->setIdCustomer($idCustomer_);
  53.                     $loyalty->setPoints($points_);
  54.                     $loyalty->setDateAdd(date_create('now'));
  55.                     $loyalty->setDateUpd(date_create('now'));
  56.                     $loyalty->setFvEnovaNr('BILANS');
  57.                     $entityManager->persist($loyalty);
  58.                     $entityManager->flush();
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     public function updateUser(BeforeEntityUpdatedEvent $event)
  64.     {
  65.         $entity $event->getEntityInstance();
  66.         if (!($entity instanceof User)) {
  67.             return;
  68.         }
  69.         $this->setPassword($entity);
  70.     }
  71.     public function addUser(BeforeEntityPersistedEvent $event)
  72.     {
  73.         $entity $event->getEntityInstance();
  74.         if (!($entity instanceof User)) {
  75.             return;
  76.         }
  77.         $this->setPassword($entity);
  78.     }
  79.     /**
  80.      * @param User $entity
  81.      */
  82.     public function setPassword(User $entity): void
  83.     {
  84.         $pass $entity->getPassword();
  85.         $entity->setPassword(
  86.             $this->passwordEncoder->encodePassword(
  87.                 $entity,
  88.                 $pass
  89.             )
  90.         );
  91.         $this->entityManager->persist($entity);
  92.         $this->entityManager->flush();
  93.     }
  94. }