Files
chill-bundles/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextWithThirdParty.php
Julien Fastré a16244a3f5 Feature: [docgen] generate documents in an async queue
The documents are now generated in a queue, using symfony messenger. This queue should be configured:

```yaml
# app/config/messenger.yaml
framework:
    messenger:
        # reset services after consuming messages
        # reset_on_message: true

        failure_transport: failed

        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
            async: '%env(MESSENGER_TRANSPORT_DSN)%'
            priority:
                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
            failed: 'doctrine://default?queue_name=failed'

        routing:
            # ... other messages
            'Chill\DocGeneratorBundle\Service\Messenger\RequestGenerationMessage': priority
```

`StoredObject`s now have additionnal properties:

* status (pending, failure, ready (by default) ), which explain if the document is generated;
* a generationTrialCounter, which is incremented on each generation trial, which prevent each generation more than 5 times;

The generator computation is moved from the `DocGenTemplateController` to a `Generator` (implementing `GeneratorInterface`. 

There are new methods to `Context` which allow to normalize/denormalize context data to/from a messenger's `Message`.
2023-02-28 15:25:47 +00:00

156 lines
5.0 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\PersonBundle\Service\DocGenerator;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType;
use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Context to generate a document with a destinee (i.e. generate a letter).
*/
class PersonContextWithThirdParty implements DocGeneratorContextWithAdminFormInterface, DocGeneratorContextWithPublicFormInterface
{
private NormalizerInterface $normalizer;
private PersonContextInterface $personContext;
private ThirdPartyRepository $thirdPartyRepository;
public function __construct(
PersonContextInterface $personContext,
NormalizerInterface $normalizer,
ThirdPartyRepository $thirdPartyRepository
) {
$this->personContext = $personContext;
$this->normalizer = $normalizer;
$this->thirdPartyRepository = $thirdPartyRepository;
}
public function adminFormReverseTransform(array $data): array
{
return array_merge(
$this->personContext->adminFormReverseTransform($data),
['label' => $data['label']]
);
}
public function adminFormTransform(array $data): array
{
return array_merge(
$this->personContext->adminFormTransform($data),
['label' => $data['label'] ?? '']
);
}
public function buildAdminForm(FormBuilderInterface $builder): void
{
$this->personContext->buildAdminForm($builder);
$builder->add('label', TextType::class, [
'label' => 'docgen.Label for third party',
'required' => true,
]);
}
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void
{
$this->personContext->buildPublicForm($builder, $template, $entity);
$builder->add('thirdParty', PickThirdpartyDynamicType::class, [
'multiple' => false,
'label' => $template->getOptions()['label'] ?? 'ThirdParty',
'validation_groups' => ['__none__'],
]);
}
public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array
{
$data = $this->personContext->getData($template, $entity, $contextGenerationData);
$data['thirdParty'] = $this->normalizer->normalize(
$contextGenerationData['thirdParty'],
'docgen',
['docgen:expects' => ThirdParty::class, 'groups' => ['docgen:read']]
);
return $data;
}
public function getDescription(): string
{
return 'docgen.A context for person with a third party (for sending mail)';
}
public function getEntityClass(): string
{
return $this->personContext->getEntityClass();
}
public function getFormData(DocGeneratorTemplate $template, $entity): array
{
return $this->personContext->getFormData($template, $entity);
}
public static function getKey(): string
{
return self::class;
}
public function getName(): string
{
return 'docgen.Person with third party';
}
public function hasAdminForm(): bool
{
return true;
}
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool
{
return true;
}
public function contextGenerationDataNormalize(DocGeneratorTemplate $template, $entity, array $data): array
{
return array_merge(
[
'thirdParty' => null === $data['thirdParty'] ? null : $data['thirdParty']->getId(),
],
$this->personContext->contextGenerationDataNormalize($template, $entity, $data),
);
}
public function contextGenerationDataDenormalize(DocGeneratorTemplate $template, $entity, array $data): array
{
return array_merge(
[
'thirdParty' => null === $data['thirdParty'] ? null : $this->thirdPartyRepository->find($data['thirdParty']),
],
$this->personContext->contextGenerationDataDenormalize($template, $entity, $data),
);
}
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void
{
$this->personContext->storeGenerated($template, $storedObject, $entity, $contextGenerationData);
}
}