From d4a5735e1562251b0f67d189a6f748b74f5c0ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 15 Dec 2021 22:43:06 +0000 Subject: [PATCH] Docgen/add base context --- CHANGELOG.md | 2 + .../Service/Context/BaseContextData.php | 53 +++++++++++++++ .../config/services.yaml | 5 ++ .../Service/Context/BaseContextDataTest.php | 68 +++++++++++++++++++ .../Controller/LocationApiController.php | 8 +-- .../ChillMainBundle/Entity/Location.php | 12 ++-- .../ChillMainBundle/Entity/LocationType.php | 5 +- .../Normalizer/PersonDocGenNormalizer.php | 3 +- .../AccompanyingPeriodContext.php | 10 ++- .../Normalizer/PersonDocGenNormalizerTest.php | 1 + .../Entity/ThirdParty.php | 2 - 11 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php create mode 100644 src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index db1a07606..67a26b73c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to * AddAddress: optimize loading: wait for the user finish typing; * UserPicker: fix bug with deprecated role +* docgen: add base context + tests +* docgen: add age for person ## Test releases diff --git a/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php new file mode 100644 index 000000000..0e2da164f --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/Service/Context/BaseContextData.php @@ -0,0 +1,53 @@ +security = $security; + $this->normalizer = $normalizer; + } + + public function getData(): array + { + $data = []; + $user = $this->security->getUser(); + + $data['creator'] = $this->normalizer->normalize( + $user instanceof User ? $user : null, + 'docgen', + ['docgen:expects' => User::class, 'groups' => ['docgen:read']] + ); + $data['createdAt'] = $this->normalizer->normalize(new DateTimeImmutable(), 'docgen', [ + 'docgen:expects' => DateTimeImmutable::class, 'groups' => ['docgen:read'], + ]); + $data['location'] = $this->normalizer->normalize( + $user instanceof User ? $user->getCurrentLocation() : null, + 'docgen', + ['docgen:expects' => Location::class, 'groups' => ['docgen:read']] + ); + + return $data; + } +} diff --git a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml index 54bf5a697..5bdfe2a11 100644 --- a/src/Bundle/ChillDocGeneratorBundle/config/services.yaml +++ b/src/Bundle/ChillDocGeneratorBundle/config/services.yaml @@ -34,6 +34,11 @@ services: autowire: true autoconfigure: true + Chill\DocGeneratorBundle\Service\Context\: + resource: "../Service/Context/" + autowire: true + autoconfigure: true + Chill\DocGeneratorBundle\GeneratorDriver\: resource: "../GeneratorDriver/" autowire: true diff --git a/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php new file mode 100644 index 000000000..1960fbcb9 --- /dev/null +++ b/src/Bundle/ChillDocGeneratorBundle/tests/Service/Context/BaseContextDataTest.php @@ -0,0 +1,68 @@ +buildBaseContext(); + + $actual = $context->getData(); + + $this->assertIsArray($actual); + $this->assertArrayHasKey('creator', $actual); + $this->assertArrayHasKey('createdAt', $actual); + $this->assertArrayHasKey('location', $actual); + } + + public function testGenerateWithUser() + { + $security = $this->prophesize(Security::class); + $security->getUser()->willReturn(new User()); + + $context = $this->buildBaseContext($security->reveal()); + + $actual = $context->getData(); + + $this->assertIsArray($actual); + $this->assertArrayHasKey('creator', $actual); + $this->assertArrayHasKey('createdAt', $actual); + $this->assertArrayHasKey('location', $actual); + } + + private function buildBaseContext( + ?Security $security = null, + ?NormalizerInterface $normalizer = null + ): BaseContextData { + return new BaseContextData( + $security ?? self::$container->get(Security::class), + $normalizer ?? self::$container->get(NormalizerInterface::class) + ); + } +} diff --git a/src/Bundle/ChillMainBundle/Controller/LocationApiController.php b/src/Bundle/ChillMainBundle/Controller/LocationApiController.php index aa5f46f1a..250d92980 100644 --- a/src/Bundle/ChillMainBundle/Controller/LocationApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/LocationApiController.php @@ -25,10 +25,10 @@ class LocationApiController extends ApiController ->leftJoin('e.locationType', 'lt') ->andWhere( $query->expr()->andX( - $query->expr()->eq('e.availableForUsers', "'TRUE'"), - $query->expr()->eq('lt.availableForUsers', "'TRUE'"), - $query->expr()->eq('e.active', "'TRUE'"), - ) + $query->expr()->eq('e.availableForUsers', "'TRUE'"), + $query->expr()->eq('lt.availableForUsers', "'TRUE'"), + $query->expr()->eq('e.active', "'TRUE'"), + ) ); } } diff --git a/src/Bundle/ChillMainBundle/Entity/Location.php b/src/Bundle/ChillMainBundle/Entity/Location.php index ba1ad8fb0..8496374d8 100644 --- a/src/Bundle/ChillMainBundle/Entity/Location.php +++ b/src/Bundle/ChillMainBundle/Entity/Location.php @@ -40,7 +40,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\ManyToOne(targetEntity=Address::class, cascade={"persist"}) * @ORM\JoinColumn(nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?Address $address = null; @@ -72,26 +72,26 @@ class Location implements TrackCreationInterface, TrackUpdateInterface * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; /** * @ORM\ManyToOne(targetEntity=LocationType::class) * @ORM\JoinColumn(nullable=false) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?LocationType $locationType = null; /** * @ORM\Column(type="string", length=255, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) */ private ?string $name = null; /** * @ORM\Column(type="string", length=64, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @PhonenumberConstraint(type="any") */ @@ -99,7 +99,7 @@ class Location implements TrackCreationInterface, TrackUpdateInterface /** * @ORM\Column(type="string", length=64, nullable=true) - * @Serializer\Groups({"read", "write"}) + * @Serializer\Groups({"read", "write", "docgen:read"}) * @Assert\Regex(pattern="/^([\+{1}])([0-9\s*]{4,20})$/") * @PhonenumberConstraint(type="any") */ diff --git a/src/Bundle/ChillMainBundle/Entity/LocationType.php b/src/Bundle/ChillMainBundle/Entity/LocationType.php index cda7e073f..9081304c8 100644 --- a/src/Bundle/ChillMainBundle/Entity/LocationType.php +++ b/src/Bundle/ChillMainBundle/Entity/LocationType.php @@ -71,13 +71,14 @@ class LocationType * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) */ private ?int $id = null; /** * @ORM\Column(type="json") - * @Serializer\Groups({"read"}) + * @Serializer\Groups({"read", "docgen:read"}) + * @Serializer\Context({"is-translatable": true}, groups={"docgen:read"}) */ private array $title = []; diff --git a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php index a1f8b1f83..d6c9e0aac 100644 --- a/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php +++ b/src/Bundle/ChillPersonBundle/Serializer/Normalizer/PersonDocGenNormalizer.php @@ -74,7 +74,7 @@ class PersonDocGenNormalizer implements $data = [ 'type' => 'person', 'isNull' => false, - 'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expect' => Civility::class])), + 'civility' => $this->normalizer->normalize($person->getCivility(), $format, array_merge($context, ['docgen:expects' => Civility::class])), 'firstname' => $person->getFirstName(), 'lastname' => $person->getLastName(), 'altNames' => implode( @@ -87,6 +87,7 @@ class PersonDocGenNormalizer implements ) ), 'text' => $this->personRender->renderString($person, []), + 'age' => $person->getAge(), 'birthdate' => $this->normalizer->normalize($person->getBirthdate(), $format, $dateContext), 'deathdate' => $this->normalizer->normalize($person->getDeathdate(), $format, $dateContext), 'gender' => $this->translator->trans($person->getGender()), diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 345fa2556..60406c3d5 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -15,6 +15,7 @@ use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithAdminFormInterface; use Chill\DocGeneratorBundle\Context\DocGeneratorContextWithPublicFormInterface; use Chill\DocGeneratorBundle\Context\Exception\UnexpectedTypeException; use Chill\DocGeneratorBundle\Entity\DocGeneratorTemplate; +use Chill\DocGeneratorBundle\Service\Context\BaseContextData; use Chill\DocStoreBundle\Entity\AccompanyingCourseDocument; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Repository\DocumentCategoryRepository; @@ -38,6 +39,8 @@ class AccompanyingPeriodContext implements DocGeneratorContextWithAdminFormInterface, DocGeneratorContextWithPublicFormInterface { + private BaseContextData $baseContextData; + private DocumentCategoryRepository $documentCategoryRepository; private EntityManagerInterface $em; @@ -56,7 +59,8 @@ class AccompanyingPeriodContext implements TranslatableStringHelperInterface $translatableStringHelper, EntityManagerInterface $em, PersonRender $personRender, - TranslatorInterface $translator + TranslatorInterface $translator, + BaseContextData $baseContextData ) { $this->documentCategoryRepository = $documentCategoryRepository; $this->normalizer = $normalizer; @@ -64,12 +68,11 @@ class AccompanyingPeriodContext implements $this->em = $em; $this->personRender = $personRender; $this->translator = $translator; + $this->baseContextData = $baseContextData; } public function adminFormReverseTransform(array $data): array { - dump($data); - if (array_key_exists('category', $data)) { $data['category'] = [ 'idInsideBundle' => $data['category']->getIdInsideBundle(), @@ -171,6 +174,7 @@ class AccompanyingPeriodContext implements $options = $template->getOptions(); $data = []; + $data = array_merge($data, $this->baseContextData->getData()); $data['course'] = $this->normalizer->normalize($entity, 'docgen', ['docgen:expects' => AccompanyingPeriod::class, 'groups' => 'docgen:read']); foreach (['mainPerson', 'person1', 'person2'] as $k) { diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php index 6c9d8de5c..b0341105f 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/PersonDocGenNormalizerTest.php @@ -56,6 +56,7 @@ final class PersonDocGenNormalizerTest extends KernelTestCase 'placeOfBirth' => '', 'memo' => '', 'numberOfChildren' => '', + 'age' => '@ignored', ]; private NormalizerInterface $normalizer; diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 8b2dc6aca..eb9a3849b 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -406,8 +406,6 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface /** * Get email. - * - * @return string|null */ public function getEmail(): ?string {