From 33f93d484d409d3d7ce31145305ff24a2c88a5bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 11:34:41 +0200 Subject: [PATCH] deferring the sending of notification to kernel.terminate: prepare --- ...NotificationOnTerminateEventSubscriber.php | 47 +++++++++++++++++++ .../Notification/NotificationPersister.php | 23 +++++++++ .../NotificationPersisterInterface.php | 22 +++++++++ ...ficationOnTerminateEventSubscriberTest.php | 34 ++++++++++++++ .../config/services/notification.yaml | 11 ++--- .../Events/UserRefEventSubscriber.php | 13 +++-- 6 files changed, 137 insertions(+), 13 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php create mode 100644 src/Bundle/ChillMainBundle/Notification/NotificationPersister.php create mode 100644 src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php create mode 100644 src/Bundle/ChillMainBundle/Tests/Notification/EventListener/PersistNotificationOnTerminateEventSubscriberTest.php diff --git a/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php new file mode 100644 index 000000000..2739930f5 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php @@ -0,0 +1,47 @@ +em = $em; + $this->persister = $persister; + } + + public static function getSubscribedEvents() + { + return [ + 'kernel.terminate' => [ + ['onKernelTerminate', 1024], // we must ensure that the priority is before sending email + ] + ]; + } + + public function onKernelTerminate(TerminateEvent $event): void + { + if ($event->isMasterRequest()) { + $this->persistNotifications(); + } + } + + private function persistNotifications(): void + { + foreach ($this->persister->getWaitingNotifications() as $notification) { + $this->em->persist($notification); + } + + $this->em->flush(); + } +} diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationPersister.php b/src/Bundle/ChillMainBundle/Notification/NotificationPersister.php new file mode 100644 index 000000000..b8bd132ed --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/NotificationPersister.php @@ -0,0 +1,23 @@ +waitingNotifications[] = $notification; + } + + /** + * @return array|Notification[] + */ + public function getWaitingNotifications(): array + { + return $this->waitingNotifications; + } +} diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php b/src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php new file mode 100644 index 000000000..a87b5f90a --- /dev/null +++ b/src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php @@ -0,0 +1,22 @@ +prophesize(EntityManagerInterface::class); + $em->persist(Argument::type(Notification::class))->shouldBeCalledTimes(1); + $em->flush()->shouldBeCalledTimes(1); + $event = $this->prophesize(TerminateEvent::class); + $event->isMasterRequest()->willReturn(true); + + $eventSubscriber = new PersistNotificationOnTerminateEventSubscriber($em->reveal(), $persister); + + $notification = new Notification(); + $persister->persist($notification); + + $eventSubscriber->onKernelTerminate($event->reveal()); + } +} diff --git a/src/Bundle/ChillMainBundle/config/services/notification.yaml b/src/Bundle/ChillMainBundle/config/services/notification.yaml index 544f5544c..bb0b6bb8c 100644 --- a/src/Bundle/ChillMainBundle/config/services/notification.yaml +++ b/src/Bundle/ChillMainBundle/config/services/notification.yaml @@ -3,6 +3,11 @@ services: autowire: true autoconfigure: true + Chill\MainBundle\Notification\: + resource: ../../Notification/ + autoconfigure: true + autowire: true + Chill\MainBundle\Notification\Mailer: arguments: $logger: '@Psr\Log\LoggerInterface' @@ -17,12 +22,6 @@ services: arguments: $handlers: !tagged_iterator chill_main.notification_handler - Chill\MainBundle\Notification\NotificationPresence: ~ - - Chill\MainBundle\Notification\Templating\NotificationTwigExtension: ~ - - Chill\MainBundle\Notification\Templating\NotificationTwigExtensionRuntime: ~ - Chill\MainBundle\Notification\Counter\NotificationByUserCounter: autoconfigure: true autowire: true diff --git a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php index ce52fe5e0..6b0004f9b 100644 --- a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php +++ b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\AccompanyingPeriod\Events; use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Notification\NotificationPersisterInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Doctrine\ORM\EntityManagerInterface; use Doctrine\Persistence\Event\LifecycleEventArgs; @@ -24,7 +25,8 @@ use Symfony\Contracts\Translation\TranslatorInterface; class UserRefEventSubscriber implements EventSubscriberInterface { - private EntityManagerInterface $em; + + private NotificationPersisterInterface $notificationPersister; private EngineInterface $engine; @@ -32,12 +34,12 @@ class UserRefEventSubscriber implements EventSubscriberInterface private TranslatorInterface $translator; - public function __construct(Security $security, TranslatorInterface $translator, EngineInterface $engine, EntityManagerInterface $em) + public function __construct(Security $security, TranslatorInterface $translator, EngineInterface $engine, NotificationPersisterInterface $notificationPersister) { $this->security = $security; $this->translator = $translator; $this->engine = $engine; - $this->em = $em; + $this->notificationPersister = $notificationPersister; } public static function getSubscribedEvents() @@ -65,9 +67,6 @@ class UserRefEventSubscriber implements EventSubscriberInterface ) { $this->generateNotificationToUser($period); } - - // we are just out of a flush operation. Launch a new one - $this->em->flush(); } private function generateNotificationToUser(AccompanyingPeriod $period) @@ -89,7 +88,7 @@ class UserRefEventSubscriber implements EventSubscriberInterface )) ->addAddressee($period->getUser()); - $this->em->persist($notification); + $this->notificationPersister->persist($notification); } private function onPeriodConfirmed(AccompanyingPeriod $period)