diff --git a/application/src/Domain/MemberManagement/MemberManager.php b/application/src/Domain/MemberManagement/MemberManager.php new file mode 100644 index 0000000..91e28a3 --- /dev/null +++ b/application/src/Domain/MemberManagement/MemberManager.php @@ -0,0 +1,50 @@ +memberRelation = $memberRelation; + } + + public function addMember(MemberRelationInterface $member): void + { + if (!$this->memberRelation->getMembers()->contains($member)) { + $this->memberRelation->getMembers()[] = $member; + (new self($member))->addMembership($this->memberRelation); + } + } + + public function removeMember(MemberRelationInterface $member): void + { + if ($this->memberRelation->getMembers()->contains($member)) { + $this->memberRelation->getMembers()->removeElement($member); + (new self($member))->removeMembership($this->memberRelation); + } + } + + public function addMembership(MemberRelationInterface $membership): void + { + if (!$this->memberRelation->getMemberships()->contains($membership)) { + $this->memberRelation->getMemberships()[] = $membership; + (new self($membership))->addMember($this->memberRelation); + } + } + + public function removeMembership(MemberRelationInterface $membership): void + { + if ($this->memberRelation->getMemberships()->contains($membership)) { + $this->memberRelation->getMemberships()->removeElement($membership); + (new self($membership))->removeMember($this->memberRelation); + } + } +} diff --git a/application/src/Domain/MemberManagement/MemberManagerInterface.php b/application/src/Domain/MemberManagement/MemberManagerInterface.php new file mode 100644 index 0000000..6d20611 --- /dev/null +++ b/application/src/Domain/MemberManagement/MemberManagerInterface.php @@ -0,0 +1,28 @@ +memberRelation = $memberRelation; + } + + public function getMemberRelation(): MemberRelationInterface + { + return $this->memberRelation; + } +} diff --git a/application/src/Entity/Attribut/MemberRelationAttributInterface.php b/application/src/Entity/Attribut/MemberRelationAttributInterface.php new file mode 100644 index 0000000..bb9d3e8 --- /dev/null +++ b/application/src/Entity/Attribut/MemberRelationAttributInterface.php @@ -0,0 +1,12 @@ +members = new ArrayCollection(); + $this->memberships = new ArrayCollection(); + } } diff --git a/application/src/Entity/Meta/Relation/Parent/CreatorRelation.php b/application/src/Entity/Meta/Relation/Parent/CreatorRelation.php index 4a664bf..97c28f0 100644 --- a/application/src/Entity/Meta/Relation/Parent/CreatorRelation.php +++ b/application/src/Entity/Meta/Relation/Parent/CreatorRelation.php @@ -4,7 +4,6 @@ namespace App\Entity\Meta\Relation\Parent; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\Collection; -use App\Entity\Source\SourceInterface; /** * @author kevinfrantz @@ -12,14 +11,6 @@ use App\Entity\Source\SourceInterface; */ class CreatorRelation extends AbstractParentRelation implements CreatorRelationInterface { - /** - * @ORM\OneToOne(targetEntity="App\Entity\Source\AbstractSource",cascade={"persist", "remove"}) - * @ORM\JoinColumn(name="source_id", referencedColumnName="id",onDelete="CASCADE") - * - * @var SourceInterface - */ - protected $source; - /** * @ORM\ManyToMany(targetEntity="CreatorRelation",mappedBy="childs") * diff --git a/application/src/Entity/Source/AbstractSource.php b/application/src/Entity/Source/AbstractSource.php index e05c0e4..94cfd1d 100644 --- a/application/src/Entity/Source/AbstractSource.php +++ b/application/src/Entity/Source/AbstractSource.php @@ -5,19 +5,18 @@ namespace App\Entity\Source; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation\Exclude; use App\Entity\AbstractEntity; -use Doctrine\Common\Collections\Collection; use App\Entity\Attribut\LawAttribut; use App\Entity\Meta\LawInterface; use App\Entity\Meta\Law; use Doctrine\Common\Collections\ArrayCollection; -use App\Entity\Attribut\MembershipsAttribut; use App\Entity\Attribut\SlugAttribut; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; -use App\Entity\Attribut\MembersAttribut; use App\Entity\Attribut\CreatorRelationAttribut; -use App\Entity\Meta\Relation\CreatorRelationInterface; +use App\Entity\Meta\Relation\Parent\CreatorRelationInterface; use App\Entity\Meta\Relation\Parent\CreatorRelation; +use App\Entity\Attribut\MemberRelationAttribut; +use App\Entity\Meta\Relation\Member\MemberRelation; /** * @author kevinfrantz @@ -47,7 +46,7 @@ use App\Entity\Meta\Relation\Parent\CreatorRelation; */ abstract class AbstractSource extends AbstractEntity implements SourceInterface { - use MembershipsAttribut, LawAttribut,SlugAttribut,MembersAttribut,CreatorRelationAttribut; + use LawAttribut,SlugAttribut,CreatorRelationAttribut, MemberRelationAttribut; /** * System slugs should be writen in UPPER CASES @@ -69,22 +68,12 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface protected $creatorRelation; /** - * Many Sources have many Source Members. - * - * @var Collection|SourceInterface[] - * @ORM\ManyToMany(targetEntity="AbstractSource", inversedBy="memberships",cascade={"persist", "remove"}) - * @ORM\JoinTable(name="source_members", - * joinColumns={@ORM\JoinColumn(name="source_id", referencedColumnName="id",onDelete="CASCADE")}, - * inverseJoinColumns={@ORM\JoinColumn(name="member_id", referencedColumnName="id",onDelete="CASCADE")} - * ) + * @var CreatorRelationInterface + * @ORM\OneToOne(targetEntity="App\Entity\Meta\Relation\Member\MemberRelation",cascade={"persist", "remove"}) + * @ORM\JoinColumn(name="member_relation_id", referencedColumnName="id", onDelete="CASCADE") + * @Exclude */ - protected $members; - - /** - * @var Collection|SourceInterface[] - * @ORM\ManyToMany(targetEntity="AbstractSource",mappedBy="members") - */ - protected $memberships; + protected $memberRelation; /** * @ORM\OneToOne(targetEntity="App\Entity\Meta\Law",cascade={"persist", "remove"}) @@ -99,6 +88,8 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface parent::__construct(); $this->creatorRelation = new CreatorRelation(); $this->creatorRelation->setSource($this); + $this->memberRelation = new MemberRelation(); + $this->memberRelation->setSource($this); $this->law = new Law(); $this->law->setSource($this); /* diff --git a/application/src/Entity/Source/SourceInterface.php b/application/src/Entity/Source/SourceInterface.php index d5e930f..70fbd4c 100644 --- a/application/src/Entity/Source/SourceInterface.php +++ b/application/src/Entity/Source/SourceInterface.php @@ -5,14 +5,13 @@ namespace App\Entity\Source; use App\Entity\Attribut\IdAttributInterface; use App\Entity\EntityInterface; use App\Entity\Attribut\LawAttributInterface; -use App\Entity\Attribut\MembershipsAttributInterface; use App\Entity\Attribut\SlugAttributInterface; -use App\Entity\Attribut\MembersAttributInterface; use App\Entity\Attribut\CreatorRelationAttributInterface; +use App\Entity\Attribut\MemberRelationAttributInterface; /** * @author kevinfrantz */ -interface SourceInterface extends IdAttributInterface, EntityInterface, MembershipsAttributInterface, LawAttributInterface, SlugAttributInterface, MembersAttributInterface, CreatorRelationAttributInterface +interface SourceInterface extends IdAttributInterface, EntityInterface, LawAttributInterface, SlugAttributInterface, CreatorRelationAttributInterface, MemberRelationAttributInterface { } diff --git a/application/tests/Unit/Domain/MemberManager/MemberManagerTest.php b/application/tests/Unit/Domain/MemberManager/MemberManagerTest.php new file mode 100644 index 0000000..3b3a549 --- /dev/null +++ b/application/tests/Unit/Domain/MemberManager/MemberManagerTest.php @@ -0,0 +1,50 @@ +memberRelation = new MemberRelation(); + $this->MemberManager = new MemberManager($this->memberRelation); + } + + public function testAddAndRemoveMember(): void + { + $member = new MemberRelation(); + $this->assertNull($this->MemberManager->addMember($member)); + $this->assertEquals($member, $this->memberRelation->getMembers()->get(0)); + $this->assertEquals($this->memberRelation, $member->getMemberships()->get(0)); + $this->assertNull($this->MemberManager->removeMember($member)); + $this->assertEquals(0, $this->memberRelation->getMembers()->count()); + $this->assertEquals(0, $member->getMemberships()->count()); + } + + public function testAddAndRemoveMembership(): void + { + $membership = new MemberRelation(); + $this->assertNull($this->MemberManager->addMembership($membership)); + $this->assertEquals($membership, $this->memberRelation->getMemberships()->get(0)); + $this->assertEquals($this->memberRelation, $membership->getMembers()->get(0)); + $this->assertNull($this->MemberManager->removeMembership($membership)); + $this->assertEquals(0, $this->memberRelation->getMemberships()->count()); + $this->assertEquals(0, $membership->getMembers()->count()); + } +} diff --git a/application/tests/Unit/Entity/Attribut/MemberRelationAttributTest.php b/application/tests/Unit/Entity/Attribut/MemberRelationAttributTest.php new file mode 100644 index 0000000..037275f --- /dev/null +++ b/application/tests/Unit/Entity/Attribut/MemberRelationAttributTest.php @@ -0,0 +1,37 @@ +memberRelation = new class() implements MemberRelationAttributInterface { + use MemberRelationAttribut; + }; + } + + public function testConstructor(): void + { + $this->expectException(\TypeError::class); + $this->memberRelation->getMemberRelation(); + } + + public function testAccessors(): void + { + $membership = $this->createMock(MemberRelationInterface::class); + $this->assertNull($this->memberRelation->setMemberRelation(new ArrayCollection([$membership]))); + $this->assertEquals($this->memberRelation->getMemberRelation()->get(0), $membership); + } +} diff --git a/application/tests/Unit/Entity/Meta/Relation/Member/MemberRelationTest.php b/application/tests/Unit/Entity/Meta/Relation/Member/MemberRelationTest.php new file mode 100644 index 0000000..f5b29e9 --- /dev/null +++ b/application/tests/Unit/Entity/Meta/Relation/Member/MemberRelationTest.php @@ -0,0 +1,27 @@ +memberRelation = new MemberRelation(); + } + + public function testConstructor(): void + { + $this->assertInstanceOf(Collection::class, $this->memberRelation->getMembers()); + $this->assertInstanceOf(Collection::class, $this->memberRelation->getMembership()); + } +}