src/EventSubscriber/Activity/ActivitySubscriber.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Activity;
  3. use App\Event\Activity\ActivityEvent;
  4. use App\Service\Activity\ActivityManager;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class ActivitySubscriber implements EventSubscriberInterface
  8. {
  9.     private ActivityManager $activityManager;
  10.     private EntityManagerInterface $entityManager;
  11.     public function __construct(ActivityManager $activityManagerEntityManagerInterface $entityManager)
  12.     {
  13.         $this->activityManager $activityManager;
  14.         $this->entityManager $entityManager;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             ActivityEvent::NAME => 'save',
  20.         ];
  21.     }
  22.     public function save(ActivityEvent $even): void
  23.     {
  24.         $entity $even->getEntity();
  25.         $activity $this->activityManager->saveNewActivity();
  26.         $entity->setActivity($activity);
  27.         $this->entityManager->persist($entity);
  28.         $this->entityManager->flush();
  29.     }
  30. }