From 3836d0dc9b28c8aae84601161cc1c66784ff0364 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 22 Jul 2024 23:40:10 +0200 Subject: [PATCH] Update PdfSignedMessageHandler to manage signature state Additional dependencies have been added to the PdfSignedMessageHandler to handle the state of the signature. After writing the signed message content, the state is set to 'signed' and the state date is updated with the current time. Also, modifications are flushed in the EntityManager to save these changes to the database. Corresponding updates and tests have been made in the PdfSignedMessageHandlerTest file. --- .../BaseSigner/PdfSignedMessageHandler.php | 9 +++++++++ .../BaseSigner/PdfSignedMessageHandlerTest.php | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php b/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php index 81fb97dbf..5cb31de96 100644 --- a/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php +++ b/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php @@ -12,9 +12,12 @@ declare(strict_types=1); namespace Chill\DocStoreBundle\Service\Signature\Driver\BaseSigner; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; +use Chill\MainBundle\Entity\Workflow\EntityWorkflowSignatureStateEnum; use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepSignatureRepository; use Chill\MainBundle\Workflow\EntityWorkflowManager; +use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; +use Symfony\Component\Clock\ClockInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; final readonly class PdfSignedMessageHandler implements MessageHandlerInterface @@ -29,6 +32,8 @@ final readonly class PdfSignedMessageHandler implements MessageHandlerInterface private EntityWorkflowManager $entityWorkflowManager, private StoredObjectManagerInterface $storedObjectManager, private EntityWorkflowStepSignatureRepository $entityWorkflowStepSignatureRepository, + private EntityManagerInterface $entityManager, + private ClockInterface $clock, ) {} public function __invoke(PdfSignedMessage $message): void @@ -48,5 +53,9 @@ final readonly class PdfSignedMessageHandler implements MessageHandlerInterface } $this->storedObjectManager->write($storedObject, $message->content); + + $signature->setState(EntityWorkflowSignatureStateEnum::SIGNED)->setStateDate($this->clock->now()); + $this->entityManager->flush(); + $this->entityManager->clear(); } } diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandlerTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandlerTest.php index 62d50c03f..eb2142175 100644 --- a/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandlerTest.php +++ b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandlerTest.php @@ -22,8 +22,10 @@ use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepSignatureRepository; use Chill\MainBundle\Workflow\EntityWorkflowManager; use Chill\MainBundle\Workflow\WorkflowTransitionContextDTO; use Chill\PersonBundle\Entity\Person; +use Doctrine\ORM\EntityManagerInterface; use PHPUnit\Framework\TestCase; use Psr\Log\NullLogger; +use Symfony\Component\Clock\MockClock; /** * @internal @@ -48,12 +50,16 @@ class PdfSignedMessageHandlerTest extends TestCase new NullLogger(), $this->buildEntityWorkflowManager($storedObject), $this->buildStoredObjectManager($storedObject, $expectedContent = '1234'), - $this->buildSignatureRepository($signature) + $this->buildSignatureRepository($signature), + $this->buildEntityManager(true), + new MockClock('now'), ); // we simply call the handler. The mocked StoredObjectManager will check that the "write" method is invoked once // with the content "1234" $handler(new PdfSignedMessage(10, $expectedContent)); + + self::assertEquals('signed', $signature->getState()->value); } private function buildSignatureRepository(EntityWorkflowStepSignature $signature): EntityWorkflowStepSignatureRepository @@ -81,4 +87,13 @@ class PdfSignedMessageHandlerTest extends TestCase return $storedObjectManager; } + + private function buildEntityManager(bool $willFlush): EntityManagerInterface + { + $em = $this->createMock(EntityManagerInterface::class); + $em->expects($willFlush ? $this->once() : $this->never())->method('flush'); + $em->expects($willFlush ? $this->once() : $this->never())->method('clear'); + + return $em; + } }