<?php
namespace App\EventSubscriber;
use App\Entity\IAProcessus;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class IAProcessusSubscriber implements EventSubscriberInterface
{
private $entityManager;
private ManagerRegistry $manager;
public function __construct(
ManagerRegistry $manager,
EntityManagerInterface $entityManager,
TokenStorageInterface $tokenStorage
) {
$this->manager = $manager;
$this->entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['afterAdd'],
];
}
public function afterAdd(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if ($entity instanceof IAProcessus) {
if ($entity->getIATache()->getPrompt() != '') {
$entity->setPrompt($entity->getIATache()->getPrompt());
}
if ($entity->getIaTache()->getChampsSource() != '') {
$entity->setChampsSource($entity->getIaTache()->getChampsSource());
}
if ($entity->getIaTache()->getChampsDestination()) {
$entity->setChampsDestination($entity->getIaTache()->getChampsDestination());
}
if ($entity->getIATache()->getEchange()) {
$entity->setEchange($entity->getIATache()->getEchange());
}
if ($entity->getIATache()->getIaFournisseur()) {
$entity->setIaFournisseur($entity->getIATache()->getIaFournisseur());
}
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
}
}