From ab5f9923a67db936ca3b5f28b4071039e9e9b865 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Tue, 25 Jan 2022 16:41:44 +0100 Subject: [PATCH] CSfixer changes --- .../EntityToJsonTransformer.php | 11 +- .../Controller/PersonResourceController.php | 179 ++++++------ .../Entity/Person/PersonResource.php | 260 +++++++++--------- .../Entity/Person/PersonResourceKind.php | 24 +- .../Form/PersonResourceType.php | 19 +- .../Menu/PersonMenuBuilder.php | 6 +- .../Repository/PersonResourceRepository.php | 2 +- .../Templating/Entity/ResourceKindRender.php | 6 +- .../migrations/Version20220119155944.php | 25 +- 9 files changed, 269 insertions(+), 263 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php b/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php index 85030606d..c6c205a47 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php +++ b/src/Bundle/ChillMainBundle/Form/Type/DataTransformer/EntityToJsonTransformer.php @@ -16,7 +16,6 @@ use Chill\PersonBundle\Entity\Person; use Chill\ThirdPartyBundle\Entity\ThirdParty; use Symfony\Component\Form\DataTransformerInterface; use Symfony\Component\Form\Exception\TransformationFailedException; -use Symfony\Component\Serializer\Exception\UnexpectedValueException as ExceptionUnexpectedValueException; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; use Symfony\Component\Serializer\SerializerInterface; @@ -59,7 +58,7 @@ class EntityToJsonTransformer implements DataTransformerInterface ); } - if ($value === '') { + if ('' === $value) { return null; } @@ -93,15 +92,21 @@ class EntityToJsonTransformer implements DataTransformerInterface switch ($this->type) { case 'user': $class = User::class; + break; + case 'person': $class = Person::class; + break; + case 'thirdparty': $class = ThirdParty::class; + break; + default: - throw new \UnexpectedValueException('This type is not supported'); + throw new UnexpectedValueException('This type is not supported'); } return diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php b/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php index 1146dbd51..6b9b5dc6a 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonResourceController.php @@ -25,9 +25,12 @@ use Symfony\Contracts\Translation\TranslatorInterface; final class PersonResourceController extends AbstractController { - private PersonResourceRepository $personResourceRepository; - private PersonRepository $personRepository; private EntityManagerInterface $em; + + private PersonRepository $personRepository; + + private PersonResourceRepository $personResourceRepository; + private TranslatorInterface $translator; public function __construct( @@ -35,14 +38,95 @@ final class PersonResourceController extends AbstractController PersonRepository $personRepository, EntityManagerInterface $em, TranslatorInterface $translator - ) - { + ) { $this->personResourceRepository = $personResourceRepository; $this->personRepository = $personRepository; $this->em = $em; $this->translator = $translator; } + public function deleteAction(Request $request, $person_id, $resource_id): Response + { + $personOwner = $this->personRepository->find($person_id); + $resource = $this->personResourceRepository->find($resource_id); + + $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $personOwner); + + if (null === $resource) { + throw $this->createNotFoundException('Unable to find Resource entity.'); + } + + $form = $this->createFormBuilder() + ->setAction($this->generateUrl('chill_person_resource_delete', [ + 'resource_id' => $resource_id, + 'person_id' => $person_id, + ])) + ->setMethod('DELETE') + ->add('submit', SubmitType::class, ['label' => 'Delete']) + ->getForm(); + + if ($request->getMethod() === Request::METHOD_DELETE) { + $form->handleRequest($request); + + if ($form->isValid()) { + $this->em->remove($resource); + $this->em->flush(); + + $this->addFlash('success', $this->translator + ->trans('The resource has been successfully removed.')); + + return $this->redirectToRoute('chill_person_resource_list', [ + 'person_id' => $personOwner->getId(), + ]); + } + } + + return $this->render( + 'ChillPersonBundle:PersonResource:delete.html.twig', + [ + 'person' => $personOwner, + 'resource' => $resource, + 'form' => $form->createView(), + ] + ); + } + + public function editAction(Request $request, $resource_id, $person_id): Response + { + $resource = $this->personResourceRepository->find($resource_id); + $personOwner = $this->personRepository->find($person_id); + + $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $personOwner); + + if (null === $resource) { + throw $this->createNotFoundException('Unable to find Resource entity.'); + } + + $form = $this->createForm(PersonResourceType::class, $resource); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->em->persist($resource); + $this->em->flush(); + + return $this->redirectToRoute('chill_person_resource_list', [ + 'person_id' => $personOwner->getId(), + ]); + } + + dump($resource); + + return $this->render( + 'ChillPersonBundle:PersonResource:edit.html.twig', + [ + 'person' => $personOwner, + 'resource' => $resource, + 'form' => $form->createView(), + 'action' => 'edit', + ] + ); + } + public function listAction(Request $request, $person_id) { $personOwner = $this->personRepository->find($person_id); @@ -56,7 +140,6 @@ final class PersonResourceController extends AbstractController $form->handleRequest($request); if ($request->getMethod() === Request::METHOD_POST && $form->isValid()) { - $this->denyAccessUnlessGranted(PersonVoter::CREATE, $personOwner); $personResource = new PersonResource(); @@ -81,7 +164,6 @@ final class PersonResourceController extends AbstractController return $this->redirectToRoute('chill_person_resource_list', [ 'person_id' => $personOwner->getId(), ]); - } return $this->render( @@ -89,91 +171,8 @@ final class PersonResourceController extends AbstractController [ 'person' => $personOwner, 'personResources' => $personResources, - 'form' => $form->createView() - ] - ); - } - - public function editAction(Request $request, $resource_id, $person_id): Response - { - $resource = $this->personResourceRepository->find($resource_id); - $personOwner = $this->personRepository->find($person_id); - - $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $personOwner); - - if (null === $resource) { - throw $this->createNotFoundException('Unable to find Resource entity.'); - } - - $form = $this->createForm(PersonResourceType::class, $resource); - $form->handleRequest($request); - - if($form->isSubmitted() && $form->isValid()) { - $this->em->persist($resource); - $this->em->flush(); - - return $this->redirectToRoute('chill_person_resource_list', [ - 'person_id' => $personOwner->getId(), - ]); - } - - dump($resource); - - return $this->render( - 'ChillPersonBundle:PersonResource:edit.html.twig', - [ - 'person' => $personOwner, - 'resource' => $resource, 'form' => $form->createView(), - 'action' => 'edit' ] ); } - - public function deleteAction(Request $request, $person_id, $resource_id): Response - { - - $personOwner = $this->personRepository->find($person_id); - $resource = $this->personResourceRepository->find($resource_id); - - $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $personOwner); - - if (null === $resource) { - throw $this->createNotFoundException('Unable to find Resource entity.'); - } - - $form = $this->createFormBuilder() - ->setAction($this->generateUrl('chill_person_resource_delete', [ - 'resource_id' => $resource_id, - 'person_id' => $person_id, - ])) - ->setMethod('DELETE') - ->add('submit', SubmitType::class, ['label' => 'Delete']) - ->getForm(); - - if($request->getMethod() === Request::METHOD_DELETE) { - $form->handleRequest($request); - - if ($form->isValid()) { - $this->em->remove($resource); - $this->em->flush(); - - $this->addFlash('success', $this->translator - ->trans('The resource has been successfully removed.')); - - return $this->redirectToRoute('chill_person_resource_list', [ - 'person_id' => $personOwner->getId(), - ]); - } - } - - return $this->render( - 'ChillPersonBundle:PersonResource:delete.html.twig', - [ - 'person' => $personOwner, - 'resource' => $resource, - 'form' => $form->createView() - ] - ); - } -} \ No newline at end of file +} diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php index 3e4345a58..344f2103a 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php @@ -32,53 +32,12 @@ use Symfony\Component\Serializer\Annotation\Groups; */ class PersonResource implements TrackCreationInterface, TrackUpdateInterface { - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ - private ?int $id; - - /** - * @ORM\ManyToOne(targetEntity=Person::class) - * @ORM\JoinColumn(nullable=false) - * @Groups({"read"}) - */ - private ?Person $personOwner = null; - - /** - * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="personResources") - * @ORM\JoinColumn(nullable=true) - * @Groups({"read"}) - */ - private ?Person $person = null; - - /** - * @ORM\ManyToOne(targetEntity=ThirdParty::class, inversedBy="personResources") - * @ORM\JoinColumn(nullable=true) - * @Groups({"read"}) - */ - private ?ThirdParty $thirdParty = null; - - /** - * @ORM\Column(type="text", nullable=true) - * @Groups({"read"}) - */ - private $freeText; - /** * @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\CommentEmbeddable", columnPrefix="comment_") * @Groups({"read"}) */ private CommentEmbeddable $comment; - /** - * @ORM\ManyToOne(targetEntity=PersonResourceKind::class, inversedBy="personResources") - * @ORM\JoinColumn(nullable=true) - * @Groups({"read"}) - */ - private $kind; - /** * @ORM\Column(type="datetime") */ @@ -90,6 +49,47 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface */ private User $createdBy; + /** + * @ORM\Column(type="text", nullable=true) + * @Groups({"read"}) + */ + private $freeText; + + /** + * @ORM\Id + * @ORM\GeneratedValue + * @ORM\Column(type="integer") + */ + private ?int $id; + + /** + * @ORM\ManyToOne(targetEntity=PersonResourceKind::class, inversedBy="personResources") + * @ORM\JoinColumn(nullable=true) + * @Groups({"read"}) + */ + private $kind; + + /** + * @ORM\ManyToOne(targetEntity=Person::class, inversedBy="personResources") + * @ORM\JoinColumn(nullable=true) + * @Groups({"read"}) + */ + private ?Person $person = null; + + /** + * @ORM\ManyToOne(targetEntity=Person::class) + * @ORM\JoinColumn(nullable=false) + * @Groups({"read"}) + */ + private ?Person $personOwner = null; + + /** + * @ORM\ManyToOne(targetEntity=ThirdParty::class, inversedBy="personResources") + * @ORM\JoinColumn(nullable=true) + * @Groups({"read"}) + */ + private ?ThirdParty $thirdParty = null; + /** * @ORM\Column(type="datetime", nullable=true) */ @@ -105,45 +105,11 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface $this->comment = new CommentEmbeddable(); } - /** - * GETTERS - */ - - public function getId(): ?int - { - return $this->id; - } - - public function getFreeText(): ?string - { - return $this->freeText; - } - public function getComment(): CommentEmbeddable { return $this->comment; } - public function getKind(): ?PersonResourceKind - { - return $this->kind; - } - - public function getPersonOwner(): ?Person - { - return $this->personOwner; - } - - public function getPerson(): ?Person - { - return $this->person; - } - - public function getThirdParty(): ?ThirdParty - { - return $this->thirdParty; - } - public function getCreatedAt(): ?DateTimeInterface { return $this->createdAt; @@ -154,6 +120,39 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return $this->createdBy; } + public function getFreeText(): ?string + { + return $this->freeText; + } + + /** + * GETTERS. + */ + public function getId(): ?int + { + return $this->id; + } + + public function getKind(): ?PersonResourceKind + { + return $this->kind; + } + + public function getPerson(): ?Person + { + return $this->person; + } + + public function getPersonOwner(): ?Person + { + return $this->personOwner; + } + + public function getThirdParty(): ?ThirdParty + { + return $this->thirdParty; + } + public function getUpdatedAt(): ?DateTimeInterface { return $this->updatedAt; @@ -164,26 +163,11 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return $this->updatedBy; } - /** - * SETTERS - */ - - public function setFreeText(?string $freeText): self - { - $this->freeText = $freeText; - - if ('' !== $freeText && $freeText !== null) { - $this->setPerson(null); - $this->setThirdParty(null); - } - - return $this; - } - public function setComment(?CommentEmbeddable $comment): self { - if ($comment === null) { + if (null === $comment) { $this->comment->setComment(''); + return $this; } @@ -192,44 +176,6 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return $this; } - public function setKind(?PersonResourceKind $kind): self - { - $this->kind = $kind; - - return $this; - } - - public function setPersonOwner(?Person $personOwner): self - { - $this->personOwner = $personOwner; - - return $this; - } - - public function setPerson(?Person $person): self - { - $this->person = $person; - - if (null !== $person) { - $this->setFreeText(""); - $this->setThirdParty(null); - } - - return $this; - } - - public function setThirdParty(?ThirdParty $thirdParty): self - { - $this->thirdParty = $thirdParty; - - if (null !== $thirdParty) { - $this->setFreeText(""); - $this->setPerson(null); - } - - return $this; - } - public function setCreatedAt(DateTimeInterface $createdAt): self { $this->createdAt = $createdAt; @@ -244,6 +190,59 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return $this; } + /** + * SETTERS. + */ + public function setFreeText(?string $freeText): self + { + $this->freeText = $freeText; + + if ('' !== $freeText && null !== $freeText) { + $this->setPerson(null); + $this->setThirdParty(null); + } + + return $this; + } + + public function setKind(?PersonResourceKind $kind): self + { + $this->kind = $kind; + + return $this; + } + + public function setPerson(?Person $person): self + { + $this->person = $person; + + if (null !== $person) { + $this->setFreeText(''); + $this->setThirdParty(null); + } + + return $this; + } + + public function setPersonOwner(?Person $personOwner): self + { + $this->personOwner = $personOwner; + + return $this; + } + + public function setThirdParty(?ThirdParty $thirdParty): self + { + $this->thirdParty = $thirdParty; + + if (null !== $thirdParty) { + $this->setFreeText(''); + $this->setPerson(null); + } + + return $this; + } + public function setUpdatedAt(DateTimeInterface $updatedAt): self { $this->updatedAt = $updatedAt; @@ -257,5 +256,4 @@ class PersonResource implements TrackCreationInterface, TrackUpdateInterface return $this; } - } diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php index 619382f68..4e65fda9f 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResourceKind.php @@ -28,16 +28,16 @@ class PersonResourceKind */ private int $id; - /** - * @ORM\Column(type="json", length=255) - */ - private array $title; - /** * @ORM\Column(type="boolean") */ private bool $isActive = true; + /** + * @ORM\Column(type="json", length=255) + */ + private array $title; + public function getId(): ?int { return $this->id; @@ -53,17 +53,17 @@ class PersonResourceKind return $this->title; } - public function setTitle(array $title): self - { - $this->title = $title; - - return $this; - } - public function setIsActive(bool $isActive): self { $this->isActive = $isActive; return $this; } + + public function setTitle(array $title): self + { + $this->title = $title; + + return $this; + } } diff --git a/src/Bundle/ChillPersonBundle/Form/PersonResourceType.php b/src/Bundle/ChillPersonBundle/Form/PersonResourceType.php index 91b1c2643..f69081584 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonResourceType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonResourceType.php @@ -11,32 +11,30 @@ declare(strict_types=1); namespace Chill\PersonBundle\Form; -use Chill\AsideActivityBundle\Templating\Entity\CategoryRender; use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Form\Type\CommentType; -use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person\PersonResource; use Chill\PersonBundle\Entity\Person\PersonResourceKind; use Chill\PersonBundle\Form\Type\PickPersonDynamicType; use Chill\PersonBundle\Templating\Entity\PersonRender; use Chill\PersonBundle\Templating\Entity\ResourceKindRender; -use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Form\Type\PickThirdpartyDynamicType; use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender; use Doctrine\ORM\EntityRepository; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\Extension\Core\Type\ChoiceType; -use Symfony\Component\Form\Extension\Core\Type\RadioType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Contracts\Translation\TranslatorInterface; final class PersonResourceType extends AbstractType { - private ResourceKindRender $resourceKindRender; private PersonRender $personRender; + + private ResourceKindRender $resourceKindRender; + private ThirdPartyRender $thirdPartyRender; + private TranslatorInterface $translator; public function __construct(ResourceKindRender $resourceKindRender, PersonRender $personRender, ThirdPartyRender $thirdPartyRender, TranslatorInterface $translator) @@ -57,13 +55,15 @@ final class PersonResourceType extends AbstractType 'query_builder' => static function (EntityRepository $er) { $qb = $er->createQueryBuilder('pr'); $qb->where($qb->expr()->eq('pr.isActive', 'TRUE')); + return $qb; }, 'placeholder' => $this->translator->trans('Select a type'), 'choice_label' => function (PersonResourceKind $personResourceKind) { $options = []; + return $this->resourceKindRender->renderString($personResourceKind, $options); - } + }, ]) ->add('person', PickPersonDynamicType::class, [ 'label' => 'Usager', @@ -73,11 +73,11 @@ final class PersonResourceType extends AbstractType ]) ->add('freetext', ChillTextareaType::class, [ 'label' => 'Description libre', - 'required' => false + 'required' => false, ]) ->add('comment', CommentType::class, [ 'label' => 'Note', - 'required' => false + 'required' => false, ]); } @@ -92,5 +92,4 @@ final class PersonResourceType extends AbstractType { return 'chill_personbundle_person_resource'; } - } diff --git a/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php b/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php index 228309737..229b2d373 100644 --- a/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php +++ b/src/Bundle/ChillPersonBundle/Menu/PersonMenuBuilder.php @@ -103,9 +103,9 @@ class PersonMenuBuilder implements LocalMenuBuilderInterface 'person_id' => $parameters['person']->getId(), ], ]) - ->setExtras([ - 'order' => 99999, - ]); + ->setExtras([ + 'order' => 99999, + ]); } public static function getMenuIds(): array diff --git a/src/Bundle/ChillPersonBundle/Repository/PersonResourceRepository.php b/src/Bundle/ChillPersonBundle/Repository/PersonResourceRepository.php index 1874bec05..a0847a844 100644 --- a/src/Bundle/ChillPersonBundle/Repository/PersonResourceRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/PersonResourceRepository.php @@ -58,4 +58,4 @@ final class PersonResourceRepository implements ObjectRepository { return PersonResource::class; } -} \ No newline at end of file +} diff --git a/src/Bundle/ChillPersonBundle/Templating/Entity/ResourceKindRender.php b/src/Bundle/ChillPersonBundle/Templating/Entity/ResourceKindRender.php index 377df38b0..8c340f305 100644 --- a/src/Bundle/ChillPersonBundle/Templating/Entity/ResourceKindRender.php +++ b/src/Bundle/ChillPersonBundle/Templating/Entity/ResourceKindRender.php @@ -39,8 +39,7 @@ final class ResourceKindRender implements ChillEntityRenderInterface $title = ''; if (null !== $entity->getTitle()) { - $title = $this->translatableStringHelper->localize($entity->getTitle()); - return $title; + return $this->translatableStringHelper->localize($entity->getTitle()); } return $title; @@ -50,5 +49,4 @@ final class ResourceKindRender implements ChillEntityRenderInterface { return $entity instanceof PersonResourceKind; } - -} \ No newline at end of file +} diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20220119155944.php b/src/Bundle/ChillPersonBundle/migrations/Version20220119155944.php index a4d2e5575..a5cb09fca 100644 --- a/src/Bundle/ChillPersonBundle/migrations/Version20220119155944.php +++ b/src/Bundle/ChillPersonBundle/migrations/Version20220119155944.php @@ -1,5 +1,12 @@ addSql('DROP SEQUENCE chill_person_resource_id_seq CASCADE'); + $this->addSql('DROP SEQUENCE chill_person_resource_kind_id_seq CASCADE'); + $this->addSql('DROP TABLE chill_person_resource'); + $this->addSql('DROP TABLE chill_person_resource_kind'); + } + public function getDescription(): string { return 'Creation of person resource and person resource kind'; @@ -36,12 +51,4 @@ final class Version20220119155944 extends AbstractMigration $this->addSql('ALTER TABLE chill_person_resource ADD CONSTRAINT FK_FA5B7D923174800F FOREIGN KEY (createdBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); $this->addSql('ALTER TABLE chill_person_resource ADD CONSTRAINT FK_FA5B7D9265FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); } - - public function down(Schema $schema): void - { - $this->addSql('DROP SEQUENCE chill_person_resource_id_seq CASCADE'); - $this->addSql('DROP SEQUENCE chill_person_resource_kind_id_seq CASCADE'); - $this->addSql('DROP TABLE chill_person_resource'); - $this->addSql('DROP TABLE chill_person_resource_kind'); - } }