<?php
namespace App\EventSubscriber;
use App\Entity\CarteCadeau;
use App\Entity\Genres;
use App\Entity\PrixLitteraire;
use App\Entity\Produit;
use App\Entity\Promotion;
use App\Entity\Publics;
use App\Entity\PublicsJeunesse;
use App\Entity\Selection;
use App\Entity\Serie;
use App\Entity\SousGenres;
use App\Entity\Theme;
use App\Entity\Tier;
use App\Entity\Transaction;
use App\Entity\User;
use App\Entity\UserProduits;
use App\Entity\UserWishlists;
use App\Entity\Version;
use App\Exception\BunnyCDNStorageException;
use App\Service\AirTableService;
use App\Service\BunnyManager;
use App\Service\StringManager;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Psr\Log\LoggerInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $em;
private $bunnyManager;
private $airTableService;
private $passwordEncoder;
private $stringManager;
private $logger;
/**
* @param EntityManagerInterface $em
* @param BunnyManager $bunnyManager
* @param AirTableService $airTableService
* @param UserPasswordHasherInterface $passwordEncoder
* @param StringManager $stringManager
* @param LoggerInterface $logger
*/
public function __construct(
EntityManagerInterface $em,
BunnyManager $bunnyManager,
AirTableService $airTableService,
UserPasswordHasherInterface $passwordEncoder,
StringManager $stringManager,
LoggerInterface $logger
)
{
$this->em = $em;
$this->bunnyManager = $bunnyManager;
$this->airTableService = $airTableService;
$this->passwordEncoder = $passwordEncoder;
$this->stringManager = $stringManager;
$this->logger = $logger;
}
/**
* @return string[][]
*/
public static function getSubscribedEvents(): array
{
return [
BeforeEntityPersistedEvent::class => ['add'],
AfterEntityPersistedEvent::class => ['import'],
BeforeEntityUpdatedEvent::class => ['update'],
AfterEntityUpdatedEvent::class => ['importAfter'],
];
}
/**
* @param BeforeEntityPersistedEvent $event
* @return void
*/
public function add(BeforeEntityPersistedEvent $event): void
{
$entity = $event->getEntityInstance();
if ($entity instanceof Tier) {
$prenom = $entity->getPrenom();
$nom = $entity->getNom();
$slugger = new AsciiSlugger();
if ($prenom && $nom) {
$alias = $slugger->slug($prenom . '-' . $nom);
} elseif ($prenom) {
$alias = $slugger->slug($prenom);
} elseif ($nom) {
$alias = $slugger->slug($nom);
} else {
$alias = '';
}
$entity->setAlgoliaName(strtolower($alias))
->setDateUpdate(new \DateTime())
->setDateCreation(new \DateTime());
} else if ($entity instanceof Selection) {
$entity->setDateUpdate(new \DateTime)
->setDateCreated(new \DateTime);
} else if ($entity instanceof User) {
$this->setPassword($entity);
} else if ($entity instanceof Serie) {
$entity->setDateCreation(new \DateTime);
$entity->setSlug($this->stringManager->formatAlias($entity->getTitre()));
} else if ($entity instanceof Produit) {
$entity->setDateCreation(new \DateTime);
$this->airTableService->addProduct($entity);
} else if ($entity instanceof PrixLitteraire) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Genres) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof SousGenres) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Publics) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof PublicsJeunesse) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Theme) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Version) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof CarteCadeau) {
$entity->setDateCreation(new \DateTime);
} else if ($entity instanceof Promotion) {
$this->addProductToPromotion($entity);
}
}
/**
* @param BeforeEntityUpdatedEvent $event
* @return void
*/
public function update(BeforeEntityUpdatedEvent $event): void
{
$entity = $event->getEntityInstance();
if ($entity instanceof Tier) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Selection) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof User) {
$this->setPassword($entity);
} else if ($entity instanceof Serie) {
$entity->setDateUpdate(new \DateTime);
$entity->setSlug($this->stringManager->formatAlias($entity->getTitre()));
} else if ($entity instanceof Produit) {
$entity->setDateUpdate(new \DateTime);
$this->airTableService->editProduct($entity);
} else if ($entity instanceof PrixLitteraire) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Genres) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof SousGenres) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Publics) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof PublicsJeunesse) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Theme) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Version) {
$entity->setDateUpdate(new \DateTime);
} else if ($entity instanceof Transaction) {
$this->updateTransaction($entity);
} elseif ($entity instanceof Promotion) {
$this->addProductToPromotion($entity);
}
}
/**
* Import les fichiers d'un produit dans bunny CDN
* après son enregistrement en base de données.
*
* @param AfterEntityPersistedEvent $event
* @return void
* @throws BunnyCDNStorageException
*/
public function import(AfterEntityPersistedEvent $event): void
{
$entity = $event->getEntityInstance();
if ($entity instanceof Produit) {
if ($entity->getImageHdName()) {
$this->bunnyManager->importFileToBunnyCdn($entity->getImageHdName(), 'hd');
}
}
if ($entity instanceof Tier) {
if ($entity->getImageName()) {
$this->bunnyManager->importFileToBunnyCdnTiers($entity->getImageName());
}
}
}
/**
* Import les fichiers d'un produit dans bunny CDN
* après son enregistrement en base de données.
*
* @param AfterEntityUpdatedEvent $event
* @return void
* @throws BunnyCDNStorageException
*/
public function importAfter(AfterEntityUpdatedEvent $event): void
{
$entity = $event->getEntityInstance();
if ($entity instanceof Produit) {
if ($entity->getImageHdName()) {
$this->bunnyManager->importFileToBunnyCdn($entity->getImageHdName(), 'hd');
}
}
if ($entity instanceof Tier) {
if ($entity->getImageName()) {
$this->bunnyManager->importFileToBunnyCdnTiers($entity->getImageName());
}
}
}
public function updateTransaction($entity): void
{
if (($entity->getStatus()->getId() == 2) ||
($entity->getStatus()->getId() == 3) ||
($entity->getStatus()->getId() == 4)) {
foreach ($entity->getTransactionItems() as $item) {
if ($item->getProduit() != null) {
$produit = $item->getProduit();
$uproduit = $this->em->getRepository(UserProduits::class)->findOneBy(array(
'user' => $entity->getClient()->getId(),
'produit' => $produit->getId()
));
if ($uproduit != null) {
$this->em->remove($uproduit);
}
} else {
$carte = $item->getCartecadeau();
$carte->setEtat('invalide');
$this->em->persist($carte);
}
}
$user = $entity->getClient();
$user->setFidelite($user->getFidelite() - $entity->getPointsFidelite());
$this->em->persist($user);
$this->em->flush();
} else if ($entity->getStatus()->getId() == 1) {
foreach ($entity->getTransactionItems() as $item) {
if ($item->getProduit() != null) {
$produit = $item->getProduit();
$uproduit = $this->em->getRepository(UserProduits::class)->findOneBy(array(
'user' => $entity->getClient()->getId(),
'produit' => $produit->getId()
));
if ($uproduit == null) {
$uproduit = new UserProduits;
$user = $entity->getClient();
$uproduit->setDateActivation(new \DateTime)
->setProduit($produit)
->setUser($user)
->setNbEcouteStream(0)
->setNbTelechargementPartiel(0)
->setNbTelechargementZip(0);
$this->em->persist($uproduit);
$productsWishlist = $this->em->getRepository(UserWishlists::class)->findOneBy(array('produit' => $produit, 'user' => $user));
if ($productsWishlist != null) {
$this->em->remove($productsWishlist);
}
}
}
}
$this->em->flush();
}
}
/**
* @param User $user
* @return void
*/
public function setPassword(User $user): void
{
$plainPassword = $user->getPlainPassword();
if ($plainPassword) {
$user->setPassword(
$this->passwordEncoder->hashPassword(
$user,
$plainPassword
)
);
$this->em->flush();
}
}
/**
* @param Promotion $entity
* @return void
*/
private function addProductToPromotion(Promotion $entity): void
{
if (str_contains($entity->getProductStrings(), ';')) {
$productLists = explode(';', $entity->getProductStrings());
} else {
$productLists = [$entity->getProductStrings()];
}
if ($entity->getProduits()) {
foreach ($entity->getProduits() as $produit) {
$entity->removeProduit($produit);
}
$this->em->flush();
}
foreach ($productLists as $productList) {
if ($product = $this->em->getRepository(Produit::class)->findOneBy(['id' => $productList])) {
$entity->addProduit($product);
}
}
$this->em->flush();
}
}