Revert "Feature: [docgen][stored object] handler for request generator and required fixes"

This reverts commit 91d21ba939e39af504888be3cced98d0b95a92bb.
This commit is contained in:
Julien Fastré 2023-02-16 14:08:11 +01:00
parent 7c4bc8f46a
commit 9676975cd8
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
18 changed files with 111 additions and 308 deletions

View File

@ -206,16 +206,6 @@ class ActivityContext implements
return $options['mainPerson'] || $options['person1'] || $options['person2'];
}
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormTransform() method.
}
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormReverseTransform() method.
}
/**
* @param Activity $entity
*/

View File

@ -146,16 +146,6 @@ class ListActivitiesByAccompanyingPeriodContext implements
return $this->accompanyingPeriodContext->hasPublicForm($template, $entity);
}
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormTransform() method.
}
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormReverseTransform() method.
}
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
{
$this->accompanyingPeriodContext->storeGenerated($template, $storedObject, $entity, $contextGenerationData);

View File

@ -226,16 +226,6 @@ final class CalendarContext implements CalendarContextInterface
return true;
}
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormTransform() method.
}
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormReverseTransform() method.
}
/**
* @param array{mainPerson?: Person, thirdParty?: ThirdParty, title: string} $contextGenerationData
*/

View File

@ -56,10 +56,6 @@ interface CalendarContextInterface extends DocGeneratorContextWithPublicFormInte
*/
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool;
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array;
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array;
/**
* @param Calendar $entity
*/

View File

@ -23,9 +23,6 @@ interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContext
*/
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void;
/**
* Fill the form with initial data
*/
public function getFormData(DocGeneratorTemplate $template, $entity): array;
/**
@ -34,14 +31,4 @@ interface DocGeneratorContextWithPublicFormInterface extends DocGeneratorContext
* @param mixed $entity
*/
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool;
/**
* Transform the data from the form into serializable data, storable into messenger's message
*/
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array;
/**
* Reverse the data from the messenger's message into data usable for doc's generation
*/
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array;
}

View File

@ -16,28 +16,30 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface;
use Chill\DocGeneratorBundle\GeneratorDriver\Exception\TemplateException;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\DocGeneratorBundle\Service\Generator\GeneratorInterface;
use Chill\DocGeneratorBundle\Service\Messenger\RequestGenerationMessage;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\Service\StoredObjectManagerInterface;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Serializer\Model\Collection;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
// TODO à mettre dans services
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Throwable;
use function strlen;
final class DocGeneratorTemplateController extends AbstractController
@ -52,24 +54,17 @@ final class DocGeneratorTemplateController extends AbstractController
private EntityManagerInterface $entityManager;
private GeneratorInterface $generator;
private LoggerInterface $logger;
private MessageBusInterface $messageBus;
private PaginatorFactory $paginatorFactory;
private StoredObjectManagerInterface $storedObjectManager;
public function __construct(
ContextManager $contextManager,
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
DriverInterface $driver,
GeneratorInterface $generator,
LoggerInterface $logger,
MessageBusInterface $messageBus,
PaginatorFactory $paginatorFactory,
HttpClientInterface $client,
StoredObjectManagerInterface $storedObjectManager,
@ -78,9 +73,7 @@ final class DocGeneratorTemplateController extends AbstractController
$this->contextManager = $contextManager;
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->driver = $driver;
$this->generator = $generator;
$this->logger = $logger;
$this->messageBus = $messageBus;
$this->paginatorFactory = $paginatorFactory;
$this->client = $client;
$this->storedObjectManager = $storedObjectManager;
@ -266,72 +259,99 @@ final class DocGeneratorTemplateController extends AbstractController
}
}
// transform context generation data
$contextGenerationDataSanitized = array_merge(
$contextGenerationData,
$context instanceof DocGeneratorContextWithPublicFormInterface ?
$context->publicFormTransform($template, $entity, $contextGenerationData)
: []
);
$document = $template->getFile();
if ($isTest && ($contextGenerationData['test_file'] instanceof File)) {
$dataDecrypted = file_get_contents($contextGenerationData['test_file']->getPathname());
} else {
try {
$dataDecrypted = $this->storedObjectManager->read($document);
} catch (Throwable $exception) {
throw $exception;
}
}
// if is test, render the data or generate the doc
if ($isTest && isset($form) && $form['show_data']->getData()) {
return $this->render('@ChillDocGenerator/Generator/debug_value.html.twig', [
'datas' => json_encode($context->getData($template, $entity, $contextGenerationData), JSON_PRETTY_PRINT)
]);
} elseif ($isTest) {
$generated = $this->generator->generateDocFromTemplate(
$template,
$entityClassName,
$entityId,
$contextGenerationDataSanitized,
null,
true,
isset($form) ? $form['test_file']->getData() : null
);
}
try {
$generatedResource = $this
->driver
->generateFromString(
$dataDecrypted,
$template->getFile()->getType(),
$context->getData($template, $entity, $contextGenerationData),
$template->getFile()->getFilename()
);
} catch (TemplateException $e) {
return new Response(
$generated,
implode("\n", $e->getErrors()),
400,
[
'Content-Type' => 'text/plain',
]
);
}
if ($isTest) {
return new Response(
$generatedResource,
Response::HTTP_OK,
[
'Content-Transfer-Encoding', 'binary',
'Content-Type' => 'application/vnd.oasis.opendocument.text',
'Content-Disposition' => 'attachment; filename="generated.odt"',
'Content-Length' => strlen($generated),
'Content-Length' => strlen($generatedResource),
],
);
}
// this is not a test
// we prepare the object to store the document
$storedObject = (new StoredObject())
->setStatus(StoredObject::STATUS_PENDING)
;
/** @var StoredObject $storedObject */
$storedObject = (new ObjectNormalizer())
->denormalize(
[
'type' => $template->getFile()->getType(),
'filename' => sprintf('%s_odt', uniqid('doc_', true)),
],
StoredObject::class
);
try {
$this->storedObjectManager->write($storedObject, $generatedResource);
} catch (Throwable $exception) {
throw $exception;
}
$this->entityManager->persist($storedObject);
// we store the generated document
$context
->storeGenerated(
$template,
$storedObject,
$entity,
$contextGenerationData
);
try {
$context
->storeGenerated(
$template,
$storedObject,
$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();
$this->messageBus->dispatch(
new RequestGenerationMessage(
$this->getUser(),
$template,
$entityId,
$entityClassName,
$storedObject,
$contextGenerationDataSanitized,
)
);
return $this
->redirectToRoute(
'chill_wopi_file_edit',

View File

@ -3,7 +3,6 @@
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;
@ -13,7 +12,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\File\File;
class Generator implements GeneratorInterface
class Generator
{
private ContextManagerInterface $contextManager;
@ -51,7 +50,6 @@ class Generator implements GeneratorInterface
DocGeneratorTemplate $template,
string $entityClassName,
int $entityId,
array $contextGenerationData,
?StoredObject $destinationStoredObject = null,
bool $isTest = false,
?File $testFile = null
@ -61,6 +59,7 @@ class Generator implements GeneratorInterface
}
$context = $this->contextManager->getContextByDocGeneratorTemplate($template);
$contextGenerationData = ['test_file' => $testFile];
$entity = $this
->entityManager
@ -71,13 +70,6 @@ class Generator implements GeneratorInterface
throw new RelatedEntityNotFoundException($entityClassName, $entityId);
}
$contextGenerationData = array_merge(
$contextGenerationData,
$context instanceof DocGeneratorContextWithPublicFormInterface ?
$context->publicFormReverseTransform($template, $entity, $contextGenerationData)
: []
);
if ($isTest && ($testFile instanceof File)) {
$dataDecrypted = file_get_contents($testFile->getPathname());
} else {
@ -110,6 +102,29 @@ class Generator implements GeneratorInterface
$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;

View File

@ -1,28 +0,0 @@
<?php
namespace Chill\DocGeneratorBundle\Service\Generator;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject;
use Symfony\Component\HttpFoundation\File\File;
interface GeneratorInterface
{
/**
* @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,
array $contextGenerationData,
?StoredObject $destinationStoredObject = null,
bool $isTest = false,
?File $testFile = null
): ?string;
}

View File

@ -1,50 +0,0 @@
<?php
namespace Chill\DocGeneratorBundle\Service\Messenger;
use Chill\DocGeneratorBundle\Repository\DocGeneratorTemplateRepository;
use Chill\DocGeneratorBundle\Service\Generator\Generator;
use Chill\DocStoreBundle\Repository\StoredObjectRepository;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
/**
* Handle the request of document generation
*/
class RequestGenerationHandler implements MessageHandlerInterface
{
private StoredObjectRepository $storedObjectRepository;
private DocGeneratorTemplateRepository $docGeneratorTemplateRepository;
private Generator $generator;
public function __construct(
StoredObjectRepository $storedObjectRepository,
DocGeneratorTemplateRepository $docGeneratorTemplateRepository,
Generator $generator
) {
$this->storedObjectRepository = $storedObjectRepository;
$this->docGeneratorTemplateRepository = $docGeneratorTemplateRepository;
$this->generator = $generator;
}
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());
}
$this->generator->generateDocFromTemplate(
$template,
$message->getEntityClassName(),
$message->getEntityId(),
$message->getContextGenerationData(),
$destinationStoredObject
);
}
}

View File

@ -3,7 +3,6 @@
namespace Chill\DocGeneratorBundle\Service\Messenger;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\Entity\User;
class RequestGenerationMessage
@ -16,24 +15,12 @@ class RequestGenerationMessage
private string $entityClassName;
private int $destinationStoredObjectId;
private array $contextGenerationData;
public function __construct(
User $creator,
DocGeneratorTemplate $template,
int $entityId,
string $entityClassName,
StoredObject $destinationStoredObject,
array $contextGenerationData
) {
public function __construct(User $creator, DocGeneratorTemplate $template, int $entityId, string $entityClassName)
{
$this->creatorId = $creator->getId();
$this->templateId = $template->getId();
$this->entityId = $entityId;
$this->entityClassName = $entityClassName;
$this->destinationStoredObjectId = $destinationStoredObject->getId();
$this->contextGenerationData = $contextGenerationData;
}
public function getCreatorId(): int
@ -41,11 +28,6 @@ class RequestGenerationMessage
return $this->creatorId;
}
public function getDestinationStoredObjectId(): int
{
return $this->destinationStoredObjectId;
}
public function getTemplateId(): int
{
return $this->templateId;
@ -60,9 +42,4 @@ class RequestGenerationMessage
{
return $this->entityClassName;
}
public function getContextGenerationData(): array
{
return $this->contextGenerationData;
}
}

View File

@ -20,14 +20,10 @@ services:
resource: '../Serializer/Normalizer/'
tags:
- { name: 'serializer.normalizer', priority: -152 }
Chill\DocGeneratorBundle\Serializer\Normalizer\CollectionDocGenNormalizer:
tags:
- { name: 'serializer.normalizer', priority: -126 }
Chill\DocGeneratorBundle\Service\Context\:
resource: "../Service/Context"
Chill\DocGeneratorBundle\Controller\:
resource: "../Controller"
autowire: true
@ -38,20 +34,18 @@ services:
autowire: true
autoconfigure: true
Chill\DocGeneratorBundle\Service\Context\:
resource: "../Service/Context/"
autowire: true
autoconfigure: true
Chill\DocGeneratorBundle\GeneratorDriver\:
resource: "../GeneratorDriver/"
autowire: true
autoconfigure: true
Chill\DocGeneratorBundle\Service\Messenger\:
resource: "../Service/Messenger/"
Chill\DocGeneratorBundle\Service\Generator\Generator: ~
Chill\DocGeneratorBundle\Service\Generator\GeneratorInterface: '@Chill\DocGeneratorBundle\Service\Generator\Generator'
Chill\DocGeneratorBundle\Driver\RelatorioDriver: '@Chill\DocGeneratorBundle\Driver\DriverInterface'
Chill\DocGeneratorBundle\Context\ContextManager:
arguments:
$contexts: !tagged_iterator { tag: chill_docgen.context, default_index_method: getKey }
Chill\DocGeneratorBundle\Context\ContextManagerInterface: '@Chill\DocGeneratorBundle\Context\ContextManager'

View File

@ -118,7 +118,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
* Return true if the phonenumber is a landline or voip phone. Return always true
* if the validation is not configured.
*
* @param string|PhoneNumber $phonenumber
* @param string $phonenumber
*/
public function isValidPhonenumberAny($phonenumber): bool
{
@ -138,7 +138,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
* Return true if the phonenumber is a landline or voip phone. Return always true
* if the validation is not configured.
*
* @param string|PhoneNumber $phonenumber
* @param string $phonenumber
*/
public function isValidPhonenumberLandOrVoip($phonenumber): bool
{
@ -159,7 +159,7 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
* REturn true if the phonenumber is a mobile phone. Return always true
* if the validation is not configured.
*
* @param string|PhoneNumber $phonenumber
* @param string $phonenumber
*/
public function isValidPhonenumberMobile($phonenumber): bool
{
@ -182,10 +182,6 @@ final class PhonenumberHelper implements PhoneNumberHelperInterface
return null;
}
if ($phonenumber instanceof PhoneNumber) {
$phonenumber = (string) $phonenumber;
}
// filter only number
$filtered = preg_replace('/[^0-9]/', '', $phonenumber);

View File

@ -256,16 +256,6 @@ class AccompanyingPeriodContext implements
return $options['mainPerson'] || $options['person1'] || $options['person2'];
}
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormTransform() method.
}
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormReverseTransform() method.
}
/**
* @param AccompanyingPeriod $entity
*/

View File

@ -109,8 +109,6 @@ class AccompanyingPeriodWorkContext
return $this->periodContext->hasPublicForm($template, $entity);
}
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
{
// TODO: Implement storeGenerated() method.

View File

@ -174,16 +174,6 @@ class AccompanyingPeriodWorkEvaluationContext implements
->hasPublicForm($template, $entity->getAccompanyingPeriodWork());
}
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormTransform() method.
}
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormReverseTransform() method.
}
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
{
$doc = new AccompanyingPeriodWorkEvaluationDocument();

View File

@ -21,13 +21,11 @@ use Chill\DocStoreBundle\Repository\DocumentCategoryRepository;
use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter;
use Chill\MainBundle\Entity\Scope;
use Chill\MainBundle\Form\Type\ScopePickerType;
use Chill\MainBundle\Repository\ScopeRepositoryInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\PersonRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
@ -57,8 +55,6 @@ final class PersonContext implements PersonContextInterface
private NormalizerInterface $normalizer;
private ScopeRepositoryInterface $scopeRepository;
private Security $security;
private bool $showScopes;
@ -75,7 +71,6 @@ final class PersonContext implements PersonContextInterface
EntityManagerInterface $em,
NormalizerInterface $normalizer,
ParameterBagInterface $parameterBag,
ScopeRepositoryInterface $scopeRepository,
Security $security,
TranslatorInterface $translator,
TranslatableStringHelperInterface $translatableStringHelper
@ -86,7 +81,6 @@ final class PersonContext implements PersonContextInterface
$this->documentCategoryRepository = $documentCategoryRepository;
$this->em = $em;
$this->normalizer = $normalizer;
$this->scopeRepository = $scopeRepository;
$this->security = $security;
$this->showScopes = $parameterBag->get('chill_main')['acl']['form_show_scopes'];
$this->translator = $translator;
@ -217,38 +211,6 @@ final class PersonContext implements PersonContextInterface
return true;
}
/**
* @param Person $entity
*/
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
$scope = $data['scope'] ?? null;
return [
'title' => $data['title'] ?? '',
'scope_id' => $scope instanceof Scope ? $scope->getId() : null,
];
}
/**
* @param Person $entity
*/
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
if (!isset($data['scope'])) {
$scope = null;
} else {
if (null === $scope = $this->scopeRepository->find($data['scope'])) {
throw new \UnexpectedValueException('scope not found');
}
}
return [
'title' => $data['title'] ?? '',
'scope' => $scope,
];
}
/**
* @param Person $entity
*/

View File

@ -48,10 +48,6 @@ interface PersonContextInterface extends DocGeneratorContextWithAdminFormInterfa
*/
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool;
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array;
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array;
/**
* @param Person $entity
*/

View File

@ -123,16 +123,6 @@ class PersonContextWithThirdParty implements DocGeneratorContextWithAdminFormInt
return true;
}
public function publicFormTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormTransform() method.
}
public function publicFormReverseTransform(DocGeneratorTemplate $template, $entity, array $data): array
{
// TODO: Implement publicFormReverseTransform() method.
}
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
{
$this->personContext->storeGenerated($template, $storedObject, $entity, $contextGenerationData);