src/Listener/EasyAdminListener.php line 93

Open in your IDE?
  1. <?php
  2. /** @noinspection PhpUnused */
  3. namespace App\Listener;
  4. use Agediss\RabbitMQ\Entity\AMQPMessageBody;
  5. use App\Entity\AdresseVoisine;
  6. use App\Entity\Client;
  7. use App\Entity\Evenement;
  8. use App\Entity\Flux;
  9. use App\Entity\Motif;
  10. use App\Entity\MotifDetail;
  11. use App\Entity\Pays;
  12. use App\Entity\Plateforme;
  13. use App\Entity\Pole;
  14. use App\Entity\Societe;
  15. use App\Entity\SousTraitant;
  16. use App\Entity\Transporteur;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  19. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  20. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  21. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  22. use http\Exception\RuntimeException;
  23. use Psr\Log\LoggerInterface;
  24. use Symfony\Component\DependencyInjection\ContainerInterface;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use function get_class;
  27. use function in_array;
  28. class EasyAdminListener implements EventSubscriberInterface
  29. {
  30.     public const ENTITIES_NOTIF = [
  31.         Plateforme::class,
  32.         Client::class,
  33.         Pole::class,
  34.         SousTraitant::class,
  35.         Societe::class,
  36.         Transporteur::class,
  37.     ];
  38.     /**
  39.      * @var ContainerInterface
  40.      */
  41.     private $container;
  42.     /**
  43.      * @var LoggerInterface
  44.      */
  45.     private $logger;
  46.     /**
  47.      * @var EntityManagerInterface
  48.      */
  49.     private $entityManager;
  50.     public function __construct(ContainerInterface $containerLoggerInterface $loggerEntityManagerInterface $entityManager)
  51.     {
  52.         $this->container $container;
  53.         $this->logger $logger;
  54.         $this->entityManager $entityManager;
  55.     }
  56.     public static function getSubscribedEvents(): array
  57.     {
  58.         return [
  59.             AfterEntityPersistedEvent::class => ['notifyAfterPersist'],
  60.             AfterEntityUpdatedEvent::class => ['notifyAfterUpdate'],
  61.             AfterEntityDeletedEvent::class => ['notifyAfterDelete'],
  62.             BeforeEntityPersistedEvent::class => ['initializeEmptyAiguillage'],
  63.         ];
  64.     }
  65.     public function notifyAfterPersist(AfterEntityPersistedEvent $event): void
  66.     {
  67.         if (!in_array(get_class($event->getEntityInstance()), self::ENTITIES_NOTIF)) {
  68.             return;
  69.         }
  70.         if (get_class($event->getEntityInstance()) === 'App\Entity\Client') {
  71.             if($event->getEntityInstance()->getCategorie()->getCode() !== 'COMPTE_CLIENT' && $event->getEntityInstance()->getCategorie()->getCode() !== 'CLIENT') {
  72.                 return;
  73.             }
  74.         }
  75.         // creation de ligne de transcodage.
  76.         $this->generateTranscodeEntry($event->getEntityInstance());
  77.         $this->notifyAgedissMQ(
  78.             get_class($event->getEntityInstance()),
  79.             $event->getEntityInstance()->getCode(), AMQPMessageBody::CONTEXT_CREATE
  80.         );
  81.     }
  82.     public function notifyAfterUpdate(AfterEntityUpdatedEvent $event): void
  83.     {
  84.         if (!in_array(get_class($event->getEntityInstance()), self::ENTITIES_NOTIF)) {
  85.             return;
  86.         }
  87.         if (get_class($event->getEntityInstance()) === 'App\Entity\Client') {
  88.             if($event->getEntityInstance()->getCategorie()->getCode() !== 'COMPTE_CLIENT' && $event->getEntityInstance()->getCategorie()->getCode() !== 'CLIENT') {
  89.                 return;
  90.             }
  91.         }
  92.         $context AMQPMessageBody::CONTEXT_UPDATE;
  93.         // si on est sur une suppression en softdeletable
  94.         if (property_exists($event->getEntityInstance(), 'deletedAt') && $event->getEntityInstance()->isDeleted()) {
  95.             $context AMQPMessageBody::CONTEXT_DELETE;
  96.         }
  97.         $this->notifyAgedissMQ(
  98.             get_class($event->getEntityInstance()),
  99.             $event->getEntityInstance()->getCode(), $context
  100.         );
  101.     }
  102.     public function notifyAfterDelete(AfterEntityDeletedEvent $event): void
  103.     {
  104.         if (!in_array(get_class($event->getEntityInstance()), self::ENTITIES_NOTIF)) {
  105.             return;
  106.         }
  107.         if (get_class($event->getEntityInstance()) === 'App\Entity\Client') {
  108.             if($event->getEntityInstance()->getCategorie()->getCode() !== 'COMPTE_CLIENT' && $event->getEntityInstance()->getCategorie()->getCode() !== 'CLIENT') {
  109.                 return;
  110.             }
  111.         }
  112.         $this->notifyAgedissMQ(
  113.             get_class($event->getEntityInstance()),
  114.             $event->getEntityInstance()->getCode(), AMQPMessageBody::CONTEXT_DELETE
  115.         );
  116.     }
  117.     /**
  118.      * @param $entityType
  119.      * @param $entityCode
  120.      * @param int $context
  121.      */
  122.     private function notifyAgedissMQ($entityType$entityCodeint $context)
  123.     {
  124.         switch ($entityType) {
  125.             case Client::class:
  126.                 $callbackUrl '/api-referentiel/clients/' $entityCode;
  127.                 $producer 'client';
  128.                 break;
  129.             case Motif::class:
  130.                 $callbackUrl '/api-referentiel/motifs/' $entityCode;
  131.                 $producer 'motifs_et_motifs';
  132.                 break;
  133.             case MotifDetail::class:
  134.                 // récupération du motif lié
  135.                 $motif $this->container->get('doctrine.orm.entity_manager')->getRepository(Motif::class)->findOneBy(['code' => $entityCode]);
  136.                 if (!$motif) {
  137.                     return;
  138.                 }
  139.                 $callbackUrl '/api-referentiel/motifs/' $motif->getCode();
  140.                 $producer 'motifs_et_motifs';
  141.                 break;
  142.             case Plateforme::class:
  143.                 $callbackUrl '/api-referentiel/plateformes/' $entityCode;
  144.                 $producer 'plateforme';
  145.                 break;
  146.             case Societe::class:
  147.                 $callbackUrl '/api-referentiel/societes/' $entityCode;
  148.                 $producer 'societe';
  149.                 break;
  150.             case SousTraitant::class:
  151.                 $callbackUrl '/api-referentiel/sous-traitants/' $entityCode;
  152.                 $producer 'sous_traitant';
  153.                 break;
  154.             case Pole::class:
  155.                 $callbackUrl '/api-referentiel/poles/' $entityCode;
  156.                 $producer 'pole';
  157.                 break;
  158.             case Transporteur::class:
  159.                 $callbackUrl '/api-referentiel/transporteurs/' $entityCode;
  160.                 $producer 'transporteur';
  161.                 break;
  162.             default:
  163.                 throw new RuntimeException('vous devez passer un entityType valide ! [ ' $entityType ' ]');
  164.         }
  165.         $this->logger->info('une entité ( ' $entityType ' ) à été créer ou modifié et relayée dans le Agediss MQ');
  166.         $msgBody = new AMQPMessageBody($callbackUrl$context);
  167.         $this->container->get('old_sound_rabbit_mq.referentiel_' $producer '_producer')->publish($msgBody);
  168.     }
  169.     public function initializeEmptyAiguillage(BeforeEntityPersistedEvent $event)
  170.     {
  171.         /** @var Flux $entity */
  172.         $entity $event->getEntityInstance();
  173.         if (!($entity instanceof Flux)) {
  174.             return;
  175.         }
  176.         if (empty($entity->getAiguillage())) {
  177.             $entity->setAiguillage('');
  178.         }
  179.     }
  180.     private function generateTranscodeEntry($entity)
  181.     {
  182.         $transcode = new Flux();
  183.         $transcode->setReferentiel($entity->getCode());
  184.         $entityType get_class($entity);
  185.         switch ($entityType) {
  186.             case Client::class:
  187.                 switch ($entity->getCategorie()->getCode()) {
  188.                     case 'COMPTE_CLIENT':
  189.                         $transcode->setField(Client::FIELD_TYPE_COMPTE_CLIENT);
  190.                         break;
  191.                     case 'CLIENT':
  192.                         $transcode->setField(Client::FIELD_TYPE_CLIENT);
  193.                         break;
  194.                     case 'SERVICE_CLIENT':
  195.                         $transcode->setField(Client::FIELD_TYPE_SERVICE_CLIENT);
  196.                         break;
  197.                     case 'AGENCE':
  198.                         $transcode->setField(Client::FIELD_TYPE_AGENCE);
  199.                         break;
  200.                     case 'TRACTION':
  201.                         $transcode->setField(Client::FIELD_TYPE_TRACTION);
  202.                         break;
  203.                     case 'HUB':
  204.                         $transcode->setField(Client::FIELD_TYPE_HUB);
  205.                         break;
  206.                     case 'DESTINATAIRE_REMARQUABLE':
  207.                         $transcode->setField(Client::FIELD_TYPE_DESTINATAIRE_REMARQUABLE);
  208.                         break;
  209.                     default:
  210.                 }
  211.                 $transcode->setTradiss($entity->getCodeTradiss());
  212.                 $transcode->setWexlog($entity->getCodeWexlog());
  213.                 break;
  214.             case AdresseVoisine::class:
  215.                 $transcode->setField(AdresseVoisine::FIELD_TYPE);
  216.                 break;
  217.             case Plateforme::class:
  218.                 $transcode->setField(Plateforme::FIELD_TYPE);
  219.                 break;
  220.             case Pole::class:
  221.                 $transcode->setField(Pole::FIELD_TYPE);
  222.                 break;
  223.             case Societe::class:
  224.                 $transcode->setField(Societe::FIELD_TYPE);
  225.                 break;
  226.             case SousTraitant::class:
  227.                 $transcode->setField(SousTraitant::FIELD_TYPE);
  228.                 break;
  229.             case Transporteur::class:
  230.                 $transcode->setField(Transporteur::FIELD_TYPE);
  231.                 break;
  232.             case Motif::class:
  233.                 $transcode->setField(Motif::FIELD_TYPE);
  234.                 break;
  235.             case MotifDetail::class:
  236.                 $transcode->setField(MotifDetail::FIELD_TYPE);
  237.                 break;
  238.             case Pays::class:
  239.                 $transcode->setField(Pays::FIELD_TYPE);
  240.                 break;
  241.             case Evenement::class:
  242.                 $transcode->setField(Evenement::FIELD_TYPE);
  243.                 break;
  244.             default:
  245.                 throw new RuntimeException('vous devez passer un entityType valide ! [ ' $entityType ' ]');
  246.         }
  247.         $this->entityManager->persist($transcode);
  248.         $this->entityManager->flush();
  249.         $this->logger->info('une entité ( ' $entityType ' ) à été créer ainsi que ça ligne de transcode');
  250.     }
  251. }