move logic of context to different interfaces

This commit is contained in:
Julien Fastré 2021-12-02 18:18:11 +01:00
parent af6efdd0ba
commit be626079d0
7 changed files with 71 additions and 171 deletions

View File

@ -13,26 +13,12 @@ namespace Chill\DocGeneratorBundle\Context;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Symfony\Component\Form\FormBuilderInterface;
/** /**
* Interface for context for document generation. * Interface for context for document generation.
*/ */
interface DocGeneratorContextInterface interface DocGeneratorContextInterface
{ {
public function adminFormReverseTransform(array $data): array;
public function adminFormTransform(array $data): array;
public function buildAdminForm(FormBuilderInterface $builder): void;
/**
* Generate the form that display.
*
* @param mixed $entity
*/
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void;
/** /**
* Get the data that will be injected to the generated document. * Get the data that will be injected to the generated document.
* *
@ -48,14 +34,5 @@ interface DocGeneratorContextInterface
public function getName(): string; public function getName(): string;
public function hasAdminForm(): bool;
/**
* has form.
*
* @param mixed $entity
*/
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool;
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void; public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void;
} }

View File

@ -0,0 +1,25 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Context;
use Symfony\Component\Form\FormBuilderInterface;
interface DocGeneratorContextWithAdminFormInterface
{
public function adminFormReverseTransform(array $data): array;
public function adminFormTransform(array $data): array;
public function buildAdminForm(FormBuilderInterface $builder): void;
public function hasAdminForm(): bool;
}

View File

@ -0,0 +1,32 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Context;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Symfony\Component\Form\FormBuilderInterface;
interface DocGeneratorContextWithPublicFormInterface
{
/**
* Generate the form that display.
*
* @param mixed $entity
*/
public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void;
/**
* has form.
*
* @param mixed $entity
*/
public function hasPublicForm(DocGeneratorTemplate $template, $entity): bool;
}

View File

@ -1,143 +0,0 @@
<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Context;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation;
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use function count;
use function get_class;
/**
* Context that display a form to select a member of a houseHold.
*/
class HouseholdMemberSelectionContext //implements DocGeneratorContextInterface
{
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
/**
* Get the data that will be injected to the generated document.
*
* @param mixed $entity
*/
public function getData($entity): array
{
$datas = [
'setValues' => [],
'cloneRowAndSetValues' => [],
];
$persons = $entity->getAccompanyingPeriodWork()->getPersons();
if (count($persons) > 0) {
$firstPerson = $persons[0];
$datas['setValues'][] = [
'firstPersonFirstName' => $firstPerson->getFirstName(),
'firstPersonLastName' => $firstPerson->getLastName(), ];
}
if (get_class($entity) === AccompanyingPeriodWorkEvaluation::class) {
$values = [];
foreach ($entity->getAccompanyingPeriodWork()->getPersons() as $person) {
$i = 1;
$values[] = [
'personRowId' => $i,
'personFirstName' => $person->getFirstName(),
'personLastName' => $person->getLastName(),
];
}
$datas['cloneRowAndSetValues'][] = [
'personRowId', $values, ];
}
return $datas;
}
/**
* Generate the form that display.
*
* @param mixed $entity
*/
public function getForm($entity)
{
throw new Exception('No implemented yet', 1);
$choices = [];
if (get_class($entity) === AccompanyingPeriodWorkEvaluation::class) {
foreach ($entity->getAccompanyingPeriodWork()->getPersons() as $person) {
$choices[$person->getId()] = $person->getName();
}
}
$builder->add('members', ChoiceType::class, [
'choices' => $choices,
'placeholder' => 'Choose a person',
'label' => 'Person to add',
]);
return $builder;
}
public static function getKey(): string
{
return self::class;
}
public function getName(): string
{
return 'household member';
}
/**
* has form.
*/
public function hasForm(): bool
{
return true;
}
public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity): void
{
// Only for evaluation
if ($entity instanceof AccompanyingPeriodWorkEvaluation) {
$doc = new AccompanyingPeriodWorkEvaluationDocument();
$doc
->setStoredObject($storedObject)
->setTemplate($template);
$entity->addDocument($doc);
$this->em->persist($doc);
}
}
/**
* True of false which entity supports.
*/
public function supports(string $entityClass): bool
{
return
(AccompanyingPeriod::class === $entityClass)
|| (SocialAction::class === $entityClass);
}
}

View File

@ -14,6 +14,7 @@ namespace Chill\DocGeneratorBundle\Controller;
use Base64Url\Base64Url; use Base64Url\Base64Url;
use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface; use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface;
use Chill\DocGeneratorBundle\Context\ContextManager; use Chill\DocGeneratorBundle\Context\ContextManager;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface;
@ -103,7 +104,8 @@ final class DocGeneratorTemplateController extends AbstractController
$contextGenerationData = []; $contextGenerationData = [];
if ($context->hasPublicForm($template, $entity)) { if ($context instanceof DocGeneratorContextWithPublicFormInterface
&& $context->hasPublicForm($template, $entity)) {
$builder = $this->createFormBuilder(); $builder = $this->createFormBuilder();
$context->buildPublicForm($builder, $template, $entity); $context->buildPublicForm($builder, $template, $entity);
$form = $builder->getForm()->handleRequest($request); $form = $builder->getForm()->handleRequest($request);

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\DocGeneratorBundle\Form; namespace Chill\DocGeneratorBundle\Form;
use Chill\DocGeneratorBundle\Context\ContextManager; use Chill\DocGeneratorBundle\Context\ContextManager;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\DocStoreBundle\Form\StoredObjectType;
use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Chill\MainBundle\Form\Type\TranslatableStringFormType;
@ -44,7 +45,8 @@ class DocGeneratorTemplateType extends AbstractType
'error_bubbling' => true, 'error_bubbling' => true,
]); ]);
if ($context->hasAdminForm()) { if ($context instanceof DocGeneratorContextWithAdminFormInterface
&& $context->hasAdminForm()) {
$sub = $builder $sub = $builder
->create('options', null, ['compound' => true]) ->create('options', null, ['compound' => true])
->addModelTransformer(new CallbackTransformer( ->addModelTransformer(new CallbackTransformer(

View File

@ -12,6 +12,8 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Service\DocGenerator; namespace Chill\PersonBundle\Service\DocGenerator;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface;
use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface;
use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException;
use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate;
use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument;
@ -31,7 +33,10 @@ use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use function array_key_exists; use function array_key_exists;
class AccompanyingPeriodContext implements DocGeneratorContextInterface class AccompanyingPeriodContext implements
DocGeneratorContextInterface,
DocGeneratorContextWithAdminFormInterface,
DocGeneratorContextWithPublicFormInterface
{ {
private DocumentCategoryRepository $documentCategoryRepository; private DocumentCategoryRepository $documentCategoryRepository;
@ -196,8 +201,8 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface
$doc $doc
->setCategory( ->setCategory(
$this->documentCategoryRepository->find( $this->documentCategoryRepository->find(
$template->getOptions()['category'] $template->getOptions()['category']
) )
); );
} }