mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
Fixed: re-introduce creator in async doc generation
This commit is contained in:
@@ -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,
|
||||
|
@@ -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)
|
||||
: []
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user