src/EventSubscriber/SessionQuestionSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use App\Event\SessionQuestionPreRemoveEvent;
  5. use App\Event\SessionQuestionPrePersistEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class SessionQuestionSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(
  10.         private EntityManagerInterface $_em,
  11.     ) {}
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             SessionQuestionPrePersistEvent::NAME => 'preCreate',
  16.             SessionQuestionPreRemoveEvent::NAME => 'preRemove',
  17.         ];
  18.     }
  19.     public function preCreate(SessionQuestionPrePersistEvent $event)
  20.     {
  21.         $object $event->getObject();
  22.         $session $object->getSession();
  23.         if ($session && !$session->getIsCommentModerated()) {
  24.             $parent $object->getParent();
  25.             if (!empty($parent)) {
  26.                 $parent->setIsReplyed(true);
  27.                 $this->_em->persist($parent);
  28.             }
  29.         }
  30.     }
  31.     public function preRemove(SessionQuestionPreRemoveEvent $event)
  32.     {
  33.         $object $event->getObject();
  34.         $session $object->getSession();
  35.         if ($session && !$session->getIsCommentModerated()) {
  36.             $parent $object->getParent();
  37.             if (!empty($parent)) {
  38.                 $children $parent->getChildren();
  39.                 if (count($children) === 1) {
  40.                     $parent->setIsReplyed(false);
  41.                     $this->_em->persist($parent);
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }