From c6658aa2f3a39f37d1500c174be0edf7c878b858 Mon Sep 17 00:00:00 2001 From: nobohan Date: Mon, 22 May 2023 17:15:49 +0200 Subject: [PATCH 01/12] Feature: add thirdParty choice in docgen activityContext --- .../Service/DocGenerator/ActivityContext.php | 57 ++++++++++++++++++- .../translations/messages.fr.yml | 4 +- .../translations/messages.fr.yml | 2 + 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php index 624859eda..d3a770490 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -24,6 +24,9 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Templating\Entity\PersonRenderInterface; +use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender; +use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; @@ -55,6 +58,10 @@ class ActivityContext implements private TranslatorInterface $translator; + private ThirdPartyRender $thirdPartyRender; + + private ThirdPartyRepository $thirdPartyRepository; + public function __construct( DocumentCategoryRepository $documentCategoryRepository, NormalizerInterface $normalizer, @@ -63,7 +70,9 @@ class ActivityContext implements PersonRenderInterface $personRender, PersonRepository $personRepository, TranslatorInterface $translator, - BaseContextData $baseContextData + BaseContextData $baseContextData, + ThirdPartyRender $thirdPartyRender, + ThirdPartyRepository $thirdPartyRepository ) { $this->documentCategoryRepository = $documentCategoryRepository; $this->normalizer = $normalizer; @@ -73,6 +82,8 @@ class ActivityContext implements $this->personRepository = $personRepository; $this->translator = $translator; $this->baseContextData = $baseContextData; + $this->thirdPartyRender = $thirdPartyRender; + $this->thirdPartyRepository = $thirdPartyRepository; } public function adminFormReverseTransform(array $data): array @@ -89,6 +100,8 @@ class ActivityContext implements 'person1Label' => $data['person1Label'] ?? $this->translator->trans('docgen.person 1'), 'person2' => $data['person2'] ?? false, 'person2Label' => $data['person2Label'] ?? $this->translator->trans('docgen.person 2'), + 'thirdParty' => $data['thirdParty'] ?? false, + 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('thirdParty'), ]; } @@ -118,7 +131,15 @@ class ActivityContext implements ->add('person2Label', TextType::class, [ 'label' => 'person 2 label', 'required' => true, - ]); + ]) + ->add('thirdParty', CheckboxType::class, [ + 'required' => false, + 'label' => 'docgen.Ask for thirdParty', + ]) + ->add('thirdPartyLabel', TextType::class, [ + 'label' => 'thirdParty label', + 'required' => true, + ]);; } /** @@ -143,6 +164,21 @@ class ActivityContext implements ]); } } + + $thirdParties = $entity->getThirdParties(); + if ($options['thirdParty'] ?? false) { + $builder->add('thirdParty', EntityType::class, [ + 'class' => ThirdParty::class, + 'choices' => $thirdParties, + 'choice_label' => fn (ThirdParty $p) => $this->thirdPartyRender->renderString($p, []), + 'multiple' => false, + 'required' => false, + 'expanded' => true, + 'label' => $options['thirdPartyLabel'], + 'placeholder' => $this->translator->trans('Any third party selected'), + ]); + } + } public function contextGenerationDataDenormalize(DocGeneratorTemplate $template, $entity, array $data): array @@ -157,6 +193,12 @@ class ActivityContext implements } } + if (null !== ($id = ($data['thirdParty'] ?? null))) { + $denormalized['thirdParty'] = $this->thirdPartyRepository->find($id); + } else { + $denormalized['thirdParty'] = null; + } + return $denormalized; } @@ -168,6 +210,8 @@ class ActivityContext implements $normalized[$k] = null === $data[$k] ? null : $data[$k]->getId(); } + $normalized['thirdParty'] = null === $data['thirdParty'] ? null : $data['thirdParty']->getId(); + return $normalized; } @@ -196,6 +240,13 @@ class ActivityContext implements } } + if ($options['thirdParty']) { + $data['thirdParty'] = $this->normalizer->normalize($contextGenerationData['thirdParty'], 'docgen', [ + 'docgen:expects' => ThirdParty::class, + 'groups' => 'docgen:read' + ]); + } + return $data; } @@ -235,7 +286,7 @@ class ActivityContext implements { $options = $template->getOptions(); - return $options['mainPerson'] || $options['person1'] || $options['person2']; + return $options['mainPerson'] || $options['person1'] || $options['person2'] || $options ['thirdParty']; } public function storeGenerated(DocGeneratorTemplate $template, StoredObject $storedObject, object $entity, array $contextGenerationData): void diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index 341136f65..5cc9ee1f1 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -927,10 +927,10 @@ docgen: Accompanying period work: "Action d'accompagnement" Accompanying period work context: "Evaluation des actions d'accompagnement" Main person: Usager principal - person 1: Premièr usager + person 1: Premier usager person 2: Second usager Ask for main person: Demander à l'utilisateur de préciser l'usager principal - Ask for person 1: Demander à l'utilisateur de préciser le premièr usager + Ask for person 1: Demander à l'utilisateur de préciser le premier usager Ask for person 2: Demander à l'utilisateur de préciser le second usager A basic context for accompanying period: Contexte pour les parcours A context for accompanying period work: Contexte pour les actions d'accompagnement diff --git a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml index 94a10177b..cf1b6105e 100644 --- a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml @@ -75,6 +75,7 @@ No email given: Aucune adresse courriel renseignée The party is visible in those centers: Le tiers est visible dans ces centres The party is not visible in any center: Le tiers n'est associé à aucun centre No third parties: Aucun tiers +Any third party selected: Aucun tiers sélectionné Thirdparty handling: Tiers traitant Thirdparty workers: Tiers intervenants @@ -112,6 +113,7 @@ crud: docgen: A context for person with a third party (for sending mail): Un contexte d'une personne avec un tiers (pour envoyer un courrier à ce tiers, par exemple) Person with third party: Personne avec choix d'un tiers + Ask for thirdParty: Demander à l'utilisateur de préciser un tiers # exports export: From 91a19b9e991a438a527ed25a9fe691f32c084ef3 Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 23 May 2023 17:28:23 +0200 Subject: [PATCH 02/12] Feature: add thirdParty choice in docgen accperiod context --- .../AccompanyingPeriodContext.php | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index a42765c12..53222b753 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -22,10 +22,14 @@ use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; +use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource; use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Templating\Entity\PersonRenderInterface; +use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender; +use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; use DateTime; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\EntityManagerInterface; @@ -62,6 +66,10 @@ class AccompanyingPeriodContext implements private TranslatorInterface $translator; + private ThirdPartyRender $thirdPartyRender; + + private ThirdPartyRepository $thirdPartyRepository; + public function __construct( DocumentCategoryRepository $documentCategoryRepository, NormalizerInterface $normalizer, @@ -70,7 +78,9 @@ class AccompanyingPeriodContext implements PersonRenderInterface $personRender, PersonRepository $personRepository, TranslatorInterface $translator, - BaseContextData $baseContextData + BaseContextData $baseContextData, + ThirdPartyRender $thirdPartyRender, + ThirdPartyRepository $thirdPartyRepository ) { $this->documentCategoryRepository = $documentCategoryRepository; $this->normalizer = $normalizer; @@ -80,6 +90,8 @@ class AccompanyingPeriodContext implements $this->personRepository = $personRepository; $this->translator = $translator; $this->baseContextData = $baseContextData; + $this->thirdPartyRender = $thirdPartyRender; + $this->thirdPartyRepository = $thirdPartyRepository; } public function adminFormReverseTransform(array $data): array @@ -103,6 +115,8 @@ class AccompanyingPeriodContext implements 'person1Label' => $data['person1Label'] ?? $this->translator->trans('docgen.person 1'), 'person2' => $data['person2'] ?? false, 'person2Label' => $data['person2Label'] ?? $this->translator->trans('docgen.person 2'), + 'thirdParty' => $data['thirdParty'] ?? false, + 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('thirdParty'), ]; if (array_key_exists('category', $data)) { @@ -140,6 +154,14 @@ class AccompanyingPeriodContext implements 'label' => 'person 2 label', 'required' => true, ]) + ->add('thirdParty', CheckboxType::class, [ + 'required' => false, + 'label' => 'docgen.Ask for thirdParty', + ]) + ->add('thirdPartyLabel', TextType::class, [ + 'label' => 'thirdParty label', + 'required' => true, + ]) ->add('category', EntityType::class, [ 'placeholder' => 'Choose a document category', 'class' => DocumentCategory::class, @@ -190,6 +212,29 @@ class AccompanyingPeriodContext implements ]); } } + + $thirdParties = array_merge( + array_filter([$entity->getRequestorThirdParty()]), + array_filter(array_map( + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray()) + ) + ); + + if ($options['thirdParty'] ?? false) { + $builder->add('thirdParty', EntityType::class, [ + 'class' => ThirdParty::class, + 'choices' => $thirdParties, + 'choice_label' => fn (ThirdParty $p) => $this->thirdPartyRender->renderString($p, []), + 'multiple' => false, + 'required' => false, + 'expanded' => true, + 'label' => $options['thirdPartyLabel'], + 'placeholder' => $this->translator->trans('Any third party selected'), + ]); + } } public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array @@ -215,6 +260,13 @@ class AccompanyingPeriodContext implements } } + if ($options['thirdParty']) { + $data['thirdParty'] = $this->normalizer->normalize($contextGenerationData['thirdParty'], 'docgen', [ + 'docgen:expects' => ThirdParty::class, + 'groups' => 'docgen:read' + ]); + } + return $data; } @@ -254,7 +306,7 @@ class AccompanyingPeriodContext implements { $options = $template->getOptions(); - return $options['mainPerson'] || $options['person1'] || $options['person2']; + return $options['mainPerson'] || $options['person1'] || $options['person2'] || $options ['thirdParty']; } public function contextGenerationDataNormalize(DocGeneratorTemplate $template, $entity, array $data): array @@ -264,6 +316,8 @@ class AccompanyingPeriodContext implements $normalized[$k] = null !== ($data[$k] ?? null) ? $data[$k]->getId() : null; } + $normalized['thirdParty'] = null === $data['thirdParty'] ? null : $data['thirdParty']->getId(); + return $normalized; } @@ -279,6 +333,12 @@ class AccompanyingPeriodContext implements } } + if (null !== ($id = ($data['thirdParty'] ?? null))) { + $denormalized['thirdParty'] = $this->thirdPartyRepository->find($id); + } else { + $denormalized['thirdParty'] = null; + } + return $denormalized; } From db33fab097c36d2de80f0c498da3f858a925f3da Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 23 May 2023 18:30:18 +0200 Subject: [PATCH 03/12] Feature: add thirdParty choice in docgen accperiodworkevaluation context (WIP) --- ...ccompanyingPeriodWorkEvaluationContext.php | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 34b339822..1c33c9323 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -18,13 +18,18 @@ use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluation; use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWorkEvaluationDocument; +use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource; use Chill\PersonBundle\Entity\SocialWork\Evaluation; use Chill\PersonBundle\Repository\SocialWork\EvaluationRepository; +use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender; +use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; +use Symfony\Contracts\Translation\TranslatorInterface; /** * @implements DocGeneratorContextWithPublicFormInterface @@ -43,18 +48,30 @@ class AccompanyingPeriodWorkEvaluationContext implements private TranslatableStringHelperInterface $translatableStringHelper; + private ThirdPartyRender $thirdPartyRender; + + private ThirdPartyRepository $thirdPartyRepository; + + private TranslatorInterface $translator; + public function __construct( AccompanyingPeriodWorkContext $accompanyingPeriodWorkContext, EntityManagerInterface $em, EvaluationRepository $evaluationRepository, NormalizerInterface $normalizer, - TranslatableStringHelperInterface $translatableStringHelper + TranslatableStringHelperInterface $translatableStringHelper, + ThirdPartyRender $thirdPartyRender, + ThirdPartyRepository $thirdPartyRepository, + TranslatorInterface $translator ) { $this->accompanyingPeriodWorkContext = $accompanyingPeriodWorkContext; $this->em = $em; $this->evaluationRepository = $evaluationRepository; $this->normalizer = $normalizer; $this->translatableStringHelper = $translatableStringHelper; + $this->thirdPartyRender = $thirdPartyRender; + $this->thirdPartyRepository = $thirdPartyRepository; + $this->translator = $translator; } public function adminFormReverseTransform(array $data): array @@ -102,6 +119,39 @@ class AccompanyingPeriodWorkEvaluationContext implements public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { $this->accompanyingPeriodWorkContext->buildPublicForm($builder, $template, $entity->getAccompanyingPeriodWork()); + + //TODO add more choices to thirdParty form + //Question: replace the form or just add more choices to form 'thirdParty'? + + $thirdParties = array_merge( + array_filter($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray()), + array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()]), + array_filter(array_map( + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray()) + ) + + ); + dump($thirdParties); + + $options = $template->getOptions(); + if ($options['thirdParty'] ?? false) { + $builder->add('thirdParty', EntityType::class, [ + 'class' => ThirdParty::class, + 'choices' => $thirdParties, + 'choice_label' => fn (ThirdParty $p) => $this->thirdPartyRender->renderString($p, []), + 'multiple' => false, + 'required' => false, + 'expanded' => true, + 'label' => $options['thirdPartyLabel'], + 'placeholder' => $this->translator->trans('Any third party selected'), + ]); + } + + + } public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array @@ -116,7 +166,7 @@ class AccompanyingPeriodWorkEvaluationContext implements AbstractNormalizer::GROUPS => ['docgen:read'], ] ); - + dump($data); return $data; } From d0867f9aa3d39af5e2f67289849153de82f153cb Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 23 May 2023 23:02:00 +0200 Subject: [PATCH 04/12] Feature: add thirdParty choice in docgen person context --- .../Service/DocGenerator/ActivityContext.php | 4 +- .../AccompanyingPeriodContext.php | 2 +- .../Service/DocGenerator/PersonContext.php | 71 ++++++++++++++++++- .../translations/messages.fr.yml | 1 + 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php index d3a770490..768a0fabe 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -137,9 +137,9 @@ class ActivityContext implements 'label' => 'docgen.Ask for thirdParty', ]) ->add('thirdPartyLabel', TextType::class, [ - 'label' => 'thirdParty label', + 'label' => 'docgen.thirdParty label', 'required' => true, - ]);; + ]); } /** diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 53222b753..29e8c81a3 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -159,7 +159,7 @@ class AccompanyingPeriodContext implements 'label' => 'docgen.Ask for thirdParty', ]) ->add('thirdPartyLabel', TextType::class, [ - 'label' => 'thirdParty label', + 'label' => 'docgen.thirdParty label', 'required' => true, ]) ->add('category', EntityType::class, [ diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 1fdfd23d9..30faa97fd 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -26,14 +26,21 @@ 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\PersonResource; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Entity\Person\ResidentialAddress; use Chill\PersonBundle\Repository\PersonRepository; +use Chill\PersonBundle\Repository\ResidentialAddressRepository; +use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender; +use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; use DateTime; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use LogicException; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Security\Core\Security; @@ -67,6 +74,12 @@ final class PersonContext implements PersonContextInterface private TranslatorInterface $translator; + private ThirdPartyRender $thirdPartyRender; + + private ThirdPartyRepository $thirdPartyRepository; + + private ResidentialAddressRepository $residentialAddressRepository; + public function __construct( AuthorizationHelperInterface $authorizationHelper, BaseContextData $baseContextData, @@ -78,7 +91,10 @@ final class PersonContext implements PersonContextInterface ScopeRepositoryInterface $scopeRepository, Security $security, TranslatorInterface $translator, - TranslatableStringHelperInterface $translatableStringHelper + TranslatableStringHelperInterface $translatableStringHelper, + ThirdPartyRender $thirdPartyRender, + ThirdPartyRepository $thirdPartyRepository, + ResidentialAddressRepository $residentialAddressRepository ) { $this->authorizationHelper = $authorizationHelper; $this->centerResolverManager = $centerResolverManager; @@ -91,6 +107,9 @@ final class PersonContext implements PersonContextInterface $this->showScopes = $parameterBag->get('chill_main')['acl']['form_show_scopes']; $this->translator = $translator; $this->translatableStringHelper = $translatableStringHelper; + $this->thirdPartyRender = $thirdPartyRender; + $this->thirdPartyRepository = $thirdPartyRepository; + $this->residentialAddressRepository = $residentialAddressRepository; } public function adminFormReverseTransform(array $data): array @@ -110,6 +129,8 @@ final class PersonContext implements PersonContextInterface $r = [ 'mainPerson' => $data['mainPerson'] ?? false, 'mainPersonLabel' => $data['mainPersonLabel'] ?? $this->translator->trans('docgen.Main person'), + 'thirdParty' => $data['thirdParty'] ?? false, + 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('thirdParty'), ]; if (array_key_exists('category', $data)) { @@ -131,6 +152,14 @@ final class PersonContext implements PersonContextInterface ->setParameter('docClass', PersonDocument::class), 'choice_label' => fn ($entity = null) => $entity ? $this->translatableStringHelper->localize($entity->getName()) : '', 'required' => true, + ]) + ->add('thirdParty', CheckboxType::class, [ + 'required' => false, + 'label' => 'docgen.Ask for thirdParty', + ]) + ->add('thirdPartyLabel', TextType::class, [ + 'label' => 'docgen.thirdParty label', + 'required' => true, ]); } @@ -139,12 +168,43 @@ final class PersonContext implements PersonContextInterface */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { + $options = $template->getOptions(); + $builder->add('title', TextType::class, [ 'required' => true, 'label' => 'docgen.Document title', 'data' => $this->translatableStringHelper->localize($template->getName()), ]); + $thirdParties = array_merge( + array_filter(array_map( + fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), + $this + ->residentialAddressRepository + ->findCurrentResidentialAddressByPerson($entity) + ) + ), + array_filter(array_map( + fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (PersonResource $r): bool => null !== $r->getThirdParty() + )->toArray()) + ) + ); + + if ($options['thirdParty'] ?? false) { + $builder->add('thirdParty', EntityType::class, [ + 'class' => ThirdParty::class, + 'choices' => $thirdParties, + 'choice_label' => fn (ThirdParty $p) => $this->thirdPartyRender->renderString($p, []), + 'multiple' => false, + 'required' => false, + 'expanded' => true, + 'label' => $options['thirdPartyLabel'], + 'placeholder' => $this->translator->trans('Any third party selected'), + ]); + } + if ($this->isScopeNecessary($entity)) { $builder->add('scope', ScopePickerType::class, [ 'center' => $this->centerResolverManager->resolveCenters($entity), @@ -170,6 +230,13 @@ final class PersonContext implements PersonContextInterface 'docgen:person:with-budget' => true, ]); + if ($template->getOptions()['thirdParty']) { + $data['thirdParty'] = $this->normalizer->normalize($contextGenerationData['thirdParty'], 'docgen', [ + 'docgen:expects' => ThirdParty::class, + 'groups' => 'docgen:read' + ]); + } + return $data; } @@ -223,6 +290,7 @@ final class PersonContext implements PersonContextInterface return [ 'title' => $data['title'] ?? '', 'scope_id' => $scope instanceof Scope ? $scope->getId() : null, + 'thirdParty' => null === $data['thirdParty'] ? null : $data['thirdParty']->getId(), ]; } @@ -242,6 +310,7 @@ final class PersonContext implements PersonContextInterface return [ 'title' => $data['title'] ?? '', 'scope' => $scope, + 'thirdParty' => null !== ($id = ($data['thirdParty'] ?? null)) ? $this->thirdPartyRepository->find($id) : null, ]; } diff --git a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml index cf1b6105e..f62a5beff 100644 --- a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml @@ -114,6 +114,7 @@ docgen: A context for person with a third party (for sending mail): Un contexte d'une personne avec un tiers (pour envoyer un courrier à ce tiers, par exemple) Person with third party: Personne avec choix d'un tiers Ask for thirdParty: Demander à l'utilisateur de préciser un tiers + thirdParty label: Libellé du tiers # exports export: From 303666b30d85e5fa85438225260ec3d144550640 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 25 May 2023 09:50:25 +0200 Subject: [PATCH 05/12] Feature: add thirdParty choice in docgen accperiodworkevaluation context - clean code --- .../DocGenerator/AccompanyingPeriodWorkEvaluationContext.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 1c33c9323..efd687ec9 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -120,9 +120,6 @@ class AccompanyingPeriodWorkEvaluationContext implements { $this->accompanyingPeriodWorkContext->buildPublicForm($builder, $template, $entity->getAccompanyingPeriodWork()); - //TODO add more choices to thirdParty form - //Question: replace the form or just add more choices to form 'thirdParty'? - $thirdParties = array_merge( array_filter($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray()), array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()]), @@ -134,7 +131,6 @@ class AccompanyingPeriodWorkEvaluationContext implements ) ); - dump($thirdParties); $options = $template->getOptions(); if ($options['thirdParty'] ?? false) { @@ -166,7 +162,6 @@ class AccompanyingPeriodWorkEvaluationContext implements AbstractNormalizer::GROUPS => ['docgen:read'], ] ); - dump($data); return $data; } From ef59e6dc1c8e5fb4142df5855b30422443ee91a7 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 25 May 2023 09:59:16 +0200 Subject: [PATCH 06/12] Feature: add thirdParty choice in docgen accperiodworkevaluation context - phpcs fix --- .../Controller/ActivityController.php | 4 ++-- .../Service/DocGenerator/ActivityContext.php | 1 - .../DocGenerator/AccompanyingPeriodContext.php | 6 ++++-- .../AccompanyingPeriodWorkEvaluationContext.php | 10 ++++------ .../Service/DocGenerator/PersonContext.php | 11 +++++++---- .../ChillThirdPartyBundle/Form/ThirdPartyType.php | 2 +- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php index 444c663fc..97678af50 100644 --- a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php +++ b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php @@ -650,8 +650,8 @@ final class ActivityController extends AbstractController throw $this->createNotFoundException('Accompanying Period not found'); } - // TODO Add permission - // $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); + // TODO Add permission + // $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); } else { throw $this->createNotFoundException('Person or Accompanying Period not found'); } diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php index 768a0fabe..91827267c 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -178,7 +178,6 @@ class ActivityContext implements 'placeholder' => $this->translator->trans('Any third party selected'), ]); } - } public function contextGenerationDataDenormalize(DocGeneratorTemplate $template, $entity, array $data): array diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 29e8c81a3..b8923267a 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -215,11 +215,13 @@ class AccompanyingPeriodContext implements $thirdParties = array_merge( array_filter([$entity->getRequestorThirdParty()]), - array_filter(array_map( + array_filter( + array_map( fn (Resource $r): ?ThirdParty => $r->getThirdParty(), $entity->getResources()->filter( static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray()) + )->toArray() + ) ) ); diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index efd687ec9..86e2f8ae9 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -123,13 +123,14 @@ class AccompanyingPeriodWorkEvaluationContext implements $thirdParties = array_merge( array_filter($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray()), array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()]), - array_filter(array_map( + array_filter( + array_map( fn (Resource $r): ?ThirdParty => $r->getThirdParty(), $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray()) + )->toArray() + ) ) - ); $options = $template->getOptions(); @@ -145,9 +146,6 @@ class AccompanyingPeriodWorkEvaluationContext implements 'placeholder' => $this->translator->trans('Any third party selected'), ]); } - - - } public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 30faa97fd..cc46c3341 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -177,18 +177,21 @@ final class PersonContext implements PersonContextInterface ]); $thirdParties = array_merge( - array_filter(array_map( + array_filter( + array_map( fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), $this ->residentialAddressRepository ->findCurrentResidentialAddressByPerson($entity) - ) + ) ), - array_filter(array_map( + array_filter( + array_map( fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), $entity->getResources()->filter( static fn (PersonResource $r): bool => null !== $r->getThirdParty() - )->toArray()) + )->toArray() + ) ) ); diff --git a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php index 6c4984f33..b1446a543 100644 --- a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php +++ b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php @@ -117,7 +117,7 @@ class ThirdPartyType extends AbstractType 'label' => 'thirdparty.Contact data are confidential', ]); - // Institutional ThirdParty (parent) + // Institutional ThirdParty (parent) } else { $builder ->add('nameCompany', TextType::class, [ From 6232dabae5a58383550361a134a58023ade4ce57 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 25 May 2023 10:10:52 +0200 Subject: [PATCH 07/12] Feature: add thirdParty choice in docgen accperiodworkevaluation context - phpstan --- .../DocGenerator/AccompanyingPeriodWorkEvaluationContext.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 86e2f8ae9..75546417f 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -50,8 +50,6 @@ class AccompanyingPeriodWorkEvaluationContext implements private ThirdPartyRender $thirdPartyRender; - private ThirdPartyRepository $thirdPartyRepository; - private TranslatorInterface $translator; public function __construct( @@ -61,7 +59,6 @@ class AccompanyingPeriodWorkEvaluationContext implements NormalizerInterface $normalizer, TranslatableStringHelperInterface $translatableStringHelper, ThirdPartyRender $thirdPartyRender, - ThirdPartyRepository $thirdPartyRepository, TranslatorInterface $translator ) { $this->accompanyingPeriodWorkContext = $accompanyingPeriodWorkContext; @@ -70,7 +67,6 @@ class AccompanyingPeriodWorkEvaluationContext implements $this->normalizer = $normalizer; $this->translatableStringHelper = $translatableStringHelper; $this->thirdPartyRender = $thirdPartyRender; - $this->thirdPartyRepository = $thirdPartyRepository; $this->translator = $translator; } From ad1b28ff112e23d61593cdf438e7bee27957924a Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 25 May 2023 10:15:22 +0200 Subject: [PATCH 08/12] Feature: add thirdParty choice in docgen accperiodworkevaluation context - phpcs fix again --- .../AccompanyingPeriodContext.php | 10 +++++----- ...ccompanyingPeriodWorkEvaluationContext.php | 10 +++++----- .../Service/DocGenerator/PersonContext.php | 20 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index b8923267a..d0ea59090 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -217,11 +217,11 @@ class AccompanyingPeriodContext implements array_filter([$entity->getRequestorThirdParty()]), array_filter( array_map( - fn (Resource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getResources()->filter( - static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray() - ) + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) ) ); diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 75546417f..d7fff210b 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -121,11 +121,11 @@ class AccompanyingPeriodWorkEvaluationContext implements array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()]), array_filter( array_map( - fn (Resource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( - static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray() - ) + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) ) ); diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index cc46c3341..583ac189c 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -179,19 +179,19 @@ final class PersonContext implements PersonContextInterface $thirdParties = array_merge( array_filter( array_map( - fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), - $this - ->residentialAddressRepository - ->findCurrentResidentialAddressByPerson($entity) - ) + fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), + $this + ->residentialAddressRepository + ->findCurrentResidentialAddressByPerson($entity) + ) ), array_filter( array_map( - fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getResources()->filter( - static fn (PersonResource $r): bool => null !== $r->getThirdParty() - )->toArray() - ) + fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (PersonResource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) ) ); From 9593cfde36413190e554c2ec21414f75586ead84 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 25 May 2023 10:32:52 +0200 Subject: [PATCH 09/12] Feature: add thirdParty choice in docgen accperiodworkevaluation context - phpcs fix again --- .../ChillActivityBundle/Controller/ActivityController.php | 4 ++-- src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php index 97678af50..444c663fc 100644 --- a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php +++ b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php @@ -650,8 +650,8 @@ final class ActivityController extends AbstractController throw $this->createNotFoundException('Accompanying Period not found'); } - // TODO Add permission - // $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); + // TODO Add permission + // $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); } else { throw $this->createNotFoundException('Person or Accompanying Period not found'); } diff --git a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php index b1446a543..6c4984f33 100644 --- a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php +++ b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php @@ -117,7 +117,7 @@ class ThirdPartyType extends AbstractType 'label' => 'thirdparty.Contact data are confidential', ]); - // Institutional ThirdParty (parent) + // Institutional ThirdParty (parent) } else { $builder ->add('nameCompany', TextType::class, [ From 1b0569c9744ede4a7b430888ab87a5880afaf516 Mon Sep 17 00:00:00 2001 From: nobohan Date: Thu, 25 May 2023 13:58:46 +0200 Subject: [PATCH 10/12] Feature: add thirdParty choice in docgen context - use array_values --- .../AccompanyingPeriodContext.php | 14 ++++++----- ...ccompanyingPeriodWorkEvaluationContext.php | 16 +++++++------ .../Service/DocGenerator/PersonContext.php | 24 +++++++++++-------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index d0ea59090..66be9aceb 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -214,13 +214,15 @@ class AccompanyingPeriodContext implements } $thirdParties = array_merge( - array_filter([$entity->getRequestorThirdParty()]), + array_filter(array_values([$entity->getRequestorThirdParty()])), array_filter( - array_map( - fn (Resource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getResources()->filter( - static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray() + array_values( + array_map( + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) ) ) ); diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index d7fff210b..62e13789f 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -117,14 +117,16 @@ class AccompanyingPeriodWorkEvaluationContext implements $this->accompanyingPeriodWorkContext->buildPublicForm($builder, $template, $entity->getAccompanyingPeriodWork()); $thirdParties = array_merge( - array_filter($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray()), - array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()]), + array_filter(array_values($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray())), + array_filter(array_values([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()])), array_filter( - array_map( - fn (Resource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( - static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray() + array_values( + array_map( + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) ) ) ); diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 583ac189c..4d49bcfba 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -178,19 +178,23 @@ final class PersonContext implements PersonContextInterface $thirdParties = array_merge( array_filter( - array_map( - fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), - $this - ->residentialAddressRepository - ->findCurrentResidentialAddressByPerson($entity) + array_values( + array_map( + fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), + $this + ->residentialAddressRepository + ->findCurrentResidentialAddressByPerson($entity) + ) ) ), array_filter( - array_map( - fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getResources()->filter( - static fn (PersonResource $r): bool => null !== $r->getThirdParty() - )->toArray() + array_values( + array_map( + fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (PersonResource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) ) ) ); From 80dfa092db2c1e51f6fa33a1f843d785edc7e0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 31 May 2023 23:29:34 +0200 Subject: [PATCH 11/12] fixes: add tests for generation and fix some situation --- .../Service/DocGenerator/ActivityContext.php | 4 +- .../AccompanyingPeriodContext.php | 26 +- ...ccompanyingPeriodWorkEvaluationContext.php | 8 +- .../Service/DocGenerator/PersonContext.php | 23 +- .../DocGenerator/PersonContextInterface.php | 3 +- .../AccompanyingPeriodContextTest.php | 291 ++++++++++++++++++ .../DocGenerator/PersonContextTest.php | 183 ++++++++++- .../Repository/ThirdPartyRepository.php | 2 +- 8 files changed, 503 insertions(+), 37 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php index 91827267c..a20ca365b 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -206,10 +206,10 @@ class ActivityContext implements $normalized = []; foreach (['mainPerson', 'person1', 'person2'] as $k) { - $normalized[$k] = null === $data[$k] ? null : $data[$k]->getId(); + $normalized[$k] = ($data[$k] ?? null)?->getId(); } - $normalized['thirdParty'] = null === $data['thirdParty'] ? null : $data['thirdParty']->getId(); + $normalized['thirdParty'] = ($data['thirdParty'] ?? null)?->getId(); return $normalized; } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 66be9aceb..0db54a47a 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -44,6 +44,7 @@ use Symfony\Contracts\Translation\TranslatorInterface; use function array_key_exists; /** + * @see AccompanyingPeriodContextTest * @template-implements DocGeneratorContextWithPublicFormInterface */ class AccompanyingPeriodContext implements @@ -116,12 +117,11 @@ class AccompanyingPeriodContext implements 'person2' => $data['person2'] ?? false, 'person2Label' => $data['person2Label'] ?? $this->translator->trans('docgen.person 2'), 'thirdParty' => $data['thirdParty'] ?? false, - 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('thirdParty'), + 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('Third party'), ]; if (array_key_exists('category', $data)) { - $r['category'] = array_key_exists('category', $data) ? - $this->documentCategoryRepository->find($data['category']) : null; + $r['category'] = $this->documentCategoryRepository->find($data['category']); } return $r; @@ -214,17 +214,15 @@ class AccompanyingPeriodContext implements } $thirdParties = array_merge( - array_filter(array_values([$entity->getRequestorThirdParty()])), - array_filter( - array_values( - array_map( - fn (Resource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getResources()->filter( - static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray() - ) + array_values(array_filter([$entity->getRequestorThirdParty()])), + array_values(array_filter( + array_map( + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray() ) - ) + )) ); if ($options['thirdParty'] ?? false) { @@ -320,7 +318,7 @@ class AccompanyingPeriodContext implements $normalized[$k] = null !== ($data[$k] ?? null) ? $data[$k]->getId() : null; } - $normalized['thirdParty'] = null === $data['thirdParty'] ? null : $data['thirdParty']->getId(); + $normalized['thirdParty'] = ($data['thirdParty'] ?? null)?->getId(); return $normalized; } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index 62e13789f..ee746a034 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -117,10 +117,10 @@ class AccompanyingPeriodWorkEvaluationContext implements $this->accompanyingPeriodWorkContext->buildPublicForm($builder, $template, $entity->getAccompanyingPeriodWork()); $thirdParties = array_merge( - array_filter(array_values($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray())), - array_filter(array_values([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()])), - array_filter( - array_values( + array_values(array_filter($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray())), + array_values(array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()])), + array_values( + array_filter( array_map( fn (Resource $r): ?ThirdParty => $r->getThirdParty(), $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 4d49bcfba..1ced31121 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -38,6 +38,7 @@ use DateTime; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use LogicException; +use Service\DocGenerator\PersonContextTest; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; @@ -50,6 +51,9 @@ use Symfony\Contracts\Translation\TranslatorInterface; use function array_key_exists; use function count; +/** + * @see PersonContextTest + */ final class PersonContext implements PersonContextInterface { private AuthorizationHelperInterface $authorizationHelper; @@ -130,12 +134,11 @@ final class PersonContext implements PersonContextInterface 'mainPerson' => $data['mainPerson'] ?? false, 'mainPersonLabel' => $data['mainPersonLabel'] ?? $this->translator->trans('docgen.Main person'), 'thirdParty' => $data['thirdParty'] ?? false, - 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('thirdParty'), + 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('Third party'), ]; if (array_key_exists('category', $data)) { - $r['category'] = array_key_exists('category', $data) ? - $this->documentCategoryRepository->find($data['category']) : null; + $r['category'] = $this->documentCategoryRepository->find($data['category']); } return $r; @@ -177,8 +180,8 @@ final class PersonContext implements PersonContextInterface ]); $thirdParties = array_merge( - array_filter( - array_values( + array_values( + array_filter( array_map( fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), $this @@ -187,8 +190,8 @@ final class PersonContext implements PersonContextInterface ) ) ), - array_filter( - array_values( + array_values( + array_filter( array_map( fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), $entity->getResources()->filter( @@ -223,10 +226,6 @@ final class PersonContext implements PersonContextInterface public function getData(DocGeneratorTemplate $template, $entity, array $contextGenerationData = []): array { - if (!$entity instanceof Person) { - throw new UnexpectedTypeException($entity, Person::class); - } - $data = []; $data = array_merge($data, $this->baseContextData->getData($contextGenerationData['creator'] ?? null)); $data['person'] = $this->normalizer->normalize($entity, 'docgen', [ @@ -297,7 +296,7 @@ final class PersonContext implements PersonContextInterface return [ 'title' => $data['title'] ?? '', 'scope_id' => $scope instanceof Scope ? $scope->getId() : null, - 'thirdParty' => null === $data['thirdParty'] ? null : $data['thirdParty']->getId(), + 'thirdParty' => ($data['thirdParty'] ?? null)?->getId(), ]; } diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php index 6de8c1c50..53e241292 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContextInterface.php @@ -19,7 +19,8 @@ use Chill\PersonBundle\Entity\Person; use Symfony\Component\Form\FormBuilderInterface; /** - * @template-extends DocGeneratorContextWithPublicFormInterface + * @template-extends DocGeneratorContextWithPublicFormInterface + * @template-extends DocGeneratorContextWithAdminFormInterface */ interface PersonContextInterface extends DocGeneratorContextWithAdminFormInterface, DocGeneratorContextWithPublicFormInterface { diff --git a/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php new file mode 100644 index 000000000..f9f888999 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/AccompanyingPeriodContextTest.php @@ -0,0 +1,291 @@ +baseContextData = self::$container->get(BaseContextData::class); + $this->documentCategoryRepository = self::$container->get(DocumentCategoryRepository::class); + $this->em = self::$container->get(EntityManagerInterface::class); + $this->normalizer = self::$container->get(NormalizerInterface::class); + $this->personRender = self::$container->get(PersonRenderInterface::class); + $this->personRepository = self::$container->get(PersonRepository::class); + $this->translatableStringHelper = self::$container->get(TranslatableStringHelperInterface::class); + $this->translator = self::$container->get(TranslatorInterface::class); + $this->thirdPartyRender = self::$container->get(ThirdPartyRender::class); + $this->thirdPartyRepository = self::$container->get(ThirdPartyRepository::class); + } + + private function buildContext(): AccompanyingPeriodContext + { + return new AccompanyingPeriodContext( + $this->documentCategoryRepository, + $this->normalizer, + $this->translatableStringHelper, + $this->em, + $this->personRender, + $this->personRepository, + $this->translator, + $this->baseContextData, + $this->thirdPartyRender, + $this->thirdPartyRepository, + ); + } + + /** + * This test run the methods executed when a document is generated: + * + * - normalized data from the form in a way that they are stored in message queue; + * - denormalize the data from the message queue, + * - and get the data, as they will be transmitted to the GeneratorDriver + * + * @param array $options the options, as they are stored in the DocGeneratorTemplate (the admin form data) + * @param AccompanyingPeriod $entity The entity from which the data will be extracted + * @param array $data The data, from the public form + * @param array $expectedNormalized, how the normalized data are expected (allow to check that this data will be compliant with the storage in messenger queue) + * @param callable $assertionsOnData some test that will be executed on the normalized data + * @dataProvider provideNormalizedData + */ + public function testContextGenerationDataNormalizeDenormalizeGetData( + array $options, + AccompanyingPeriod $entity, + array $data, + array $expectedNormalized, + callable $assertionsOnData + ): void { + $context = $this->buildContext(); + $template = new DocGeneratorTemplate(); + $template->setName(["fr" =>"test"])->setContext(AccompanyingPeriodContext::class) + ->setDescription("description")->setActive(true) + ->setOptions($options); + + $normalized = $context->contextGenerationDataNormalize($template, $entity, $data); + + self::assertEquals($expectedNormalized, $normalized); + + $denormalized = $context->contextGenerationDataDenormalize($template, $entity, $normalized); + + $data = $context->getData($template, $entity, $denormalized); + + call_user_func($assertionsOnData, $data); + } + + public function provideNormalizedData(): iterable + { + self::bootKernel(); + $em = self::$container->get(EntityManagerInterface::class); + + $thirdParty = $em->createQuery("SELECT t FROM " . ThirdParty::class . " t") + ->setMaxResults(1) + ->getSingleResult(); + + if (null === $thirdParty) { + throw new \RuntimeException("No thirdparty in database"); + } + + $period = $em->createQuery("SELECT a FROM " . AccompanyingPeriod::class . " a WHERE a.step = 'CONFIRMED'") + ->setMaxResults(1) + ->getSingleResult(); + + if (null === $period) { + throw new \RuntimeException("No confirmed period in database"); + } + + $person = $em->createQuery("SELECT p FROM " . Person::class . " p") + ->setMaxResults(1) + ->getSingleResult(); + + if (null === $person) { + throw new \RuntimeException("No confirmed period in database"); + } + + yield [ + // test with only thirdParty + [ + 'mainPerson' => false, + 'mainPersonLabel' => 'person', + 'person1' => false, + 'person1Label' => 'person2', + 'person2' => false, + 'person2Label' => 'person2', + 'thirdParty' => true, + 'thirdPartyLabel' => '3party' + ], + $period, + [ + 'thirdParty' => $thirdParty + ], + [ + 'thirdParty' => $thirdParty->getId(), + 'mainPerson' => null, + 'person1' => null, + 'person2' => null, + ], + function (array $data) use ($thirdParty, $period) { + self::assertArrayHasKey('thirdParty', $data); + self::assertEquals($thirdParty->getId(), $data['thirdParty']['id']); + + self::assertArrayHasKey('course', $data); + self::assertEquals($period->getId(), $data['course']['id']); + }, + ]; + + yield [ + // test with only mainPerson + [ + 'mainPerson' => true, + 'mainPersonLabel' => 'person', + 'person1' => false, + 'person1Label' => 'person2', + 'person2' => false, + 'person2Label' => 'person2', + 'thirdParty' => false, + 'thirdPartyLabel' => '3party' + ], + $period, + [ + 'mainPerson' => $person, + ], + [ + 'thirdParty' => null, + 'mainPerson' => $person->getId(), + 'person1' => null, + 'person2' => null, + ], + function (array $data) use ($person, $period) { + self::assertArrayHasKey('mainPerson', $data); + self::assertEquals($person->getId(), $data['mainPerson']['id']); + + self::assertArrayHasKey('course', $data); + self::assertEquals($period->getId(), $data['course']['id']); + }, + ]; + + yield [ + // test with every options activated + [ + 'mainPerson' => true, + 'mainPersonLabel' => 'person', + 'person1' => true, + 'person1Label' => 'person2', + 'person2' => true, + 'person2Label' => 'person2', + 'thirdParty' => true, + 'thirdPartyLabel' => '3party' + ], + $period, + [ + 'mainPerson' => $person, + 'person1' => $person, + 'person2' => $person, + 'thirdParty' => $thirdParty, + ], + [ + 'thirdParty' => $thirdParty->getId(), + 'mainPerson' => $person->getId(), + 'person1' => $person->getId(), + 'person2' => $person->getId(), + ], + function (array $data) use ($person, $thirdParty, $period) { + self::assertArrayHasKey('mainPerson', $data); + self::assertEquals($person->getId(), $data['mainPerson']['id']); + + self::assertArrayHasKey('person1', $data); + self::assertEquals($person->getId(), $data['person1']['id']); + + self::assertArrayHasKey('person2', $data); + self::assertEquals($person->getId(), $data['person2']['id']); + + self::assertArrayHasKey('thirdParty', $data); + self::assertEquals($thirdParty->getId(), $data['thirdParty']['id']); + + self::assertArrayHasKey('course', $data); + self::assertEquals($period->getId(), $data['course']['id']); + }, + ]; + + yield [ + // test with any option activated + [ + 'mainPerson' => false, + 'mainPersonLabel' => 'person', + 'person1' => false, + 'person1Label' => 'person2', + 'person2' => false, + 'person2Label' => 'person2', + 'thirdParty' => false, + 'thirdPartyLabel' => '3party' + ], + $period, + [ + 'mainPerson' => null, + 'person1' => null, + 'person2' => null, + 'thirdParty' => null, + ], + [ + 'thirdParty' => null, + 'mainPerson' => null, + 'person1' => null, + 'person2' => null, + ], + function (array $data) use ($period) { + self::assertArrayHasKey('course', $data); + self::assertEquals($period->getId(), $data['course']['id']); + }, + ]; + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php index d0138dc30..71fce8c92 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Service/DocGenerator/PersonContextTest.php @@ -18,20 +18,30 @@ use Chill\DocStoreBundle\Entity\PersonDocument; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; use Chill\DocStoreBundle\Security\Authorization\PersonDocumentVoter; +use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Center; +use Chill\MainBundle\Entity\PostalCode; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Form\Type\ScopePickerType; +use Chill\MainBundle\Repository\ScopeRepositoryInterface; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; +use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Repository\ResidentialAddressRepository; +use Chill\PersonBundle\Service\DocGenerator\AccompanyingPeriodContext; use Chill\PersonBundle\Service\DocGenerator\PersonContext; +use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; +use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender; use Doctrine\ORM\EntityManagerInterface; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\Exception\Prediction\FailedPredictionException; use Prophecy\PhpUnit\ProphecyTrait; +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -46,10 +56,142 @@ use function count; * @internal * @coversNothing */ -final class PersonContextTest extends TestCase +final class PersonContextTest extends KernelTestCase { use ProphecyTrait; + /** + * This test run the methods executed when a document is generated: + * + * - normalized data from the form in a way that they are stored in message queue; + * - denormalize the data from the message queue, + * - and get the data, as they will be transmitted to the GeneratorDriver + * + * @param array $options the options, as they are stored in the DocGeneratorTemplate (the admin form data) + * @param Person $entity The entity from which the data will be extracted + * @param array $data The data, from the public form + * @param array $expectedNormalized, how the normalized data are expected (allow to check that this data will be compliant with the storage in messenger queue) + * @param callable $assertionsOnData some test that will be executed on the normalized data + * @dataProvider provideNormalizedData + */ + public function testContextGenerationDataNormalizeDenormalizeGetData( + array $options, + Person $entity, + array $data, + array $expectedNormalized, + callable $assertionsOnData + ): void { + // we boot kernel only for this test + self::bootKernel(); + + // we create a PersonContext with the minimal dependency injection needed (relying on + // prophecy for other dependencies) + $context = $this->buildPersonContext( + null, + self::$container->get(BaseContextData::class), + self::$container->get(CenterResolverManagerInterface::class), + self::$container->get(DocumentCategoryRepository::class), + self::$container->get(EntityManagerInterface::class), + self::$container->get(NormalizerInterface::class), + (new ParameterBag(['chill_main' => ['acl' => ['form_show_scopes' => false]]])), + null, + self::$container->get(Security::class), + null, + null, + null, + self::$container->get(ThirdPartyRepository::class) + ); + $template = new DocGeneratorTemplate(); + $template->setName(["fr" =>"test"])->setContext(AccompanyingPeriodContext::class) + ->setDescription("description")->setActive(true) + ->setOptions($options); + + $normalized = $context->contextGenerationDataNormalize($template, $entity, $data); + + self::assertEquals($expectedNormalized, $normalized); + + $denormalized = $context->contextGenerationDataDenormalize($template, $entity, $normalized); + + $data = $context->getData($template, $entity, $denormalized); + + call_user_func($assertionsOnData, $data); + } + + public function provideNormalizedData(): iterable + { + self::bootKernel(); + $em = self::$container->get(EntityManagerInterface::class); + + $thirdParty = $em->createQuery("SELECT t FROM " . ThirdParty::class . " t") + ->setMaxResults(1) + ->getSingleResult(); + + if (null === $thirdParty) { + throw new \RuntimeException("No thirdparty in database"); + } + + $person = $em->createQuery("SELECT p FROM " . Person::class . " p") + ->setMaxResults(1) + ->getSingleResult(); + + if (null === $person) { + throw new \RuntimeException("No confirmed period in database"); + } + + $category = self::$container->get(DocumentCategoryRepository::class) + ->findAll()[0]; + + if (null === $category) { + throw new \RuntimeException("no document category in database"); + } + + yield [ + [ + 'thirdParty' => true, + 'thirdPartyLabel' => '3party', + 'category' => $category, + ], + $person, + [ + 'title' => 'test', + 'thirdParty' => $thirdParty, + ], + [ + 'thirdParty' => $thirdParty->getId(), + 'title' => 'test', + 'scope_id' => null, + ], + function ($data) use ($person, $thirdParty) { + self::assertArrayHasKey('person', $data); + self::assertEquals($person->getId(), $data['person']['id']); + + self::assertArrayHasKey('thirdParty', $data); + self::assertEquals($thirdParty->getId(), $data['thirdParty']['id']); + } + ]; + + yield [ + [ + 'thirdParty' => false, + 'thirdPartyLabel' => '3party', + 'category' => $category, + ], + $person, + [ + 'title' => 'test', + ], + [ + 'title' => 'test', + 'scope_id' => null, + 'thirdParty' => null, + ], + function ($data) use ($person, $thirdParty) { + self::assertArrayHasKey('person', $data); + self::assertEquals($person->getId(), $data['person']['id']); + } + ]; + } + /** * Test that the build person context works in the case when 'form_show_scope' is false. */ @@ -208,9 +350,13 @@ final class PersonContextTest extends TestCase ?EntityManagerInterface $em = null, ?NormalizerInterface $normalizer = null, ?ParameterBagInterface $parameterBag = null, + ?ScopeRepositoryInterface $scopeRepository = null, ?Security $security = null, ?TranslatorInterface $translator = null, - ?TranslatableStringHelperInterface $translatableStringHelper = null + ?TranslatableStringHelperInterface $translatableStringHelper = null, + ?ThirdPartyRender $thirdPartyRender = null, + ?ThirdPartyRepository $thirdPartyRepository = null, + ?ResidentialAddressRepository $residentialAddressRepository = null ): PersonContext { if (null === $authorizationHelper) { $authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class)->reveal(); @@ -250,6 +396,11 @@ final class PersonContextTest extends TestCase $parameterBag = new ParameterBag(['chill_main' => ['acl' => ['form_show_scopes' => true]]]); } + if (null === $scopeRepository) { + $scopeRepository = $this->prophesize(ScopeRepositoryInterface::class); + $scopeRepository = $scopeRepository->reveal(); + } + if (null === $security) { $security = $this->prophesize(Security::class); $security->getUser()->willReturn(new User()); @@ -267,6 +418,28 @@ final class PersonContextTest extends TestCase $translatableStringHelper = $translatableStringHelper->reveal(); } + if (null === $thirdPartyRender) { + $thirdPartyRender = $this->prophesize(ThirdPartyRender::class); + $thirdPartyRender = $thirdPartyRender->reveal(); + } + + if (null === $thirdPartyRepository) { + $thirdPartyRepository = $this->prophesize(ThirdPartyRepository::class); + $thirdPartyRepository = $thirdPartyRepository->reveal(); + } + + if (null === $residentialAddressRepository) { + $residentialAddressRepository = $this->prophesize(ResidentialAddressRepository::class); + $residentialAddressRepository->findCurrentResidentialAddressByPerson(Argument::type(Person::class), Argument::any()) + ->willReturn([ + (new Person\ResidentialAddress()) + ->setAddress((new Address()) + ->setStreet('test street') + ->setPostcode(new PostalCode())) + ]); + $residentialAddressRepository = $residentialAddressRepository->reveal(); + } + return new PersonContext( $authorizationHelper, $baseContextData, @@ -275,9 +448,13 @@ final class PersonContextTest extends TestCase $em, $normalizer, $parameterBag, + $scopeRepository, $security, $translator, - $translatableStringHelper + $translatableStringHelper, + $thirdPartyRender, + $thirdPartyRepository, + $residentialAddressRepository ); } } diff --git a/src/Bundle/ChillThirdPartyBundle/Repository/ThirdPartyRepository.php b/src/Bundle/ChillThirdPartyBundle/Repository/ThirdPartyRepository.php index 76e15ef17..f9893b9d9 100644 --- a/src/Bundle/ChillThirdPartyBundle/Repository/ThirdPartyRepository.php +++ b/src/Bundle/ChillThirdPartyBundle/Repository/ThirdPartyRepository.php @@ -21,7 +21,7 @@ use DomainException; use function array_key_exists; -final class ThirdPartyRepository implements ObjectRepository +class ThirdPartyRepository implements ObjectRepository { private EntityRepository $repository; From 9c109d2efdbe78d99d813b3a35abf9c532cf8fa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 31 May 2023 23:47:02 +0200 Subject: [PATCH 12/12] DX: use array spred instead of array_merge --- .../AccompanyingPeriodContext.php | 19 ++++------ ...ccompanyingPeriodWorkEvaluationContext.php | 20 ++++------ .../Service/DocGenerator/PersonContext.php | 37 +++++++++---------- 3 files changed, 33 insertions(+), 43 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 0db54a47a..4f6235930 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -213,17 +213,14 @@ class AccompanyingPeriodContext implements } } - $thirdParties = array_merge( - array_values(array_filter([$entity->getRequestorThirdParty()])), - array_values(array_filter( - array_map( - fn (Resource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getResources()->filter( - static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray() - ) - )) - ); + $thirdParties = [...array_values(array_filter([$entity->getRequestorThirdParty()])), ...array_values(array_filter( + array_map( + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) + ))]; if ($options['thirdParty'] ?? false) { $builder->add('thirdParty', EntityType::class, [ diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php index ee746a034..f58788120 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodWorkEvaluationContext.php @@ -116,20 +116,16 @@ class AccompanyingPeriodWorkEvaluationContext implements { $this->accompanyingPeriodWorkContext->buildPublicForm($builder, $template, $entity->getAccompanyingPeriodWork()); - $thirdParties = array_merge( - array_values(array_filter($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray())), - array_values(array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()])), - array_values( - array_filter( - array_map( - fn (Resource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( - static fn (Resource $r): bool => null !== $r->getThirdParty() - )->toArray() - ) + $thirdParties = [...array_values(array_filter($entity->getAccompanyingPeriodWork()->getThirdParties()->toArray())), ...array_values(array_filter([$entity->getAccompanyingPeriodWork()->getHandlingThierParty()])), ...array_values( + array_filter( + array_map( + fn (Resource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getAccompanyingPeriodWork()->getAccompanyingPeriod()->getResources()->filter( + static fn (Resource $r): bool => null !== $r->getThirdParty() + )->toArray() ) ) - ); + )]; $options = $template->getOptions(); if ($options['thirdParty'] ?? false) { diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 1ced31121..9d7f1cdd5 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -179,28 +179,25 @@ final class PersonContext implements PersonContextInterface 'data' => $this->translatableStringHelper->localize($template->getName()), ]); - $thirdParties = array_merge( - array_values( - array_filter( - array_map( - fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), - $this - ->residentialAddressRepository - ->findCurrentResidentialAddressByPerson($entity) - ) - ) - ), - array_values( - array_filter( - array_map( - fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), - $entity->getResources()->filter( - static fn (PersonResource $r): bool => null !== $r->getThirdParty() - )->toArray() - ) + $thirdParties = [...array_values( + array_filter( + array_map( + fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), + $this + ->residentialAddressRepository + ->findCurrentResidentialAddressByPerson($entity) ) ) - ); + ), ...array_values( + array_filter( + array_map( + fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (PersonResource $r): bool => null !== $r->getThirdParty() + )->toArray() + ) + ) + )]; if ($options['thirdParty'] ?? false) { $builder->add('thirdParty', EntityType::class, [