<?php
/** @noinspection PhpUnused */
namespace App\Listener;
use Agediss\RabbitMQ\Entity\AMQPMessageBody;
use App\Entity\AdresseVoisine;
use App\Entity\Client;
use App\Entity\Evenement;
use App\Entity\Flux;
use App\Entity\Motif;
use App\Entity\MotifDetail;
use App\Entity\Pays;
use App\Entity\Plateforme;
use App\Entity\Pole;
use App\Entity\Societe;
use App\Entity\SousTraitant;
use App\Entity\Transporteur;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use http\Exception\RuntimeException;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use function get_class;
use function in_array;
class EasyAdminListener implements EventSubscriberInterface
{
public const ENTITIES_NOTIF = [
Plateforme::class,
Client::class,
Pole::class,
SousTraitant::class,
Societe::class,
Transporteur::class,
];
/**
* @var ContainerInterface
*/
private $container;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function __construct(ContainerInterface $container, LoggerInterface $logger, EntityManagerInterface $entityManager)
{
$this->container = $container;
$this->logger = $logger;
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
AfterEntityPersistedEvent::class => ['notifyAfterPersist'],
AfterEntityUpdatedEvent::class => ['notifyAfterUpdate'],
AfterEntityDeletedEvent::class => ['notifyAfterDelete'],
BeforeEntityPersistedEvent::class => ['initializeEmptyAiguillage'],
];
}
public function notifyAfterPersist(AfterEntityPersistedEvent $event): void
{
if (!in_array(get_class($event->getEntityInstance()), self::ENTITIES_NOTIF)) {
return;
}
if (get_class($event->getEntityInstance()) === 'App\Entity\Client') {
if($event->getEntityInstance()->getCategorie()->getCode() !== 'COMPTE_CLIENT' && $event->getEntityInstance()->getCategorie()->getCode() !== 'CLIENT') {
return;
}
}
// creation de ligne de transcodage.
$this->generateTranscodeEntry($event->getEntityInstance());
$this->notifyAgedissMQ(
get_class($event->getEntityInstance()),
$event->getEntityInstance()->getCode(), AMQPMessageBody::CONTEXT_CREATE
);
}
public function notifyAfterUpdate(AfterEntityUpdatedEvent $event): void
{
if (!in_array(get_class($event->getEntityInstance()), self::ENTITIES_NOTIF)) {
return;
}
if (get_class($event->getEntityInstance()) === 'App\Entity\Client') {
if($event->getEntityInstance()->getCategorie()->getCode() !== 'COMPTE_CLIENT' && $event->getEntityInstance()->getCategorie()->getCode() !== 'CLIENT') {
return;
}
}
$context = AMQPMessageBody::CONTEXT_UPDATE;
// si on est sur une suppression en softdeletable
if (property_exists($event->getEntityInstance(), 'deletedAt') && $event->getEntityInstance()->isDeleted()) {
$context = AMQPMessageBody::CONTEXT_DELETE;
}
$this->notifyAgedissMQ(
get_class($event->getEntityInstance()),
$event->getEntityInstance()->getCode(), $context
);
}
public function notifyAfterDelete(AfterEntityDeletedEvent $event): void
{
if (!in_array(get_class($event->getEntityInstance()), self::ENTITIES_NOTIF)) {
return;
}
if (get_class($event->getEntityInstance()) === 'App\Entity\Client') {
if($event->getEntityInstance()->getCategorie()->getCode() !== 'COMPTE_CLIENT' && $event->getEntityInstance()->getCategorie()->getCode() !== 'CLIENT') {
return;
}
}
$this->notifyAgedissMQ(
get_class($event->getEntityInstance()),
$event->getEntityInstance()->getCode(), AMQPMessageBody::CONTEXT_DELETE
);
}
/**
* @param $entityType
* @param $entityCode
* @param int $context
*/
private function notifyAgedissMQ($entityType, $entityCode, int $context)
{
switch ($entityType) {
case Client::class:
$callbackUrl = '/api-referentiel/clients/' . $entityCode;
$producer = 'client';
break;
case Motif::class:
$callbackUrl = '/api-referentiel/motifs/' . $entityCode;
$producer = 'motifs_et_motifs';
break;
case MotifDetail::class:
// récupération du motif lié
$motif = $this->container->get('doctrine.orm.entity_manager')->getRepository(Motif::class)->findOneBy(['code' => $entityCode]);
if (!$motif) {
return;
}
$callbackUrl = '/api-referentiel/motifs/' . $motif->getCode();
$producer = 'motifs_et_motifs';
break;
case Plateforme::class:
$callbackUrl = '/api-referentiel/plateformes/' . $entityCode;
$producer = 'plateforme';
break;
case Societe::class:
$callbackUrl = '/api-referentiel/societes/' . $entityCode;
$producer = 'societe';
break;
case SousTraitant::class:
$callbackUrl = '/api-referentiel/sous-traitants/' . $entityCode;
$producer = 'sous_traitant';
break;
case Pole::class:
$callbackUrl = '/api-referentiel/poles/' . $entityCode;
$producer = 'pole';
break;
case Transporteur::class:
$callbackUrl = '/api-referentiel/transporteurs/' . $entityCode;
$producer = 'transporteur';
break;
default:
throw new RuntimeException('vous devez passer un entityType valide ! [ ' . $entityType . ' ]');
}
$this->logger->info('une entité ( ' . $entityType . ' ) à été créer ou modifié et relayée dans le Agediss MQ');
$msgBody = new AMQPMessageBody($callbackUrl, $context);
$this->container->get('old_sound_rabbit_mq.referentiel_' . $producer . '_producer')->publish($msgBody);
}
public function initializeEmptyAiguillage(BeforeEntityPersistedEvent $event)
{
/** @var Flux $entity */
$entity = $event->getEntityInstance();
if (!($entity instanceof Flux)) {
return;
}
if (empty($entity->getAiguillage())) {
$entity->setAiguillage('');
}
}
private function generateTranscodeEntry($entity)
{
$transcode = new Flux();
$transcode->setReferentiel($entity->getCode());
$entityType = get_class($entity);
switch ($entityType) {
case Client::class:
switch ($entity->getCategorie()->getCode()) {
case 'COMPTE_CLIENT':
$transcode->setField(Client::FIELD_TYPE_COMPTE_CLIENT);
break;
case 'CLIENT':
$transcode->setField(Client::FIELD_TYPE_CLIENT);
break;
case 'SERVICE_CLIENT':
$transcode->setField(Client::FIELD_TYPE_SERVICE_CLIENT);
break;
case 'AGENCE':
$transcode->setField(Client::FIELD_TYPE_AGENCE);
break;
case 'TRACTION':
$transcode->setField(Client::FIELD_TYPE_TRACTION);
break;
case 'HUB':
$transcode->setField(Client::FIELD_TYPE_HUB);
break;
case 'DESTINATAIRE_REMARQUABLE':
$transcode->setField(Client::FIELD_TYPE_DESTINATAIRE_REMARQUABLE);
break;
default:
}
$transcode->setTradiss($entity->getCodeTradiss());
$transcode->setWexlog($entity->getCodeWexlog());
break;
case AdresseVoisine::class:
$transcode->setField(AdresseVoisine::FIELD_TYPE);
break;
case Plateforme::class:
$transcode->setField(Plateforme::FIELD_TYPE);
break;
case Pole::class:
$transcode->setField(Pole::FIELD_TYPE);
break;
case Societe::class:
$transcode->setField(Societe::FIELD_TYPE);
break;
case SousTraitant::class:
$transcode->setField(SousTraitant::FIELD_TYPE);
break;
case Transporteur::class:
$transcode->setField(Transporteur::FIELD_TYPE);
break;
case Motif::class:
$transcode->setField(Motif::FIELD_TYPE);
break;
case MotifDetail::class:
$transcode->setField(MotifDetail::FIELD_TYPE);
break;
case Pays::class:
$transcode->setField(Pays::FIELD_TYPE);
break;
case Evenement::class:
$transcode->setField(Evenement::FIELD_TYPE);
break;
default:
throw new RuntimeException('vous devez passer un entityType valide ! [ ' . $entityType . ' ]');
}
$this->entityManager->persist($transcode);
$this->entityManager->flush();
$this->logger->info('une entité ( ' . $entityType . ' ) à été créer ainsi que ça ligne de transcode');
}
}