<?php
namespace App\EventSubscriber;
use App\Classes\Tools;
use App\Entity\Presta\PsCustomer;
use App\Entity\Presta\PsLoyalty;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class EasyAdminSubscriber extends AbstractController implements EventSubscriberInterface
{
private $entityManager;
private $passwordEncoder;
public function __construct(EntityManagerInterface $entityManager, UserPasswordEncoderInterface $passwordEncoder)
{
$this->entityManager = $entityManager;
$this->passwordEncoder = $passwordEncoder;
}
public static function getSubscribedEvents()
{
return [
AfterEntityUpdatedEvent::class => ['addPoints'],
BeforeEntityPersistedEvent::class => ['addUser'],
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é ...
];
}
public function addPoints(AfterEntityUpdatedEvent $event){
$idCustomer = $event->getEntityInstance();
if (($idCustomer instanceof PsCustomer)) {
//$idCustomer_ = (int)$idCustomer->getIdCustomer();
$idCustomer_ = (int)$idCustomer->getEnovaId();
$aktywny = $idCustomer->getAktywnyCms();
$idEnova_ = (int)$idCustomer->getEnovaId();
if ($idEnova_ != 0 and $aktywny !== false) {
$tools = new Tools();
$points_ = (int)($tools->getPointsPremium($idEnova_));
$entityManager = $this->getDoctrine()->getManager('presta');
$ly = $entityManager->getRepository('Presta:PsLoyalty');
$po = $ly->findOneBy([ 'idCustomer'=>$idCustomer_ , 'fvEnovaNr' => 'BILANS']);
if ($po == null) {
$loyalty = new PsLoyalty();
$loyalty->setIdCartRule(0);
$loyalty->setIdOrder(0);
$loyalty->setIdLoyaltyState(2);
$loyalty->setIdCustomer($idCustomer_);
$loyalty->setPoints($points_);
$loyalty->setDateAdd(date_create('now'));
$loyalty->setDateUpd(date_create('now'));
$loyalty->setFvEnovaNr('BILANS');
$entityManager->persist($loyalty);
$entityManager->flush();
}
}
}
}
public function updateUser(BeforeEntityUpdatedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$this->setPassword($entity);
}
public function addUser(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$this->setPassword($entity);
}
/**
* @param User $entity
*/
public function setPassword(User $entity): void
{
$pass = $entity->getPassword();
$entity->setPassword(
$this->passwordEncoder->encodePassword(
$entity,
$pass
)
);
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
}