src/EventSubscriber/WebappImageSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/EventSubscriber/WebappImageSubscriber.php
  4. // Plan.io Task #4410
  5. //----------------------------------------------------------------------
  6. namespace App\EventSubscriber;
  7. use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\ORM\Events;
  10. use Doctrine\Persistence\Event\LifecycleEventArgs;
  11. use Doctrine\Persistence\Proxy;
  12. use Doctrine\ORM\Event\OnFlushEventArgs;
  13. use Doctrine\ORM\Event\PostFlushEventArgs;
  14. use App\Entity\Webapp\Image;
  15. use App\Services\LogTools;
  16. class WebappImageSubscriber implements EventSubscriberInterface
  17. {
  18.     public function __construct(EntityManagerInterface $emLogTools $logTools)
  19.     {
  20.         $this->em $em;
  21.         $this->logTools $logTools;
  22.     }
  23.     private array $entitiesToLog = [];
  24.     public function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             Events::prePersist,
  28.             Events::preUpdate,
  29.             Events::preRemove,
  30.             Events::postFlush,
  31.         ];
  32.     }
  33.     public function prePersist(LifecycleEventArgs $args): void
  34.     {
  35.         $entity $args->getObject();
  36.         if ($entity instanceof Image)
  37.         {
  38.             $this->entitiesToLog[] = $entity;
  39.             $log "[prePersist]".$entity->getLogLabel();
  40.             $this->logTools->webapp_image_debug($log);
  41.         }
  42.     }
  43.     public function preUpdate(LifecycleEventArgs $args): void
  44.     {
  45.         $entity $args->getObject();
  46.         if ($entity instanceof Image)
  47.         {
  48.             $this->entitiesToLog[] = $entity;
  49.             $log "[preUpdate]".$entity->getLogLabel();
  50.             $this->logTools->webapp_image_debug($log);
  51.         }
  52.     }
  53.     public function preRemove(LifecycleEventArgs $args): void
  54.     {
  55.         $entity $args->getObject();
  56.         if ($entity instanceof Image)
  57.         {
  58.             // In postFlush the id is no longer availabile since the object
  59.             // has already been removed from the database
  60.             $entity->updateLoggingData([
  61.                 "id" => $entity->getId(),
  62.             ]);
  63.             $this->entitiesToLog[] = $entity;
  64.             $log "[preRemove]".$entity->getLogLabel();
  65.             $this->logTools->webapp_image_debug($log);
  66.         }
  67.     }
  68.     public function postFlush($args): void
  69.     {
  70.         foreach ($this->entitiesToLog as $entity)
  71.         {
  72.             $log "[postFlush]".$entity->getLogLabel();
  73.             $this->logTools->webapp_image_debug($log);
  74.         }
  75.         $this->entitiesToLog = [];
  76.     }
  77. }