src/EventSubscriber/DoctrineSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\Common\EventSubscriber;
  4. use Doctrine\Persistence\Event\LifecycleEventArgs;
  5. use Doctrine\ORM\Event\PostFlushEventArgs;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use App\Entity\User;
  8. use App\Entity\Log;
  9. use App\Entity\LogJournal;
  10. use App\Entity\Onglet;
  11. use App\Entity\Notification;
  12. use App\Entity\Alerte;
  13. use App\Entity\EvenementResultat;
  14. use App\Entity\Methode;
  15. use App\Entity\MethodeEtape;
  16. use App\Service\LogService;
  17. use App\Service\NotificationService;
  18. use App\Service\AlerteService;
  19. use App\Service\AppService;
  20. use App\Service\MineService;
  21. use App\Enum\NotificationActionEnum;
  22. class DoctrineSubscriber implements EventSubscriber
  23. {
  24.     private $logs = [];
  25.     private $notifs = [];
  26.     public function __construct(TokenStorageInterface $tokenStorage
  27.     MineService $res_serviceAppService $appServiceLogService $logServiceNotificationService $notificationServiceAlerteService $alerteService)
  28.     {
  29.         $this->appService           $appService;
  30.         $this->logService           $logService;
  31.         $this->tokenStorage         $tokenStorage;
  32.         $this->notificationService  $notificationService;
  33.         $this->alerteService        $alerteService;
  34.         $this->mineService     $res_service;
  35.     }
  36.     /**
  37.      * @return array
  38.      */
  39.     public function getSubscribedEvents()
  40.     {
  41.         return array(
  42.             'prePersist',
  43.             'preUpdate',
  44.             'preRemove',
  45.             'preflush',
  46.             'postPersist',
  47.             'postFlush',
  48.         );
  49.     }
  50.     /**
  51.      * @param LifecycleEventArgs $args
  52.      */
  53.     public function prePersist(LifecycleEventArgs $args)
  54.     {
  55.         if (method_exists($args->getEntity(), 'setCreatedBy')) {
  56.             if ($this->tokenStorage->getToken()) {
  57.                 $user $this->tokenStorage->getToken()->getUser();
  58.                 if ($user instanceof User) {
  59.                     $entity $args->getEntity();
  60.                     $entity->setCreatedBy($user);
  61.                 }
  62.             }
  63.         }
  64.         // Timestampable
  65.         if (method_exists($args->getEntity(), 'setCreatedAt')) {
  66.             $entity $args->getEntity();
  67.             $entity->setCreatedAt(new \DateTime());
  68.         }
  69.         // Alertes, Ajoute le numéro de version
  70.         if ($args->getEntity() instanceof Alerte) {
  71.             $entity $args->getEntity();
  72.             $this->alerteService->prePersist($entity);
  73.         }
  74.         // Référence auto
  75.         $traits class_uses($args->getEntity());
  76.         if(array_key_exists("App\Traits\Referenceable"$traits)) {
  77.             $entity $args->getEntity();
  78.             $ref $this->appService->getReferenceAuto($entity);
  79.             $entity->setReference($ref);
  80.         }// if
  81.     }
  82.     public function postPersist(LifecycleEventArgs $args) {
  83.         // Log
  84.         if (
  85.             !$args->getEntity() instanceof LogJournal
  86.             && !$args->getEntity() instanceof Log
  87.             && !$args->getEntity() instanceof Onglet //on retire les logs pour les onglets
  88.         ) {
  89.             $newLog $this->logService->createLogJournal($args'New');
  90.             $this->logs[] = $newLog;
  91.         }
  92.         //Notification a la création d'un user
  93.         if ($args->getEntity() instanceof User) {
  94.             $params_notif = [
  95.                 "prospect"      => null,
  96.                 "mine"     => null,
  97.                 "libelle"       => "Création utilisateur",
  98.                 "contenu"       => null,
  99.                 "url"           => null,
  100.                 "user"          => null,
  101.             ];
  102.             $this->notificationService->addNotification($params_notif,NotificationActionEnum::NEW_USER);
  103.         }// if
  104.         //Notification, chaque notif génère une action suivant son type. envoi de mail ...
  105.         if ($args->getEntity() instanceof Notification) {
  106.             $this->notificationService->actionNotification($args->getEntity());
  107.         }
  108.         // Prospect
  109.         if ($args->getEntity() instanceof Prospect) {
  110.             if (!is_null($this->tokenStorage->getToken())) {
  111.                 $user $this->tokenStorage->getToken()->getUser();
  112.                 $repo_methode $args->getEntityManager()->getRepository(Methode::class);
  113.                 $repo_etape $args->getEntityManager()->getRepository(MethodeEtape::class);
  114.                
  115.             }// if
  116.             
  117.         }// if
  118.         // Prescripteur
  119.         if ($args->getEntity() instanceof Prescripteur) {
  120.             
  121.             
  122.             $repo_resultat $args->getEntityManager()->getRepository(EvenementResultat::class);
  123.             $repo_methode $args->getEntityManager()->getRepository(Methode::class);
  124.             $repo_etape $args->getEntityManager()->getRepository(MethodeEtape::class);
  125.         }// if
  126.     }// function
  127.     /**
  128.      * @param LifecycleEventArgs $args
  129.      */
  130.     public function preUpdate(LifecycleEventArgs $args)
  131.     {
  132.         // Log
  133.         if (
  134.             !$args->getEntity() instanceof LogJournal
  135.             && !$args->getEntity() instanceof Log
  136.             && !$args->getEntity() instanceof Onglet //on retire les logs pour les onglets
  137.         ) {
  138.             $newLog $this->logService->createLogJournal($args'Edit');
  139.             $this->logs[] = $newLog;
  140.         }
  141.         // Blamable
  142.         if (method_exists($args->getEntity(), 'setUpdatedBy')) {
  143.             if ($this->tokenStorage->getToken()) {
  144.                 $user $this->tokenStorage->getToken()->getUser();
  145.                 if ($user instanceof User) {
  146.                     $entity $args->getEntity();
  147.                     $entity->setUpdatedBy($user);
  148.                 }
  149.             }
  150.         }
  151.         // Timestampable
  152.         if (method_exists($args->getEntity(), 'setUpdatedAt')) {
  153.             $entity $args->getEntity();
  154.             $entity->setUpdatedAt(new \DateTime());
  155.         }
  156.         // Alertes: Numéro de version ...
  157.         if ($args->getEntity() instanceof Alerte) {
  158.             $entity $args->getEntity();
  159.             $this->alerteService->preUpdate($entity);
  160.         }
  161.     }
  162.     public function preRemove(LifecycleEventArgs $args)
  163.     {
  164.         //Log
  165.         if (
  166.             !$args->getEntity() instanceof LogJournal
  167.             && !$args->getEntity() instanceof Log
  168.             && !$args->getEntity() instanceof Onglet //on retire les logs pour les onglets
  169.         ) {
  170.             $newLog $this->logService->createLogJournal($args'Remove');
  171.             $this->logs[] = $newLog;
  172.         }
  173.     }
  174.     public function postFlush(PostFlushEventArgs $args)
  175.     {
  176.         // Flush Logs
  177.         if (!empty($this->logs)) {
  178.             $em $args->getEntityManager();
  179.             foreach ($this->logs as $log) {
  180.                 $em->persist($log);
  181.             }
  182.             $this->logs = [];
  183.             $em->flush();
  184.         }
  185.         // Flush Notifs
  186.         if (!empty($this->notifs)) {
  187.             $em $args->getEntityManager();
  188.             foreach ($this->notifs as $notif) {
  189.                 $em->persist($notif);
  190.             }
  191.             $this->notifs = [];
  192.             $em->flush();
  193.         }
  194.     }
  195. }