engine = $engine; $this->entityManager = $entityManager; $this->security = $security; $this->translator = $translator; } public static function getSubscribedEvents(): array { return [ PersonAddressMoveEvent::class => 'resetPeriodLocation', ]; } public function resetPeriodLocation(PersonAddressMoveEvent $event) { if ($event->getPreviousAddress() !== $event->getNextAddress() && null !== $event->getPreviousAddress() ) { $person = $event->getPerson(); foreach ($person->getCurrentAccompanyingPeriods() as $period) { if ($period->getStep() === AccompanyingPeriod::STEP_DRAFT) { continue; } $now = new \DateTimeImmutable('now'); if ( $period->getPersonLocation() === $person && ( $event->getMoveDate() >= $period->getLastLocationHistory()->getStartDate() || ( $event->getNextAddress()->getValidFrom() < $now && (null === $event->getNextAddress()->getValidTo() || $event->getNextAddress()->getValidTo() > $now) ) ) && null !== $period->getUser() && $period->getUser() !== $this->security->getUser() ) { // reset the location, back to an address $period->setPersonLocation(null); $period->setAddressLocation(Address::createFromAddress($event->getPreviousAddress())); $notification = new Notification(); $notification ->addAddressee($period->getUser()) ->setTitle($this->translator->trans('period_notification.Person locating period has moved')) ->setRelatedEntityClass(AccompanyingPeriod::class) ->setRelatedEntityId($period->getId()) ->setMessage($this->engine->render('@ChillPerson/AccompanyingPeriod/notification_location_user_on_period_has_moved.fr.txt.twig', [ 'oldPersonLocation' => $person, 'period' => $period, ])); $this->entityManager->persist($notification); } } } } }