mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 02:23:51 +00:00
Feature: [docgen] create a service to generate a document from a template
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Service\Generator;
|
||||
|
||||
use Chill\DocGeneratorBundle\Context\ContextManagerInterface;
|
||||
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 Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
|
||||
class Generator
|
||||
{
|
||||
private ContextManagerInterface $contextManager;
|
||||
|
||||
private DriverInterface $driver;
|
||||
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
private LoggerInterface $logger;
|
||||
|
||||
private StoredObjectManagerInterface $storedObjectManager;
|
||||
|
||||
public function __construct(
|
||||
ContextManagerInterface $contextManager,
|
||||
DriverInterface $driver,
|
||||
EntityManagerInterface $entityManager,
|
||||
LoggerInterface $logger,
|
||||
StoredObjectManagerInterface $storedObjectManager
|
||||
) {
|
||||
$this->contextManager = $contextManager;
|
||||
$this->driver = $driver;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->logger = $logger;
|
||||
$this->storedObjectManager = $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,
|
||||
string $entityClassName,
|
||||
int $entityId,
|
||||
?StoredObject $destinationStoredObject = null,
|
||||
bool $isTest = false,
|
||||
?File $testFile = null
|
||||
): ?string {
|
||||
if ($destinationStoredObject instanceof StoredObject && StoredObject::STATUS_PENDING !== $destinationStoredObject->getStatus()) {
|
||||
throw new ObjectReadyException();
|
||||
}
|
||||
|
||||
$context = $this->contextManager->getContextByDocGeneratorTemplate($template);
|
||||
$contextGenerationData = ['test_file' => $testFile];
|
||||
|
||||
$entity = $this
|
||||
->entityManager
|
||||
->find($context->getEntityClass(), $entityId)
|
||||
;
|
||||
|
||||
if (null === $entity) {
|
||||
throw new RelatedEntityNotFoundException($entityClassName, $entityId);
|
||||
}
|
||||
|
||||
if ($isTest && ($testFile instanceof File)) {
|
||||
$dataDecrypted = file_get_contents($testFile->getPathname());
|
||||
} else {
|
||||
$dataDecrypted = $this->storedObjectManager->read($template->getFile());
|
||||
}
|
||||
|
||||
try {
|
||||
$generatedResource = $this
|
||||
->driver
|
||||
->generateFromString(
|
||||
$dataDecrypted,
|
||||
$template->getFile()->getType(),
|
||||
$context->getData($template, $entity, $contextGenerationData),
|
||||
$template->getFile()->getFilename()
|
||||
);
|
||||
} catch (TemplateException $e) {
|
||||
throw new GeneratorException($e->getErrors(), $e);
|
||||
}
|
||||
|
||||
if ($isTest) {
|
||||
return $generatedResource;
|
||||
}
|
||||
|
||||
/** @var StoredObject $storedObject */
|
||||
$destinationStoredObject
|
||||
->setType($template->getFile()->getType())
|
||||
->setFilename(sprintf('%s_odt', uniqid('doc_', true)))
|
||||
->setStatus(StoredObject::STATUS_READY)
|
||||
;
|
||||
|
||||
$this->storedObjectManager->write($destinationStoredObject, $generatedResource);
|
||||
|
||||
try {
|
||||
$context
|
||||
->storeGenerated(
|
||||
$template,
|
||||
$destinationStoredObject,
|
||||
$entity,
|
||||
$contextGenerationData
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$this
|
||||
->logger
|
||||
->error(
|
||||
'Unable to store the associated document to entity',
|
||||
[
|
||||
'entityClassName' => $entityClassName,
|
||||
'entityId' => $entityId,
|
||||
'contextKey' => $context->getName(),
|
||||
]
|
||||
);
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
$this->entityManager->flush();
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Service\Generator;
|
||||
|
||||
class GeneratorException extends \RuntimeException
|
||||
{
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
private array $errors;
|
||||
|
||||
public function __construct(array $errors = [], \Throwable $previous = null)
|
||||
{
|
||||
$this->errors = $errors;
|
||||
parent::__construct("Could not generate the document", 15252,
|
||||
$previous);
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Service\Generator;
|
||||
|
||||
class ObjectReadyException extends \RuntimeException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct("object is already ready", 6698856);
|
||||
}
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\DocGeneratorBundle\Service\Generator;
|
||||
|
||||
class RelatedEntityNotFoundException extends \RuntimeException
|
||||
{
|
||||
public function __construct(string $relatedEntityClass, int $relatedEntityId, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf("Related entity not found: %s, %s", $relatedEntityClass, $relatedEntityId),
|
||||
99876652,
|
||||
$previous);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user