diff --git a/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php b/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php index 4002b107b..81fb97dbf 100644 --- a/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php +++ b/src/Bundle/ChillDocStoreBundle/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandler.php @@ -11,6 +11,9 @@ declare(strict_types=1); namespace Chill\DocStoreBundle\Service\Signature\Driver\BaseSigner; +use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; +use Chill\MainBundle\Repository\Workflow\EntityWorkflowStepSignatureRepository; +use Chill\MainBundle\Workflow\EntityWorkflowManager; use Psr\Log\LoggerInterface; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; @@ -23,10 +26,27 @@ final readonly class PdfSignedMessageHandler implements MessageHandlerInterface public function __construct( private LoggerInterface $logger, + private EntityWorkflowManager $entityWorkflowManager, + private StoredObjectManagerInterface $storedObjectManager, + private EntityWorkflowStepSignatureRepository $entityWorkflowStepSignatureRepository, ) {} public function __invoke(PdfSignedMessage $message): void { $this->logger->info(self::P.'a message is received', ['signaturedId' => $message->signatureId]); + + $signature = $this->entityWorkflowStepSignatureRepository->find($message->signatureId); + + if (null === $signature) { + throw new \RuntimeException('no signature found'); + } + + $storedObject = $this->entityWorkflowManager->getAssociatedStoredObject($signature->getStep()->getEntityWorkflow()); + + if (null === $storedObject) { + throw new \RuntimeException('no stored object found'); + } + + $this->storedObjectManager->write($storedObject, $message->content); } } diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandlerTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandlerTest.php new file mode 100644 index 000000000..62d50c03f --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Service/Signature/Driver/BaseSigner/PdfSignedMessageHandlerTest.php @@ -0,0 +1,84 @@ +futurePersonSignatures[] = new Person(); + $entityWorkflow->setStep('new_step', $dto, 'new_transition', new \DateTimeImmutable(), new User()); + $step = $entityWorkflow->getCurrentStep(); + $signature = $step->getSignatures()->first(); + + $handler = new PdfSignedMessageHandler( + new NullLogger(), + $this->buildEntityWorkflowManager($storedObject), + $this->buildStoredObjectManager($storedObject, $expectedContent = '1234'), + $this->buildSignatureRepository($signature) + ); + + // 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)); + } + + private function buildSignatureRepository(EntityWorkflowStepSignature $signature): EntityWorkflowStepSignatureRepository + { + $entityWorkflowStepSignatureRepository = $this->createMock(EntityWorkflowStepSignatureRepository::class); + $entityWorkflowStepSignatureRepository->method('find')->with($this->isType('int'))->willReturn($signature); + + return $entityWorkflowStepSignatureRepository; + } + + private function buildEntityWorkflowManager(?StoredObject $associatedStoredObject): EntityWorkflowManager + { + $entityWorkflowManager = $this->createMock(EntityWorkflowManager::class); + $entityWorkflowManager->method('getAssociatedStoredObject')->willReturn($associatedStoredObject); + + return $entityWorkflowManager; + } + + private function buildStoredObjectManager(StoredObject $expectedStoredObject, string $expectedContent): StoredObjectManagerInterface + { + $storedObjectManager = $this->createMock(StoredObjectManagerInterface::class); + $storedObjectManager->expects($this->once()) + ->method('write') + ->with($this->identicalTo($expectedStoredObject), $expectedContent); + + return $storedObjectManager; + } +}