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

View File

@ -14,6 +14,7 @@ namespace Chill\MainBundle\Workflow\EventSubscriber;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Workflow\Helper\MetadataExtractor;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
@ -27,6 +28,7 @@ final readonly class NotificationToUserGroupsOnTransition implements EventSubscr
private MetadataExtractor $metadataExtractor,
private Registry $registry,
private MailerInterface $mailer,
private EntityManagerInterface $entityManager,
) {}
public static function getSubscribedEvents(): array
@ -61,6 +63,12 @@ final readonly class NotificationToUserGroupsOnTransition implements EventSubscr
$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
foreach ($entityWorkflow->getCurrentStep()->getDestUserGroups() as $userGroup) {
if (!$userGroup->hasEmail()) {