From ee45ff61a6f6497fb8f50be02b7245603d1ef1fb Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 31 Jul 2024 14:55:35 +0200 Subject: [PATCH] Reorganize person resource code to create an abstract class --- .../Entity/Person/AbstractPersonResource.php | 190 ++++++++++++++++++ .../Entity/Person/PersonResource.php | 177 +--------------- 2 files changed, 191 insertions(+), 176 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Entity/Person/AbstractPersonResource.php diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/AbstractPersonResource.php b/src/Bundle/ChillPersonBundle/Entity/Person/AbstractPersonResource.php new file mode 100644 index 000000000..a13670b97 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Entity/Person/AbstractPersonResource.php @@ -0,0 +1,190 @@ +comment = new CommentEmbeddable(); + } + + public function getComment(): CommentEmbeddable + { + return $this->comment; + } + + public function getFreeText(): ?string + { + return $this->freeText; + } + + public function getKind(): ?PersonResourceKind + { + return $this->kind; + } + + public function getPerson(): ?Person + { + return $this->person; + } + + public function getPersonOwner(): ?Person + { + return $this->personOwner; + } + + /** + * @Groups({"read", "docgen:read"}) + */ + public function getResourceKind(): string + { + if ($this->getPerson() instanceof Person) { + return 'person'; + } + + if ($this->getThirdParty() instanceof ThirdParty) { + return 'thirdparty'; + } + + if (null !== $this->getFreeText()) { + return 'freetext'; + } + + return 'none'; + } + + public function getThirdParty(): ?ThirdParty + { + return $this->thirdParty; + } + + public function setComment(?CommentEmbeddable $comment): self + { + if (null === $comment) { + $this->comment->setComment(''); + + return $this; + } + + $this->comment = $comment; + + return $this; + } + + public function setFreeText(?string $freeText): self + { + $this->freeText = $freeText; + + if ('' !== $freeText && null !== $freeText) { + $this->setPerson(null); + $this->setThirdParty(null); + } + + if ('' === $freeText) { + $this->freeText = 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; + } + + /** + * @Assert\Callback + */ + public function validate(ExecutionContextInterface $context, mixed $payload): void + { + if (null === $this->person && null === $this->thirdParty && (null === $this->freeText || '' === $this->freeText)) { + $context->buildViolation('You must associate at least one entity') + ->addViolation(); + } + + if (null !== $this->person && $this->person === $this->personOwner) { + $context->buildViolation('You cannot associate a resource with the same person') + ->addViolation(); + } + } + +} diff --git a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php index dec0ea87a..0fac5689d 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person/PersonResource.php @@ -27,191 +27,16 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; #[Serializer\DiscriminatorMap(typeProperty: 'type', mapping: ['personResource' => PersonResource::class])] #[ORM\Entity] #[ORM\Table(name: 'chill_person_resource')] -class PersonResource implements TrackCreationInterface, TrackUpdateInterface +class PersonResource extends AbstractPersonResource implements TrackCreationInterface, TrackUpdateInterface { - use TrackCreationTrait; - - use TrackUpdateTrait; - - #[Groups(['read', 'docgen:read'])] - #[ORM\Embedded(class: CommentEmbeddable::class, columnPrefix: 'comment_')] - private CommentEmbeddable $comment; - - #[Groups(['read', 'docgen:read'])] - #[ORM\Column(type: \Doctrine\DBAL\Types\Types::TEXT, nullable: true)] - private ?string $freeText = null; - #[Groups(['read', 'docgen:read'])] #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)] private ?int $id = null; - #[Groups(['read', 'docgen:read'])] - #[ORM\ManyToOne(targetEntity: PersonResourceKind::class, inversedBy: 'personResources')] - #[ORM\JoinColumn(nullable: true)] - private ?PersonResourceKind $kind = null; - - /** - * The person which host the owner of this resource. - */ - #[Groups(['read', 'docgen:read'])] - #[ORM\ManyToOne(targetEntity: Person::class)] - #[ORM\JoinColumn(nullable: true)] - private ?Person $person = null; - - /** - * The person linked with this resource. - */ - #[Groups(['read'])] - #[ORM\ManyToOne(targetEntity: Person::class, inversedBy: 'resources')] - #[ORM\JoinColumn(nullable: false)] - private ?Person $personOwner = null; - - #[Groups(['read', 'docgen:read'])] - #[ORM\ManyToOne(targetEntity: ThirdParty::class, inversedBy: 'personResources')] - #[ORM\JoinColumn(nullable: true)] - private ?ThirdParty $thirdParty = null; - - public function __construct() - { - $this->comment = new CommentEmbeddable(); - } - - public function getComment(): CommentEmbeddable - { - return $this->comment; - } - - 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; - } - - #[Groups(['read', 'docgen:read'])] - public function getResourceKind(): string - { - if ($this->getPerson() instanceof Person) { - return 'person'; - } - - if ($this->getThirdParty() instanceof ThirdParty) { - return 'thirdparty'; - } - - if (null !== $this->getFreeText()) { - return 'freetext'; - } - - return 'none'; - } - - public function getThirdParty(): ?ThirdParty - { - return $this->thirdParty; - } - - public function setComment(?CommentEmbeddable $comment): self - { - if (null === $comment) { - $this->comment->setComment(''); - - return $this; - } - - $this->comment = $comment; - - return $this; - } - - public function setFreeText(?string $freeText): self - { - $this->freeText = $freeText; - - if ('' !== $freeText && null !== $freeText) { - $this->setPerson(null); - $this->setThirdParty(null); - } - - if ('' === $freeText) { - $this->freeText = 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; - } - - #[Assert\Callback] - public function validate(ExecutionContextInterface $context, mixed $payload) - { - if (null === $this->person && null === $this->thirdParty && (null === $this->freeText || '' === $this->freeText)) { - $context->buildViolation('You must associate at least one entity') - ->addViolation(); - } - - if (null !== $this->person && $this->person === $this->personOwner) { - $context->buildViolation('You cannot associate a resource with the same person') - ->addViolation(); - } - } }