From 1897ec354ff0d6a86d8c53fb8d5dde8974107c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Tue, 2 Jan 2024 17:40:04 -0500 Subject: [PATCH] [Messenger] Support DoctrineMessageBusHook for Doctrine ODM --- app/src/Entity/User.php | 4 ++-- .../EnvelopeFactory/BasicEnvelopeFactory.php | 2 +- .../EnvelopeFactory/EnvelopeFactoryInterface.php | 2 +- .../Event/EnvelopeCreatedEvent.php | 2 +- .../EventListener/DoctrineBusMessageListener.php | 14 +++++++------- .../Message/LifeCycleAwareMessageInterface.php | 2 +- .../{Entity => Model}/MessageHolderInterface.php | 2 +- .../{Entity => Model}/MessageHolderTrait.php | 2 +- .../Entity/MessageHolderTraitTest.php | 2 +- .../EnvelopeFactory/BasicEnvelopeFactoryTest.php | 2 +- .../Event/EnvelopeCreatedEventTest.php | 2 +- .../DoctrineBusMessageListenerTest.php | 2 +- .../EnvelopeFactoryDelayStampListenerTest.php | 2 +- ...oryDispatchAfterCurrentBusStampListenerTest.php | 2 +- packages/user-bundle/Entity/LockableUserTrait.php | 3 +-- .../Entity/OnBoardingLifeCycleHookUserTrait.php | 3 +-- .../Entity/PasswordChangeEnforcerUserTrait.php | 3 +-- .../Message/PasswordChangeRequestedMessage.php | 2 +- .../Message/TemporaryUnlockedMessage.php | 2 +- .../AccountLocker/Entity/LockableUserTraitTest.php | 2 +- .../Tests/Message/TemporaryUnlockedMessageTest.php | 4 ++-- 21 files changed, 29 insertions(+), 32 deletions(-) rename packages/messenger/DoctrineMessageBusHook/{Entity => Model}/MessageHolderInterface.php (61%) rename packages/messenger/DoctrineMessageBusHook/{Entity => Model}/MessageHolderTrait.php (87%) diff --git a/app/src/Entity/User.php b/app/src/Entity/User.php index 62a8f140e..59b23e92c 100644 --- a/app/src/Entity/User.php +++ b/app/src/Entity/User.php @@ -22,8 +22,8 @@ use Draw\Bundle\UserBundle\Security\TwoFactorAuthentication\Entity\TwoFactorAuthenticationUserInterface; use Draw\Component\EntityMigrator\MigrationTargetEntityInterface; use Draw\Component\Mailer\Recipient\LocalizationAwareInterface; -use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderInterface; -use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderTrait; +use Draw\Component\Messenger\DoctrineMessageBusHook\Model\MessageHolderInterface; +use Draw\Component\Messenger\DoctrineMessageBusHook\Model\MessageHolderTrait; use Draw\DoctrineExtra\Common\Collections\CollectionUtil; use JMS\Serializer\Annotation as Serializer; use Ramsey\Uuid\Uuid; diff --git a/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/BasicEnvelopeFactory.php b/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/BasicEnvelopeFactory.php index 483cb0a25..91aa6d548 100644 --- a/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/BasicEnvelopeFactory.php +++ b/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/BasicEnvelopeFactory.php @@ -2,8 +2,8 @@ namespace Draw\Component\Messenger\DoctrineMessageBusHook\EnvelopeFactory; -use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderInterface; use Draw\Component\Messenger\DoctrineMessageBusHook\Event\EnvelopeCreatedEvent; +use Draw\Component\Messenger\DoctrineMessageBusHook\Model\MessageHolderInterface; use Symfony\Component\Messenger\Envelope; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; diff --git a/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/EnvelopeFactoryInterface.php b/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/EnvelopeFactoryInterface.php index 7d169f6dd..ced02525f 100644 --- a/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/EnvelopeFactoryInterface.php +++ b/packages/messenger/DoctrineMessageBusHook/EnvelopeFactory/EnvelopeFactoryInterface.php @@ -2,7 +2,7 @@ namespace Draw\Component\Messenger\DoctrineMessageBusHook\EnvelopeFactory; -use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderInterface; +use Draw\Component\Messenger\DoctrineMessageBusHook\Model\MessageHolderInterface; use Symfony\Component\Messenger\Envelope; interface EnvelopeFactoryInterface diff --git a/packages/messenger/DoctrineMessageBusHook/Event/EnvelopeCreatedEvent.php b/packages/messenger/DoctrineMessageBusHook/Event/EnvelopeCreatedEvent.php index ec89a4645..d0715ff41 100644 --- a/packages/messenger/DoctrineMessageBusHook/Event/EnvelopeCreatedEvent.php +++ b/packages/messenger/DoctrineMessageBusHook/Event/EnvelopeCreatedEvent.php @@ -2,7 +2,7 @@ namespace Draw\Component\Messenger\DoctrineMessageBusHook\Event; -use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderInterface; +use Draw\Component\Messenger\DoctrineMessageBusHook\Model\MessageHolderInterface; use Symfony\Component\Messenger\Envelope; use Symfony\Contracts\EventDispatcher\Event; diff --git a/packages/messenger/DoctrineMessageBusHook/EventListener/DoctrineBusMessageListener.php b/packages/messenger/DoctrineMessageBusHook/EventListener/DoctrineBusMessageListener.php index bf073e93e..5240a3d2e 100644 --- a/packages/messenger/DoctrineMessageBusHook/EventListener/DoctrineBusMessageListener.php +++ b/packages/messenger/DoctrineMessageBusHook/EventListener/DoctrineBusMessageListener.php @@ -2,13 +2,12 @@ namespace Draw\Component\Messenger\DoctrineMessageBusHook\EventListener; -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Event\OnClearEventArgs; use Doctrine\Persistence\Event\LifecycleEventArgs; +use Doctrine\Persistence\Event\OnClearEventArgs; use Doctrine\Persistence\Proxy; -use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderInterface; use Draw\Component\Messenger\DoctrineMessageBusHook\EnvelopeFactory\EnvelopeFactoryInterface; use Draw\Component\Messenger\DoctrineMessageBusHook\Message\LifeCycleAwareMessageInterface; +use Draw\Component\Messenger\DoctrineMessageBusHook\Model\MessageHolderInterface; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Contracts\Service\ResetInterface; @@ -72,9 +71,6 @@ public function postFlush(): void } } - /** - * @param LifecycleEventArgs $event - */ private function trackMessageHolder(LifecycleEventArgs $event): void { $entity = $event->getObject(); @@ -86,7 +82,11 @@ private function trackMessageHolder(LifecycleEventArgs $event): void $entityManager = $event->getObjectManager(); $classMetadata = $entityManager->getClassMetadata($entity::class); - $className = $classMetadata->rootEntityName; + + $className = $classMetadata->rootDocumentName + ?? $classMetadata->rootEntityName + ?? $classMetadata->getName(); + $this->messageHolders[$className][spl_object_id($entity)] = $entity; } diff --git a/packages/messenger/DoctrineMessageBusHook/Message/LifeCycleAwareMessageInterface.php b/packages/messenger/DoctrineMessageBusHook/Message/LifeCycleAwareMessageInterface.php index 521fa46b8..9d682b138 100644 --- a/packages/messenger/DoctrineMessageBusHook/Message/LifeCycleAwareMessageInterface.php +++ b/packages/messenger/DoctrineMessageBusHook/Message/LifeCycleAwareMessageInterface.php @@ -2,7 +2,7 @@ namespace Draw\Component\Messenger\DoctrineMessageBusHook\Message; -use Draw\Component\Messenger\DoctrineMessageBusHook\Entity\MessageHolderInterface; +use Draw\Component\Messenger\DoctrineMessageBusHook\Model\MessageHolderInterface; interface LifeCycleAwareMessageInterface { diff --git a/packages/messenger/DoctrineMessageBusHook/Entity/MessageHolderInterface.php b/packages/messenger/DoctrineMessageBusHook/Model/MessageHolderInterface.php similarity index 61% rename from packages/messenger/DoctrineMessageBusHook/Entity/MessageHolderInterface.php rename to packages/messenger/DoctrineMessageBusHook/Model/MessageHolderInterface.php index e6f798e9d..93b483d12 100644 --- a/packages/messenger/DoctrineMessageBusHook/Entity/MessageHolderInterface.php +++ b/packages/messenger/DoctrineMessageBusHook/Model/MessageHolderInterface.php @@ -1,6 +1,6 @@