src/EventSubscriber/VideoGalleryHistorySubscriber.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\VideoGalleryHistory;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use App\Event\VideoGalleryHistoryPreRemoveEvent;
  6. use App\Event\VideoGalleryHistoryPostPersistEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class VideoGalleryHistorySubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(
  11.         private EntityManagerInterface $_em
  12.     ) {}
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             VideoGalleryHistoryPostPersistEvent::NAME => 'onPostCreate',
  17.             VideoGalleryHistoryPreRemoveEvent::NAME => 'onPreRemove'
  18.         ];
  19.     }
  20.     public function onPostCreate(VideoGalleryHistoryPostPersistEvent $event)
  21.     {
  22.         $object $event->getObject();
  23.         $videoGallery $object->getVideoGallery();
  24.         $totalViews $this->_em->getRepository(VideoGalleryHistory::class)->getTotalViews($videoGallery);
  25.         $videoGallery->setTotalViews($totalViews);
  26.         $this->_em->persist($videoGallery);
  27.         $this->_em->flush();
  28.     }
  29.     public function onPreRemove(VideoGalleryHistoryPreRemoveEvent $event)
  30.     {
  31.         $object $event->getObject();
  32.         $videoGallery $object->getVideoGallery();
  33.         $totalViews $this->_em->getRepository(VideoGalleryHistory::class)->getTotalViews($videoGallery$object->getId());
  34.         $videoGallery->setTotalViews($totalViews);
  35.         $this->_em->persist($videoGallery);
  36.         $this->_em->flush();
  37.     }
  38. }