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 1/8] 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) From abc3caee00aee53323314370b6406d6be67b1896 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 11:40:29 +0200 Subject: [PATCH 2/8] AccompanyingPeriod: fix method hasPreviousUser --- src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index bd3211c5d..ff26d7b20 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -975,7 +975,7 @@ class AccompanyingPeriod implements public function hasPreviousUser(): bool { - return null !== $this->userPrevious; + return $this->user !== $this->userPrevious; } /** From f7d9551dc13e5131d445b5618875a02b713c95ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 11:41:38 +0200 Subject: [PATCH 3/8] fix cs --- ...NotificationOnTerminateEventSubscriber.php | 12 ++++++++++-- .../Notification/NotificationPersister.php | 19 ++++++++++++++----- .../NotificationPersisterInterface.php | 15 ++++++++++++--- ...ficationOnTerminateEventSubscriberTest.php | 15 ++++++++++++++- .../Events/UserRefEventSubscriber.php | 4 +--- .../Entity/AccompanyingPeriod/Comment.php | 4 ++-- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php index 2739930f5..1f19def23 100644 --- a/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php +++ b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php @@ -1,5 +1,14 @@ [ ['onKernelTerminate', 1024], // we must ensure that the priority is before sending email - ] + ], ]; } diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationPersister.php b/src/Bundle/ChillMainBundle/Notification/NotificationPersister.php index b8bd132ed..d426e3cd7 100644 --- a/src/Bundle/ChillMainBundle/Notification/NotificationPersister.php +++ b/src/Bundle/ChillMainBundle/Notification/NotificationPersister.php @@ -1,5 +1,14 @@ waitingNotifications[] = $notification; - } - /** * @return array|Notification[] */ @@ -20,4 +24,9 @@ class NotificationPersister implements NotificationPersisterInterface { return $this->waitingNotifications; } + + public function persist(Notification $notification): void + { + $this->waitingNotifications[] = $notification; + } } diff --git a/src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php b/src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php index a87b5f90a..da98e305b 100644 --- a/src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php +++ b/src/Bundle/ChillMainBundle/Notification/NotificationPersisterInterface.php @@ -1,11 +1,20 @@ Date: Fri, 22 Apr 2022 12:12:30 +0200 Subject: [PATCH 4/8] period: fix method hasPreviousUser and create method isChangedUser, and use it in Notification --- .../Events/UserRefEventSubscriber.php | 2 +- .../Entity/AccompanyingPeriod.php | 18 +++++++++++++- .../Tests/Entity/AccompanyingPeriodTest.php | 24 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php index 45fe4db70..aacb283c3 100644 --- a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php +++ b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/UserRefEventSubscriber.php @@ -58,7 +58,7 @@ class UserRefEventSubscriber implements EventSubscriberInterface public function postUpdate(AccompanyingPeriod $period, LifecycleEventArgs $args): void { - if ($period->hasPreviousUser() + if ($period->isChangedUser() && $period->getUser() !== $this->security->getUser() && null !== $period->getUser() && $period->getStep() !== AccompanyingPeriod::STEP_DRAFT diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php index ff26d7b20..7615e59fe 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod.php @@ -361,6 +361,8 @@ class AccompanyingPeriod implements */ private Collection $userHistories; + private bool $userIsChanged = false; + /** * Temporary field, which is filled when the user is changed. * @@ -975,7 +977,12 @@ class AccompanyingPeriod implements public function hasPreviousUser(): bool { - return $this->user !== $this->userPrevious; + return null !== $this->userPrevious; + } + + public function isChangedUser(): bool + { + return $this->userIsChanged && $this->user !== $this->userPrevious; } /** @@ -1103,6 +1110,14 @@ class AccompanyingPeriod implements $this->setStep(AccompanyingPeriod::STEP_CONFIRMED); } + public function resetPreviousUser(): self + { + $this->userPrevious = null; + $this->userIsChanged = false; + + return $this; + } + /** * @Groups({"write"}) */ @@ -1329,6 +1344,7 @@ class AccompanyingPeriod implements { if ($this->user !== $user) { $this->userPrevious = $this->user; + $this->userIsChanged = true; foreach ($this->userHistories as $history) { if (null === $history->getEndDate()) { diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php index 0b66d8648..542144bc2 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/AccompanyingPeriodTest.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Tests\Entity; use ArrayIterator; use Chill\MainBundle\Entity\Address; +use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; @@ -62,6 +63,29 @@ final class AccompanyingPeriodTest extends \PHPUnit\Framework\TestCase $this->assertFalse($period->isClosingAfterOpening()); } + public function testHasChangedUser() + { + $period = new AccompanyingPeriod(); + + $this->assertFalse($period->isChangedUser()); + $this->assertFalse($period->hasPreviousUser()); + + $period->setUser($user1 = new User()); + + $this->assertTrue($period->isChangedUser()); + $this->assertFalse($period->hasPreviousUser()); + + $period->resetPreviousUser(); + $this->assertFalse($period->isChangedUser()); + $this->assertFalse($period->hasPreviousUser()); + + $period->setUser($user2 = new User()); + + $this->assertTrue($period->isChangedUser()); + $this->assertTrue($period->hasPreviousUser()); + $this->assertSame($user1, $period->getPreviousUser()); + } + public function testHistoryLocation() { $period = new AccompanyingPeriod(); From 354f130e9e620c374ea48e0b394e6e4b6701d6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 12:12:50 +0200 Subject: [PATCH 5/8] use NotificationPersister into PersonMoveEventSubscriber --- .../PersonAddressMoveEventSubscriber.php | 10 ++--- .../Controller/HouseholdApiController.php | 1 - .../Events/PersonMoveEventSubscriberTest.php | 42 +++++++++---------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonAddressMoveEventSubscriber.php b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonAddressMoveEventSubscriber.php index 3eff58dc7..04dfb25cc 100644 --- a/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonAddressMoveEventSubscriber.php +++ b/src/Bundle/ChillPersonBundle/AccompanyingPeriod/Events/PersonAddressMoveEventSubscriber.php @@ -13,10 +13,10 @@ namespace Chill\PersonBundle\AccompanyingPeriod\Events; use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Notification; +use Chill\MainBundle\Notification\NotificationPersisterInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent; use DateTimeImmutable; -use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Core\Security; use Symfony\Component\Templating\EngineInterface; @@ -26,7 +26,7 @@ class PersonAddressMoveEventSubscriber implements EventSubscriberInterface { private EngineInterface $engine; - private EntityManagerInterface $entityManager; + private NotificationPersisterInterface $notificationPersister; private Security $security; @@ -34,12 +34,12 @@ class PersonAddressMoveEventSubscriber implements EventSubscriberInterface public function __construct( EngineInterface $engine, - EntityManagerInterface $entityManager, + NotificationPersisterInterface $notificationPersister, Security $security, TranslatorInterface $translator ) { $this->engine = $engine; - $this->entityManager = $entityManager; + $this->notificationPersister = $notificationPersister; $this->security = $security; $this->translator = $translator; } @@ -87,7 +87,7 @@ class PersonAddressMoveEventSubscriber implements EventSubscriberInterface 'period' => $period, ])); - $this->entityManager->persist($notification); + $this->notificationPersister->persist($notification); } } } diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php index 470094507..f10c1229f 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php @@ -102,7 +102,6 @@ class HouseholdApiController extends ApiController $event ->setPreviousAddress($household->getPreviousAddressOf($address)) ->setNextAddress($address); - dump($event); $this->eventDispatcher->dispatch($event); } diff --git a/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/Events/PersonMoveEventSubscriberTest.php b/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/Events/PersonMoveEventSubscriberTest.php index 321d69aac..892bd29f5 100644 --- a/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/Events/PersonMoveEventSubscriberTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/AccompanyingPeriod/Events/PersonMoveEventSubscriberTest.php @@ -14,6 +14,8 @@ namespace AccompanyingPeriod\Events; use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Notification\NotificationPersister; +use Chill\MainBundle\Notification\NotificationPersisterInterface; use Chill\PersonBundle\AccompanyingPeriod\Events\PersonAddressMoveEventSubscriber; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Household\Household; @@ -22,7 +24,6 @@ use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent; use DateTime; use DateTimeImmutable; -use Doctrine\ORM\EntityManagerInterface; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use ReflectionClass; @@ -73,9 +74,9 @@ final class PersonMoveEventSubscriberTest extends KernelTestCase ->setPreviousMembership($previousMembership) ->setNextMembership($nextMembership); - $em = $this->prophesize(EntityManagerInterface::class); - $em->persist(Argument::type(Notification::class))->shouldNotBeCalled(); - $eventSubscriber = $this->buildSubscriber(null, $em->reveal(), null, null); + $notificationPersister = $this->prophesize(NotificationPersisterInterface::class); + $notificationPersister->persist(Argument::type(Notification::class))->shouldNotBeCalled(); + $eventSubscriber = $this->buildSubscriber(null, $notificationPersister->reveal(), null, null); $eventSubscriber->resetPeriodLocation($event); } @@ -115,9 +116,9 @@ final class PersonMoveEventSubscriberTest extends KernelTestCase ->setPreviousMembership($previousMembership) ->setNextMembership($nextMembership); - $em = $this->prophesize(EntityManagerInterface::class); - $em->persist(Argument::type(Notification::class))->shouldBeCalledTimes(1); - $eventSubscriber = $this->buildSubscriber(null, $em->reveal(), null, null); + $notificationPersister = $this->prophesize(NotificationPersisterInterface::class); + $notificationPersister->persist(Argument::type(Notification::class))->shouldBeCalledTimes(1); + $eventSubscriber = $this->buildSubscriber(null, $notificationPersister->reveal(), null, null); $eventSubscriber->resetPeriodLocation($event); @@ -160,9 +161,9 @@ final class PersonMoveEventSubscriberTest extends KernelTestCase ->setPreviousMembership($previousMembership) ->setNextMembership($nextMembership); - $em = $this->prophesize(EntityManagerInterface::class); - $em->persist(Argument::type(Notification::class))->shouldBeCalled(1); - $eventSubscriber = $this->buildSubscriber(null, $em->reveal(), null, null); + $notificationPersister = $this->prophesize(NotificationPersisterInterface::class); + $notificationPersister->persist(Argument::type(Notification::class))->shouldBeCalled(1); + $eventSubscriber = $this->buildSubscriber(null, $notificationPersister->reveal(), null, null); $eventSubscriber->resetPeriodLocation($event); @@ -195,9 +196,9 @@ final class PersonMoveEventSubscriberTest extends KernelTestCase $event ->setPreviousMembership($previousMembership); - $em = $this->prophesize(EntityManagerInterface::class); - $em->persist(Argument::type(Notification::class))->shouldBeCalledTimes(1); - $eventSubscriber = $this->buildSubscriber(null, $em->reveal(), null, null); + $notificationPersister = $this->prophesize(NotificationPersisterInterface::class); + $notificationPersister->persist(Argument::type(Notification::class))->shouldBeCalledTimes(1); + $eventSubscriber = $this->buildSubscriber(null, $notificationPersister->reveal(), null, null); $eventSubscriber->resetPeriodLocation($event); @@ -235,9 +236,9 @@ final class PersonMoveEventSubscriberTest extends KernelTestCase ->setPreviousAddress($household->getPreviousAddressOf($newAddress)) ->setNextAddress($newAddress); - $em = $this->prophesize(EntityManagerInterface::class); - $em->persist(Argument::type(Notification::class))->shouldBeCalledTimes(1); - $eventSubscriber = $this->buildSubscriber(null, $em->reveal(), null, null); + $notificationPersister = $this->prophesize(NotificationPersisterInterface::class); + $notificationPersister->persist(Argument::type(Notification::class))->shouldBeCalledTimes(1); + $eventSubscriber = $this->buildSubscriber(null, $notificationPersister->reveal(), null, null); $eventSubscriber->resetPeriodLocation($event); @@ -247,7 +248,7 @@ final class PersonMoveEventSubscriberTest extends KernelTestCase private function buildSubscriber( ?EngineInterface $engine = null, - ?EntityManagerInterface $entityManager = null, + ?NotificationPersisterInterface $notificationPersister = null, ?Security $security = null, ?TranslatorInterface $translator = null ): PersonAddressMoveEventSubscriber { @@ -267,14 +268,13 @@ final class PersonMoveEventSubscriberTest extends KernelTestCase $engine = $double->reveal(); } - if (null === $entityManager) { - $double = $this->prophesize(EntityManagerInterface::class); - $entityManager = $double->reveal(); + if (null === $notificationPersister) { + $notificationPersister = new NotificationPersister(); } return new PersonAddressMoveEventSubscriber( $engine, - $entityManager, + $notificationPersister, $security, $translator ); From 19561c63cbff8a5e507a2b031abcb3da6d738ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 12:31:23 +0200 Subject: [PATCH 6/8] do not persist if no notification is scheduled --- .../PersistNotificationOnTerminateEventSubscriber.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php index 1f19def23..9a61043ff 100644 --- a/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php +++ b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php @@ -46,10 +46,12 @@ class PersistNotificationOnTerminateEventSubscriber implements EventSubscriberIn private function persistNotifications(): void { - foreach ($this->persister->getWaitingNotifications() as $notification) { - $this->em->persist($notification); - } + if (0 < count($this->persister->getWaitingNotifications())) { + foreach ($this->persister->getWaitingNotifications() as $notification) { + $this->em->persist($notification); + } - $this->em->flush(); + $this->em->flush(); + } } } From 6dbe8068ae9e3bf4d2e9195f5be96f6033536218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 12:31:33 +0200 Subject: [PATCH 7/8] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a19aa9495..f7c1c3d1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ and this project adheres to * [parcours]: change wording of warning message and button when user is not associated to a household yet (https://gitlab.com/champs-libres/departement-de-la-vendee/chill/-/issues/590#note_918370943) * [Accompanying period work evaluations] list documents associated to a work by creation date, and then by id, from the most recent to older * [Course comment] add validationConstraint NotNull and NotBlank on comment content, to avoid sql error +* [Notifications] delay the sending of notificaiton to kernel.terminate +* [Notifications / Period user change] fix the sending of notification when user changes ## Test releases From bd0b45b4ddd18bddc8616252c7cfaa130c23577f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 22 Apr 2022 12:47:50 +0200 Subject: [PATCH 8/8] fix cs --- .../PersistNotificationOnTerminateEventSubscriber.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php index 9a61043ff..cb9d2db92 100644 --- a/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php +++ b/src/Bundle/ChillMainBundle/Notification/EventListener/PersistNotificationOnTerminateEventSubscriber.php @@ -15,6 +15,7 @@ use Chill\MainBundle\Notification\NotificationPersisterInterface; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\TerminateEvent; +use function count; class PersistNotificationOnTerminateEventSubscriber implements EventSubscriberInterface {