From 44ecad2bca2d755f832d939cc61ac01e24cc127f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Mar 2023 13:38:19 +0100 Subject: [PATCH] Fixed: re-introduce creator in async doc generation --- .../Service/DocGenerator/ActivityContext.php | 2 +- .../Service/DocGenerator/CalendarContext.php | 2 +- .../DocGenerator/CalendarContextTest.php | 2 +- .../Service/Context/BaseContextData.php | 8 ++---- .../Service/Generator/Generator.php | 5 +++- .../Service/Generator/GeneratorInterface.php | 4 ++- .../Messenger/RequestGenerationHandler.php | 27 +++++++++++++++++-- .../Messenger/RequestGenerationMessage.php | 8 ++++++ .../AccompanyingPeriodContext.php | 2 +- .../Service/DocGenerator/PersonContext.php | 2 +- 10 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php index ac832b34b..3df0d886d 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -152,7 +152,7 @@ class ActivityContext implements $options = $template->getOptions(); $data = []; - $data = array_merge($data, $this->baseContextData->getData()); + $data = array_merge($data, $this->baseContextData->getData($contextGenerationData['creator'] ?? null)); $data['activity'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Activity::class, 'groups' => 'docgen:read']); $data['course'] = $this->normalizer->normalize($entity->getAccompanyingPeriod(), 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); diff --git a/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php b/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php index 6eadaf997..cba7fc661 100644 --- a/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php +++ b/src/Bundle/ChillCalendarBundle/Service/DocGenerator/CalendarContext.php @@ -156,7 +156,7 @@ final class CalendarContext implements CalendarContextInterface $options = $this->getOptions($template); $data = array_merge( - $this->baseContextData->getData(), + $this->baseContextData->getData($contextGenerationData['creator'] ?? null), [ 'calendar' => $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => Calendar::class, 'groups' => ['docgen:read']]), ] diff --git a/src/Bundle/ChillCalendarBundle/Tests/Service/DocGenerator/CalendarContextTest.php b/src/Bundle/ChillCalendarBundle/Tests/Service/DocGenerator/CalendarContextTest.php index be31485d4..cbb4ea3af 100644 --- a/src/Bundle/ChillCalendarBundle/Tests/Service/DocGenerator/CalendarContextTest.php +++ b/src/Bundle/ChillCalendarBundle/Tests/Service/DocGenerator/CalendarContextTest.php @@ -205,7 +205,7 @@ final class CalendarContextTest extends TestCase ?NormalizerInterface $normalizer = null ): CalendarContext { $baseContext = $this->prophesize(BaseContextData::class); - $baseContext->getData()->willReturn(['base_context' => 'data']); + $baseContext->getData(null)->willReturn(['base_context' => 'data']); $personRender = $this->prophesize(PersonRender::class); $personRender->renderString(Argument::type(Person::class), [])->willReturn('person name'); diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php index e7b56ed88..5aed8554b 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php @@ -21,18 +21,14 @@ class BaseContextData { private NormalizerInterface $normalizer; - private Security $security; - - public function __construct(Security $security, NormalizerInterface $normalizer) + public function __construct(NormalizerInterface $normalizer) { - $this->security = $security; $this->normalizer = $normalizer; } - public function getData(): array + public function getData(?User $user = null): array { $data = []; - $user = $this->security->getUser(); $data['creator'] = $this->normalizer->normalize( $user instanceof User ? $user : null, diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php index 0775cfb49..4ddcb461c 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/Generator.php @@ -9,6 +9,7 @@ use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Service\StoredObjectManagerInterface; +use Chill\MainBundle\Entity\User; use Doctrine\ORM\EntityManagerInterface; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\File\File; @@ -55,7 +56,8 @@ class Generator implements GeneratorInterface array $contextGenerationDataNormalized, ?StoredObject $destinationStoredObject = null, bool $isTest = false, - ?File $testFile = null + ?File $testFile = null, + ?User $creator = null ): ?string { if ($destinationStoredObject instanceof StoredObject && StoredObject::STATUS_PENDING !== $destinationStoredObject->getStatus()) { $this->logger->info(self::LOG_PREFIX.'Aborting generation of an already generated document'); @@ -80,6 +82,7 @@ class Generator implements GeneratorInterface $contextGenerationDataNormalized = array_merge( $contextGenerationDataNormalized, + ['creator' => $creator], $context instanceof DocGeneratorContextWithPublicFormInterface ? $context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized) : [] diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php index 385e1d010..a8f01b97c 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Generator/GeneratorInterface.php @@ -4,6 +4,7 @@ namespace Chill\DocGeneratorBundle\Service\Generator; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\StoredObject; +use Chill\MainBundle\Entity\User; use Symfony\Component\HttpFoundation\File\File; interface GeneratorInterface @@ -22,6 +23,7 @@ interface GeneratorInterface array $contextGenerationDataNormalized, ?StoredObject $destinationStoredObject = null, bool $isTest = false, - ?File $testFile = null + ?File $testFile = null, + ?User $creator = null ): ?string; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationHandler.php b/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationHandler.php index 0cfdecd0a..8fef8cf41 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationHandler.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationHandler.php @@ -6,7 +6,9 @@ use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository; use Chill\DocGeneratorBundle\Service\Generator\Generator; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\StoredObjectRepository; +use Chill\MainBundle\Repository\UserRepositoryInterface; use Doctrine\ORM\EntityManagerInterface; +use Psr\Log\LoggerInterface; use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; use Symfony\Component\Messenger\Handler\MessageHandlerInterface; @@ -23,18 +25,28 @@ class RequestGenerationHandler implements MessageHandlerInterface private Generator $generator; + private LoggerInterface $logger; + + private UserRepositoryInterface $userRepository; + public const AUTHORIZED_TRIALS = 5; + private const LOG_PREFIX = '[docgen message handler] '; + public function __construct( DocGeneratorTemplateRepository $docGeneratorTemplateRepository, EntityManagerInterface $entityManager, Generator $generator, - StoredObjectRepository $storedObjectRepository + LoggerInterface $logger, + StoredObjectRepository $storedObjectRepository, + UserRepositoryInterface $userRepository ) { $this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository; $this->entityManager = $entityManager; $this->generator = $generator; + $this->logger = $logger; $this->storedObjectRepository = $storedObjectRepository; + $this->userRepository = $userRepository; } public function __invoke(RequestGenerationMessage $message) @@ -51,6 +63,8 @@ class RequestGenerationHandler implements MessageHandlerInterface throw new UnrecoverableMessageHandlingException('maximum number of retry reached'); } + $creator = $this->userRepository->find($message->getCreatorId()); + $destinationStoredObject->addGenerationTrial(); $this->entityManager->createQuery('UPDATE '.StoredObject::class.' s SET s.generationTrialsCounter = s.generationTrialsCounter + 1 WHERE s.id = :id') ->setParameter('id', $destinationStoredObject->getId()) @@ -60,7 +74,16 @@ class RequestGenerationHandler implements MessageHandlerInterface $template, $message->getEntityId(), $message->getContextGenerationData(), - $destinationStoredObject + $destinationStoredObject, + false, + null, + $creator ); + + $this->logger->info(self::LOG_PREFIX.'Request generation finished', [ + 'template_id' => $message->getTemplateId(), + 'destination_stored_object' => $message->getDestinationStoredObjectId(), + 'duration_int' => (new \DateTimeImmutable('now'))->getTimestamp() - $message->getCreatedAt()->getTimestamp(), + ]); } } diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php b/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php index edba90ce4..8362e4ecc 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Messenger/RequestGenerationMessage.php @@ -18,6 +18,8 @@ class RequestGenerationMessage private array $contextGenerationData; + private \DateTimeImmutable $createdAt; + public function __construct( User $creator, DocGeneratorTemplate $template, @@ -30,6 +32,7 @@ class RequestGenerationMessage $this->entityId = $entityId; $this->destinationStoredObjectId = $destinationStoredObject->getId(); $this->contextGenerationData = $contextGenerationData; + $this->createdAt = new \DateTimeImmutable('now'); } public function getCreatorId(): int @@ -56,4 +59,9 @@ class RequestGenerationMessage { return $this->contextGenerationData; } + + public function getCreatedAt(): \DateTimeImmutable + { + return $this->createdAt; + } } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 9ab733d87..bbfb67b43 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -204,7 +204,7 @@ class AccompanyingPeriodContext implements $options = $template->getOptions(); $data = []; - $data = array_merge($data, $this->baseContextData->getData()); + $data = array_merge($data, $this->baseContextData->getData($contextGenerationData['creator'] ?? null)); $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); foreach (['mainPerson', 'person1', 'person2'] as $k) { diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 988c50829..ff3c4ef49 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -165,7 +165,7 @@ final class PersonContext implements PersonContextInterface } $data = []; - $data = array_merge($data, $this->baseContextData->getData()); + $data = array_merge($data, $this->baseContextData->getData($contextGenerationData['creator'] ?? null)); $data['person'] = $this->normalizer->normalize($entity, 'docgen', [ 'docgen:expects' => Person::class, 'groups' => ['docgen:read'],