mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
134 lines
5.3 KiB
PHP
134 lines
5.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\DocGeneratorBundle\Service\Generator;
|
|
|
|
use Chill\DocGeneratorBundle\Context\ContextManagerInterface;
|
|
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
|
|
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
|
|
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;
|
|
|
|
class Generator implements GeneratorInterface
|
|
{
|
|
private const LOG_PREFIX = '[docgen generator] ';
|
|
|
|
public function __construct(private readonly ContextManagerInterface $contextManager, private readonly DriverInterface $driver, private readonly EntityManagerInterface $entityManager, private readonly LoggerInterface $logger, private readonly StoredObjectManagerInterface $storedObjectManager) {}
|
|
|
|
/**
|
|
* @template T of File|null
|
|
* @template B of bool
|
|
* @param B $isTest
|
|
* @param (B is true ? T : null) $testFile
|
|
* @psalm-return (B is true ? string : null)
|
|
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface|\Throwable
|
|
*/
|
|
public function generateDocFromTemplate(
|
|
DocGeneratorTemplate $template,
|
|
int $entityId,
|
|
array $contextGenerationDataNormalized,
|
|
?StoredObject $destinationStoredObject = null,
|
|
bool $isTest = false,
|
|
?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');
|
|
throw new ObjectReadyException();
|
|
}
|
|
|
|
$this->logger->info(self::LOG_PREFIX.'Starting generation of a document', [
|
|
'entity_id' => $entityId,
|
|
'destination_stored_object' => $destinationStoredObject === null ? null : $destinationStoredObject->getId()
|
|
]);
|
|
|
|
$context = $this->contextManager->getContextByDocGeneratorTemplate($template);
|
|
|
|
$entity = $this
|
|
->entityManager
|
|
->find($context->getEntityClass(), $entityId)
|
|
;
|
|
|
|
if (null === $entity) {
|
|
throw new RelatedEntityNotFoundException($template->getEntity(), $entityId);
|
|
}
|
|
|
|
$contextGenerationDataNormalized = array_merge(
|
|
$contextGenerationDataNormalized,
|
|
['creator' => $creator],
|
|
$context instanceof DocGeneratorContextWithPublicFormInterface ?
|
|
$context->contextGenerationDataDenormalize($template, $entity, $contextGenerationDataNormalized)
|
|
: []
|
|
);
|
|
|
|
$data = $context->getData($template, $entity, $contextGenerationDataNormalized);
|
|
|
|
$destinationStoredObjectId = $destinationStoredObject instanceof StoredObject ? $destinationStoredObject->getId() : null;
|
|
$this->entityManager->clear();
|
|
gc_collect_cycles();
|
|
if (null !== $destinationStoredObjectId) {
|
|
$destinationStoredObject = $this->entityManager->find(StoredObject::class, $destinationStoredObjectId);
|
|
}
|
|
|
|
if ($isTest && ($testFile instanceof File)) {
|
|
$templateDecrypted = file_get_contents($testFile->getPathname());
|
|
} else {
|
|
$templateDecrypted = $this->storedObjectManager->read($template->getFile());
|
|
}
|
|
|
|
try {
|
|
$generatedResource = $this
|
|
->driver
|
|
->generateFromString(
|
|
$templateDecrypted,
|
|
$template->getFile()->getType(),
|
|
$data,
|
|
$template->getFile()->getFilename()
|
|
);
|
|
} catch (TemplateException $e) {
|
|
throw new GeneratorException($e->getErrors(), $e);
|
|
}
|
|
|
|
if (true === $isTest) {
|
|
$this->logger->info(self::LOG_PREFIX.'Finished generation of a document', [
|
|
'is_test' => true,
|
|
'entity_id' => $entityId,
|
|
'destination_stored_object' => $destinationStoredObject === null ? null : $destinationStoredObject->getId()
|
|
]);
|
|
return $generatedResource;
|
|
}
|
|
|
|
/** @var StoredObject $destinationStoredObject */
|
|
$destinationStoredObject
|
|
->setType($template->getFile()->getType())
|
|
->setFilename(sprintf('%s_odt', uniqid('doc_', true)))
|
|
->setStatus(StoredObject::STATUS_READY)
|
|
;
|
|
|
|
$this->storedObjectManager->write($destinationStoredObject, $generatedResource);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->logger->info(self::LOG_PREFIX.'Finished generation of a document', [
|
|
'entity_id' => $entityId,
|
|
'destination_stored_object' => $destinationStoredObject->getId(),
|
|
]);
|
|
|
|
return null;
|
|
}
|
|
}
|