Persist EntityWorkflow and its steps with EntityManager

Refactored NotificationToUserGroupsOnTransition to utilize EntityManager for persisting EntityWorkflowStep. This ensures entities have generated IDs, leading to proper email notifications during workflow transitions.
This commit is contained in:
Julien Fastré 2024-11-13 12:40:53 +01:00
parent 937caa878e
commit d6c55c830b
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 11 additions and 9 deletions

View File

@ -59,9 +59,7 @@ class NotificationToUserGroupsOnTransitionTest extends KernelTestCase
public function testOnCompletedSendNotificationToUserGroupWithEmailAddress(): void public function testOnCompletedSendNotificationToUserGroupWithEmailAddress(): void
{ {
$entityWorkflow = new EntityWorkflow(); $entityWorkflow = new EntityWorkflow();
$reflection = new \ReflectionClass($entityWorkflow); $this->em->persist($entityWorkflow);
$idProperty = $reflection->getProperty('id');
$idProperty->setValue($entityWorkflow, 1);
$entityWorkflow->setWorkflowName('dummy'); $entityWorkflow->setWorkflowName('dummy');
$dto = new WorkflowTransitionContextDTO($entityWorkflow); $dto = new WorkflowTransitionContextDTO($entityWorkflow);
@ -69,11 +67,7 @@ class NotificationToUserGroupsOnTransitionTest extends KernelTestCase
$ug->setEmail('test@email.com')->setLabel(['fr' => 'test group']); $ug->setEmail('test@email.com')->setLabel(['fr' => 'test group']);
$mailer = $this->prophesize(MailerInterface::class); $mailer = $this->prophesize(MailerInterface::class);
$sendMethod = $mailer->send(Argument::that(function (RawMessage $message) use ($entityWorkflow): bool { $sendMethod = $mailer->send(Argument::that(function (RawMessage $message): bool {
// we need to set an id to the current step of the entity workflow
$this->em->persist($entityWorkflow);
$this->em->persist($entityWorkflow->getCurrentStep());
if (!$message instanceof TemplatedEmail) { if (!$message instanceof TemplatedEmail) {
return false; return false;
} }
@ -140,7 +134,7 @@ class NotificationToUserGroupsOnTransitionTest extends KernelTestCase
} }
}); });
$notificationEventSubscriber = new NotificationToUserGroupsOnTransition($this->twig, $metadataExtractor, $registry, $mailer); $notificationEventSubscriber = new NotificationToUserGroupsOnTransition($this->twig, $metadataExtractor, $registry, $mailer, $this->em);
$eventDispatcher->addSubscriber($notificationEventSubscriber); $eventDispatcher->addSubscriber($notificationEventSubscriber);
return $registry; return $registry;

View File

@ -14,6 +14,7 @@ namespace Chill\MainBundle\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\Notification; use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow; use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor; use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mailer\MailerInterface;
@ -27,6 +28,7 @@ final readonly class NotificationToUserGroupsOnTransition implements EventSubscr
private MetadataExtractor $metadataExtractor, private MetadataExtractor $metadataExtractor,
private Registry $registry, private Registry $registry,
private MailerInterface $mailer, private MailerInterface $mailer,
private EntityManagerInterface $entityManager,
) {} ) {}
public static function getSubscribedEvents(): array public static function getSubscribedEvents(): array
@ -61,6 +63,12 @@ final readonly class NotificationToUserGroupsOnTransition implements EventSubscr
$this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName()) $this->registry->get($entityWorkflow, $entityWorkflow->getWorkflowName())
); );
$currentStep = $entityWorkflow->getCurrentStep();
if (!$this->entityManager->contains($currentStep)) {
// this is necessary to generate an id for the step
$this->entityManager->persist($currentStep);
}
// send to groups // send to groups
foreach ($entityWorkflow->getCurrentStep()->getDestUserGroups() as $userGroup) { foreach ($entityWorkflow->getCurrentStep()->getDestUserGroups() as $userGroup) {
if (!$userGroup->hasEmail()) { if (!$userGroup->hasEmail()) {