Merge branch 'master' into upgrade-php82

This commit is contained in:
2023-03-20 12:40:58 +01:00
18 changed files with 133 additions and 70 deletions

View File

@@ -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,

View File

@@ -18,6 +18,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;
@@ -64,7 +65,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');
@@ -89,9 +91,10 @@ class Generator implements GeneratorInterface
$contextGenerationDataNormalized = array_merge(
$contextGenerationDataNormalized,
$context instanceof DocGeneratorContextWithPublicFormInterface ?
$context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized)
: []
['creator' => $creator],
$context instanceof DocGeneratorContextWithPublicFormInterface ?
$context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized)
: []
);
$data = $context->getData($template, $entity, $contextGenerationDataNormalized);

View File

@@ -13,6 +13,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
@@ -31,6 +32,7 @@ interface GeneratorInterface
array $contextGenerationDataNormalized,
?StoredObject $destinationStoredObject = null,
bool $isTest = false,
?File $testFile = null
?File $testFile = null,
?User $creator = null
): ?string;
}

View File

@@ -15,7 +15,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;
@@ -32,18 +34,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)
@@ -60,6 +72,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())
@@ -69,7 +83,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(),
]);
}
}

View File

@@ -27,6 +27,8 @@ class RequestGenerationMessage
private array $contextGenerationData;
private \DateTimeImmutable $createdAt;
public function __construct(
User $creator,
DocGeneratorTemplate $template,
@@ -39,6 +41,7 @@ class RequestGenerationMessage
$this->entityId = $entityId;
$this->destinationStoredObjectId = $destinationStoredObject->getId();
$this->contextGenerationData = $contextGenerationData;
$this->createdAt = new \DateTimeImmutable('now');
}
public function getCreatorId(): int
@@ -65,4 +68,9 @@ class RequestGenerationMessage
{
return $this->contextGenerationData;
}
public function getCreatedAt(): \DateTimeImmutable
{
return $this->createdAt;
}
}