Skip to content

Commit

Permalink
Refactor accounting entity ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
subiabre committed Dec 10, 2024
1 parent f2fd686 commit b41830a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
63 changes: 29 additions & 34 deletions src/Entity/Accounting/Accounting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/State/Accounting/AccountingStateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,15 +67,15 @@ private function toResource(Accounting $accounting): AccountingApiResource

$owner = $accounting->getOwner();

$resource->owner = $owner;

switch ($owner::class) {
case User::class:
$resourceClass = UserApiResource::class;
break;
case Project::class:
$resourceClass = ProjectApiResource::class;
break;
case Tipjar::class:
$resourceClass = Tipjar::class;
}

$resource->owner = $this->autoMapper->map($owner, $resourceClass);
Expand Down

0 comments on commit b41830a

Please sign in to comment.