From be626079d0c98ee5aee6b1936fdfbeceda2426d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 2 Dec 2021 18:18:11 +0100 Subject: [PATCH] move logic of context to different interfaces --- .../Context/DocGeneratorContextInterface.php | 23 --- ...GeneratorContextWithAdminFormInterface.php | 25 +++ ...eneratorContextWithPublicFormInterface.php | 32 ++++ .../HouseholdMemberSelectionContext.php | 143 ------------------ .../DocGeneratorTemplateController.php | 4 +- .../Form/DocGeneratorTemplateType.php | 4 +- .../AccompanyingPeriodContext.php | 11 +- 7 files changed, 71 insertions(+), 171 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithPublicFormInterface.php delete mode 100644 src/Bundle/ChillDocGeneratorBundle/Context/HouseholdMemberSelectionContext.php diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php index 9bca0444d..df148f6e1 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextInterface.php @@ -13,26 +13,12 @@ namespace Chill\DocGeneratorBundle\Context; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\StoredObject; -use Symfony\Component\Form\FormBuilderInterface; /** * Interface for context for document generation. */ 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. * @@ -48,14 +34,5 @@ interface DocGeneratorContextInterface 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; } diff --git a/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php new file mode 100644 index 000000000..28ad70170 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Context/DocGeneratorContextWithAdminFormInterface.php @@ -0,0 +1,25 @@ +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); - } -} diff --git a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php index a4dd27f2e..1f5043d82 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php +++ b/src/Bundle/ChillDocGeneratorBundle/Controller/DocGeneratorTemplateController.php @@ -14,6 +14,7 @@ namespace Chill\DocGeneratorBundle\Controller; use Base64Url\Base64Url; use ChampsLibres\AsyncUploaderBundle\TempUrl\TempUrlGeneratorInterface; use Chill\DocGeneratorBundle\Context\ContextManager; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\ContextNotFoundException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocGeneratorBundle\GeneratorDriver\DriverInterface; @@ -103,7 +104,8 @@ final class DocGeneratorTemplateController extends AbstractController $contextGenerationData = []; - if ($context->hasPublicForm($template, $entity)) { + if ($context instanceof DocGeneratorContextWithPublicFormInterface + && $context->hasPublicForm($template, $entity)) { $builder = $this->createFormBuilder(); $context->buildPublicForm($builder, $template, $entity); $form = $builder->getForm()->handleRequest($request); diff --git a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php index 158c7bddc..f52962a41 100644 --- a/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php +++ b/src/Bundle/ChillDocGeneratorBundle/Form/DocGeneratorTemplateType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\DocGeneratorBundle\Form; use Chill\DocGeneratorBundle\Context\ContextManager; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Form\StoredObjectType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; @@ -44,7 +45,8 @@ class DocGeneratorTemplateType extends AbstractType 'error_bubbling' => true, ]); - if ($context->hasAdminForm()) { + if ($context instanceof DocGeneratorContextWithAdminFormInterface + && $context->hasAdminForm()) { $sub = $builder ->create('options', null, ['compound' => true]) ->addModelTransformer(new CallbackTransformer( diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 182f7aab9..26dc29b25 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -12,6 +12,8 @@ declare(strict_types=1); namespace Chill\PersonBundle\Service\DocGenerator; use Chill\DocGeneratorBundle\Context\DocGeneratorContextInterface; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; +use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; @@ -31,7 +33,10 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use function array_key_exists; -class AccompanyingPeriodContext implements DocGeneratorContextInterface +class AccompanyingPeriodContext implements + DocGeneratorContextInterface, + DocGeneratorContextWithAdminFormInterface, + DocGeneratorContextWithPublicFormInterface { private DocumentCategoryRepository $documentCategoryRepository; @@ -196,8 +201,8 @@ class AccompanyingPeriodContext implements DocGeneratorContextInterface $doc ->setCategory( $this->documentCategoryRepository->find( - $template->getOptions()['category'] - ) + $template->getOptions()['category'] + ) ); }