mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
90 lines
3.4 KiB
PHP
90 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Chill\DocGeneratorBundle\Service\Messenger;
|
|
|
|
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;
|
|
|
|
/**
|
|
* Handle the request of document generation
|
|
*/
|
|
class RequestGenerationHandler implements MessageHandlerInterface
|
|
{
|
|
private StoredObjectRepository $storedObjectRepository;
|
|
|
|
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
|
|
|
|
private EntityManagerInterface $entityManager;
|
|
|
|
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,
|
|
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)
|
|
{
|
|
if (null === $template = $this->docGeneratorTemplateRepository->find($message->getTemplateId())) {
|
|
throw new \RuntimeException('template not found: ' . $message->getTemplateId());
|
|
}
|
|
|
|
if (null === $destinationStoredObject = $this->storedObjectRepository->find($message->getDestinationStoredObjectId())) {
|
|
throw new \RuntimeException('destination stored object not found : ' . $message->getDestinationStoredObjectId());
|
|
}
|
|
|
|
if ($destinationStoredObject->getGenerationTrialsCounter() >= self::AUTHORIZED_TRIALS) {
|
|
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())
|
|
->execute();
|
|
|
|
$this->generator->generateDocFromTemplate(
|
|
$template,
|
|
$message->getEntityId(),
|
|
$message->getContextGenerationData(),
|
|
$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(),
|
|
]);
|
|
}
|
|
}
|