mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
Add async handling for signature state changes
Introduce MessageBus to handle post-signature operations asynchronously. This ensures that further steps are executed through dispatched messages, improving system scalability and performance. Implement new handlers and messages for the workflow state transitions.
This commit is contained in:
parent
cfce531754
commit
5287824dbe
@ -21,6 +21,7 @@ use Chill\PersonBundle\Entity\Person;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\Clock\MockClock;
|
||||
use Symfony\Component\Messenger\MessageBus;
|
||||
use Symfony\Component\Workflow\DefinitionBuilder;
|
||||
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
@ -44,7 +45,9 @@ class SignatureStepStateChangerTest extends TestCase
|
||||
$workflow = $registry->get($entityWorkflow, 'dummy');
|
||||
$clock = new MockClock();
|
||||
$user = new User();
|
||||
$changer = new SignatureStepStateChanger($registry, $clock, new NullLogger());
|
||||
|
||||
$messengerBus = new MessageBus([]);
|
||||
$changer = new SignatureStepStateChanger($registry, $clock, new NullLogger(), $messengerBus);
|
||||
|
||||
// move it to signature
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||
@ -61,6 +64,8 @@ class SignatureStepStateChangerTest extends TestCase
|
||||
|
||||
// we mark the first signature as signed
|
||||
$changer->markSignatureAsSigned($signatures[0], 1);
|
||||
// the next step should be done by handling an async message
|
||||
$changer->onPostMark($signatures[0]);
|
||||
|
||||
self::assertEquals('signature', $entityWorkflow->getStep(), 'there should have any change in the entity workflow step');
|
||||
self::assertEquals(EntityWorkflowSignatureStateEnum::SIGNED, $signatures[0]->getState());
|
||||
@ -70,6 +75,8 @@ class SignatureStepStateChangerTest extends TestCase
|
||||
|
||||
// we mark the second signature as signed
|
||||
$changer->markSignatureAsSigned($signatures[1], 2);
|
||||
// the next step should be done by handling an async message
|
||||
$changer->onPostMark($signatures[1]);
|
||||
self::assertEquals(EntityWorkflowSignatureStateEnum::SIGNED, $signatures[1]->getState());
|
||||
self::assertEquals('post-signature', $entityWorkflow->getStep(), 'the entity workflow step should be post-signature');
|
||||
self::assertContains($user, $entityWorkflow->getCurrentStep()->getAllDestUser());
|
||||
@ -85,7 +92,7 @@ class SignatureStepStateChangerTest extends TestCase
|
||||
$workflow = $registry->get($entityWorkflow, 'dummy');
|
||||
$clock = new MockClock();
|
||||
$user = new User();
|
||||
$changer = new SignatureStepStateChanger($registry, $clock, new NullLogger());
|
||||
$changer = new SignatureStepStateChanger($registry, $clock, new NullLogger(), new MessageBus([]));
|
||||
|
||||
// move it to signature
|
||||
$dto = new WorkflowTransitionContextDTO($entityWorkflow);
|
||||
@ -102,6 +109,8 @@ class SignatureStepStateChangerTest extends TestCase
|
||||
|
||||
// we mark the first signature as signed
|
||||
$changer->markSignatureAsSigned($signatures[0], 1);
|
||||
// the next step should be done by handling an async message
|
||||
$changer->onPostMark($signatures[0]);
|
||||
|
||||
self::assertEquals('signature-without-metadata', $entityWorkflow->getStep(), 'there should have any change in the entity workflow step');
|
||||
self::assertEquals(EntityWorkflowSignatureStateEnum::SIGNED, $signatures[0]->getState());
|
||||
|
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Workflow\Messenger;
|
||||
|
||||
use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepSignatureRepository;
|
||||
use Chill\MainBundle\Workflow\SignatureStepStateChanger;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
|
||||
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
|
||||
|
||||
final readonly class PostSignatureStateChangeHandler implements MessageHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private EntityWorkflowStepSignatureRepository $entityWorkflowStepSignatureRepository,
|
||||
private SignatureStepStateChanger $signatureStepStateChanger,
|
||||
private EntityManagerInterface $entityManager,
|
||||
) {}
|
||||
|
||||
public function __invoke(PostSignatureStateChangeMessage $message): void
|
||||
{
|
||||
$signature = $this->entityWorkflowStepSignatureRepository->find($message->signatureId);
|
||||
|
||||
if (null === $signature) {
|
||||
throw new UnrecoverableMessageHandlingException('signature not found');
|
||||
}
|
||||
|
||||
$this->signatureStepStateChanger->onPostMark($signature);
|
||||
|
||||
$this->entityManager->flush();
|
||||
$this->entityManager->clear();
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Workflow\Messenger;
|
||||
|
||||
/**
|
||||
* Message which is dispatched after a notification has a step changed.
|
||||
*/
|
||||
class PostSignatureStateChangeMessage
|
||||
{
|
||||
public function __construct(
|
||||
public int $signatureId,
|
||||
) {}
|
||||
}
|
@ -15,8 +15,10 @@ use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowSignatureStateEnum;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
|
||||
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature;
|
||||
use Chill\MainBundle\Workflow\Messenger\PostSignatureStateChangeMessage;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Clock\ClockInterface;
|
||||
use Symfony\Component\Messenger\MessageBusInterface;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
|
||||
class SignatureStepStateChanger
|
||||
@ -27,6 +29,7 @@ class SignatureStepStateChanger
|
||||
private readonly Registry $registry,
|
||||
private readonly ClockInterface $clock,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly MessageBusInterface $messageBus,
|
||||
) {}
|
||||
|
||||
public function markSignatureAsSigned(EntityWorkflowStepSignature $signature, ?int $atIndex): void
|
||||
@ -36,7 +39,7 @@ class SignatureStepStateChanger
|
||||
->setZoneSignatureIndex($atIndex)
|
||||
->setStateDate($this->clock->now());
|
||||
$this->logger->info(self::LOG_PREFIX.'Mark signature entity as signed', ['signatureId' => $signature->getId(), 'index' => (string) $atIndex]);
|
||||
$this->onPostMark($signature);
|
||||
$this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId()));
|
||||
}
|
||||
|
||||
public function markSignatureAsCanceled(EntityWorkflowStepSignature $signature): void
|
||||
@ -45,6 +48,7 @@ class SignatureStepStateChanger
|
||||
->setState(EntityWorkflowSignatureStateEnum::CANCELED)
|
||||
->setStateDate($this->clock->now());
|
||||
$this->logger->info(self::LOG_PREFIX.'Mark signature entity as canceled', ['signatureId' => $signature->getId()]);
|
||||
$this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId()));
|
||||
}
|
||||
|
||||
public function markSignatureAsRejected(EntityWorkflowStepSignature $signature): void
|
||||
@ -53,9 +57,15 @@ class SignatureStepStateChanger
|
||||
->setState(EntityWorkflowSignatureStateEnum::REJECTED)
|
||||
->setStateDate($this->clock->now());
|
||||
$this->logger->info(self::LOG_PREFIX.'Mark signature entity as rejected', ['signatureId' => $signature->getId()]);
|
||||
$this->messageBus->dispatch(new PostSignatureStateChangeMessage((int) $signature->getId()));
|
||||
}
|
||||
|
||||
private function onPostMark(EntityWorkflowStepSignature $signature): void
|
||||
/**
|
||||
* Executed after a signature has a new state.
|
||||
*
|
||||
* This should be executed only by a system user (without any user registered)
|
||||
*/
|
||||
public function onPostMark(EntityWorkflowStepSignature $signature): void
|
||||
{
|
||||
if (!EntityWorkflowStepSignature::isAllSignatureNotPendingForStep($signature->getStep())) {
|
||||
$this->logger->info(self::LOG_PREFIX.'This is not the last signature, skipping transition to another place', ['signatureId' => $signature->getId()]);
|
||||
|
Loading…
x
Reference in New Issue
Block a user