From b41830acb004bdb2dbbe9416279b4306083473b2 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Tue, 10 Dec 2024 15:27:25 +0100 Subject: [PATCH] Refactor accounting entity ownership --- src/Entity/Accounting/Accounting.php | 63 +++++++++---------- .../Accounting/AccountingStateProvider.php | 5 +- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/Entity/Accounting/Accounting.php b/src/Entity/Accounting/Accounting.php index 83f50942..39e00739 100644 --- a/src/Entity/Accounting/Accounting.php +++ b/src/Entity/Accounting/Accounting.php @@ -32,7 +32,7 @@ class Accounting private ?string $currency = null; #[ORM\Column(length: 255)] - private ?string $owner = null; + private ?string $ownerClass = null; #[ORM\OneToOne(mappedBy: 'accounting', cascade: ['persist'])] private ?User $user = null; @@ -69,37 +69,44 @@ public function setCurrency(string $currency): static return $this; } - public function getOwner(): ?AccountingOwnerInterface + public function getOwnerClass(): ?string { - $owner = $this->owner; - - return $this->$owner; + return $this->ownerClass; } - public function setOwner(AccountingOwnerInterface $owner): static + public function setOwnerClass(string $ownerClass): static { - if ($owner instanceof User) { - $this->user = $owner; - $this->owner = 'user'; - - return $this; - } + $this->ownerClass = $ownerClass; - if ($owner instanceof Project) { - $this->project = $owner; - $this->owner = 'project'; + return $this; + } - return $this; + public function getOwner(): ?AccountingOwnerInterface + { + switch ($this->getOwnerClass()) { + case User::class: + return $this->getUser(); + case Project::class: + return $this->getProject(); + case Tipjar::class: + return $this->getTipjar(); } + } - if ($owner instanceof Tipjar) { - $this->tipjar = $owner; - $this->owner = 'tipjar'; - - return $this; + public function setOwner(AccountingOwnerInterface $owner): static + { + $this->setOwnerClass($owner::class); + + switch ($this->getOwnerClass()) { + case User::class: + return $this->setUser($owner); + case Project::class: + return $this->setProject($owner); + case Tipjar::class: + return $this->setTipjar($owner); } - throw new \Exception(sprintf('%s is not a recognized AccountingOwnerInterface', $owner::class)); + return $this; } public function getUser(): ?User @@ -119,10 +126,6 @@ public function setUser(?User $user): static $user->setAccounting($this); } - if ($user !== null) { - return $this->setOwner($user); - } - $this->user = $user; return $this; @@ -145,10 +148,6 @@ public function setProject(?Project $project): static $project->setAccounting($this); } - if ($project !== null) { - return $this->setOwner($project); - } - $this->project = $project; return $this; @@ -171,10 +170,6 @@ public function setTipjar(?Tipjar $tipjar): static $tipjar->setAccounting($this); } - if ($tipjar !== null) { - return $this->setOwner($tipjar); - } - $this->tipjar = $tipjar; return $this; diff --git a/src/State/Accounting/AccountingStateProvider.php b/src/State/Accounting/AccountingStateProvider.php index e0d6e7f0..3ef5d91f 100644 --- a/src/State/Accounting/AccountingStateProvider.php +++ b/src/State/Accounting/AccountingStateProvider.php @@ -14,6 +14,7 @@ use App\ApiResource\User\UserApiResource; use App\Entity\Accounting\Accounting; use App\Entity\Project\Project; +use App\Entity\Tipjar; use App\Entity\User\User; use App\Mapping\AutoMapper; use App\Service\AccountingService; @@ -66,8 +67,6 @@ private function toResource(Accounting $accounting): AccountingApiResource $owner = $accounting->getOwner(); - $resource->owner = $owner; - switch ($owner::class) { case User::class: $resourceClass = UserApiResource::class; @@ -75,6 +74,8 @@ private function toResource(Accounting $accounting): AccountingApiResource case Project::class: $resourceClass = ProjectApiResource::class; break; + case Tipjar::class: + $resourceClass = Tipjar::class; } $resource->owner = $this->autoMapper->map($owner, $resourceClass);