Optimized draft for MemberRelation

This commit is contained in:
Kevin Frantz 2018-11-26 21:41:06 +01:00
parent ce8f53780a
commit b23f507da8
16 changed files with 296 additions and 46 deletions

View File

@ -0,0 +1,50 @@
<?php
namespace App\Domain\MemberManagement;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
final class MemberManager implements MemberManagerInterface
{
/**
* @var MemberRelationInterface
*/
private $memberRelation;
public function __construct(MemberRelationInterface $memberRelation)
{
$this->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);
}
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Domain\MemberManagement;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
interface MemberManagerInterface
{
/**
* @param MemberRelationInterface $member
*/
public function addMember(MemberRelationInterface $member): void;
/**
* @param MemberRelationInterface $member
*/
public function removeMember(MemberRelationInterface $member): void;
/**
* @param MemberRelationInterface $membership
*/
public function addMembership(MemberRelationInterface $membership): void;
/**
* @param MemberRelationInterface $membership
*/
public function removeMembership(MemberRelationInterface $membership): void;
}

View File

@ -0,0 +1,23 @@
<?php
namespace App\Entity\Attribut;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
trait MemberRelationAttribut
{
/**
* @var MemberRelationInterface
*/
protected $memberRelation;
public function setMembersRelation(MemberRelationInterface $memberRelation): void
{
$this->memberRelation = $memberRelation;
}
public function getMemberRelation(): MemberRelationInterface
{
return $this->memberRelation;
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace App\Entity\Attribut;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
interface MemberRelationAttributInterface
{
public function setMembersRelation(MemberRelationInterface $memberRelation): void;
public function getMemberRelation(): MemberRelationInterface;
}

View File

@ -3,7 +3,7 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface; use App\Entity\Meta\Relation\Member\MemberRelationInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -11,12 +11,12 @@ use App\Entity\Source\SourceInterface;
trait MembersAttribut trait MembersAttribut
{ {
/** /**
* @var Collection|SourceInterface[] * @var Collection|MemberRelationInterface[]
*/ */
protected $members; protected $members;
/** /**
* @return Collection|SourceInterface[] * @return Collection|MemberRelationInterface[]
*/ */
public function getMembers(): Collection public function getMembers(): Collection
{ {
@ -24,7 +24,7 @@ trait MembersAttribut
} }
/** /**
* @param Collection|SourceInterface[] $members * @param Collection|MemberRelationInterface[] $members
*/ */
public function setMembers(Collection $members): void public function setMembers(Collection $members): void
{ {

View File

@ -3,7 +3,7 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface; use App\Entity\Meta\Relation\Member\MemberRelationInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -11,12 +11,12 @@ use App\Entity\Source\SourceInterface;
interface MembersAttributInterface interface MembersAttributInterface
{ {
/** /**
* @param Collection|SourceInterface[] $members * @param Collection|MemberRelationInterface[] $members
*/ */
public function setMembers(Collection $members): void; public function setMembers(Collection $members): void;
/** /**
* @return Collection|SourceInterface[] * @return Collection|MemberRelationInterface[]
*/ */
public function getMembers(): Collection; public function getMembers(): Collection;
} }

View File

@ -3,7 +3,7 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface; use App\Entity\Meta\Relation\Member\MemberRelationInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -11,12 +11,12 @@ use App\Entity\Source\SourceInterface;
trait MembershipsAttribut trait MembershipsAttribut
{ {
/** /**
* @var Collection|SourceInterface[] * @var Collection|MemberRelationInterface[]
*/ */
protected $memberships; protected $memberships;
/** /**
* @return Collection|SourceInterface[] * @return Collection|MemberRelationInterface[]
*/ */
public function getMemberships(): Collection public function getMemberships(): Collection
{ {
@ -24,7 +24,7 @@ trait MembershipsAttribut
} }
/** /**
* @param Collection|SourceInterface[] $memberships * @param Collection|MemberRelationInterface[] $memberships
*/ */
public function setMemberships(Collection $memberships): void public function setMemberships(Collection $memberships): void
{ {

View File

@ -3,7 +3,7 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface; use App\Entity\Meta\Relation\Member\MemberRelationInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -11,12 +11,12 @@ use App\Entity\Source\SourceInterface;
interface MembershipsAttributInterface interface MembershipsAttributInterface
{ {
/** /**
* @param Collection|SourceInterface[] $groups * @param Collection|MemberRelationInterface[] $groups
*/ */
public function setMemberships(Collection $memberships): void; public function setMemberships(Collection $memberships): void;
/** /**
* @return Collection|SourceInterface[] * @return Collection|MemberRelationInterface[]
*/ */
public function getMemberships(): Collection; public function getMemberships(): Collection;
} }

View File

@ -3,12 +3,22 @@
namespace App\Entity\Meta\Relation; namespace App\Entity\Meta\Relation;
use App\Entity\Meta\AbstractMeta; use App\Entity\Meta\AbstractMeta;
use App\Entity\Source\SourceInterface;
use Doctrine\ORM\Mapping as ORM;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
abstract class AbstractRelation extends AbstractMeta implements RelationInterface abstract class AbstractRelation extends AbstractMeta implements RelationInterface
{ {
/**
* @ORM\OneToOne(targetEntity="App\Entity\Source\AbstractSource",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="source_id", referencedColumnName="id",onDelete="CASCADE")
*
* @var SourceInterface
*/
protected $source;
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();

View File

@ -5,8 +5,40 @@ namespace App\Entity\Meta\Relation\Member;
use App\Entity\Meta\Relation\AbstractRelation; use App\Entity\Meta\Relation\AbstractRelation;
use App\Entity\Attribut\MembersAttribut; use App\Entity\Attribut\MembersAttribut;
use App\Entity\Attribut\MembershipsAttribut; use App\Entity\Attribut\MembershipsAttribut;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @author kevinfrantz
* @ORM\Entity()
*/
class MemberRelation extends AbstractRelation implements MemberRelationInterface class MemberRelation extends AbstractRelation implements MemberRelationInterface
{ {
use MembersAttribut,MembershipsAttribut; use MembersAttribut,MembershipsAttribut;
/**
* Many Sources have many Source Members.
*
* @var Collection|MemberRelationInterface[]
* @ORM\ManyToMany(targetEntity="MemberRelation", 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")}
* )
*/
protected $members;
/**
* @var Collection|MemberRelationInterface[]
* @ORM\ManyToMany(targetEntity="MemberRelation",mappedBy="members")
*/
protected $memberships;
public function __construct()
{
parent::__construct();
$this->members = new ArrayCollection();
$this->memberships = new ArrayCollection();
}
} }

View File

@ -4,7 +4,6 @@ namespace App\Entity\Meta\Relation\Parent;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -12,14 +11,6 @@ use App\Entity\Source\SourceInterface;
*/ */
class CreatorRelation extends AbstractParentRelation implements CreatorRelationInterface 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") * @ORM\ManyToMany(targetEntity="CreatorRelation",mappedBy="childs")
* *

View File

@ -5,19 +5,18 @@ namespace App\Entity\Source;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Exclude; use JMS\Serializer\Annotation\Exclude;
use App\Entity\AbstractEntity; use App\Entity\AbstractEntity;
use Doctrine\Common\Collections\Collection;
use App\Entity\Attribut\LawAttribut; use App\Entity\Attribut\LawAttribut;
use App\Entity\Meta\LawInterface; use App\Entity\Meta\LawInterface;
use App\Entity\Meta\Law; use App\Entity\Meta\Law;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attribut\MembershipsAttribut;
use App\Entity\Attribut\SlugAttribut; use App\Entity\Attribut\SlugAttribut;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\Validator\Constraints as Assert;
use App\Entity\Attribut\MembersAttribut;
use App\Entity\Attribut\CreatorRelationAttribut; 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\Meta\Relation\Parent\CreatorRelation;
use App\Entity\Attribut\MemberRelationAttribut;
use App\Entity\Meta\Relation\Member\MemberRelation;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -47,7 +46,7 @@ use App\Entity\Meta\Relation\Parent\CreatorRelation;
*/ */
abstract class AbstractSource extends AbstractEntity implements SourceInterface 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 * System slugs should be writen in UPPER CASES
@ -69,22 +68,12 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
protected $creatorRelation; protected $creatorRelation;
/** /**
* Many Sources have many Source Members. * @var CreatorRelationInterface
* * @ORM\OneToOne(targetEntity="App\Entity\Meta\Relation\Member\MemberRelation",cascade={"persist", "remove"})
* @var Collection|SourceInterface[] * @ORM\JoinColumn(name="member_relation_id", referencedColumnName="id", onDelete="CASCADE")
* @ORM\ManyToMany(targetEntity="AbstractSource", inversedBy="memberships",cascade={"persist", "remove"}) * @Exclude
* @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")}
* )
*/ */
protected $members; protected $memberRelation;
/**
* @var Collection|SourceInterface[]
* @ORM\ManyToMany(targetEntity="AbstractSource",mappedBy="members")
*/
protected $memberships;
/** /**
* @ORM\OneToOne(targetEntity="App\Entity\Meta\Law",cascade={"persist", "remove"}) * @ORM\OneToOne(targetEntity="App\Entity\Meta\Law",cascade={"persist", "remove"})
@ -99,6 +88,8 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
parent::__construct(); parent::__construct();
$this->creatorRelation = new CreatorRelation(); $this->creatorRelation = new CreatorRelation();
$this->creatorRelation->setSource($this); $this->creatorRelation->setSource($this);
$this->memberRelation = new MemberRelation();
$this->memberRelation->setSource($this);
$this->law = new Law(); $this->law = new Law();
$this->law->setSource($this); $this->law->setSource($this);
/* /*

View File

@ -5,14 +5,13 @@ namespace App\Entity\Source;
use App\Entity\Attribut\IdAttributInterface; use App\Entity\Attribut\IdAttributInterface;
use App\Entity\EntityInterface; use App\Entity\EntityInterface;
use App\Entity\Attribut\LawAttributInterface; use App\Entity\Attribut\LawAttributInterface;
use App\Entity\Attribut\MembershipsAttributInterface;
use App\Entity\Attribut\SlugAttributInterface; use App\Entity\Attribut\SlugAttributInterface;
use App\Entity\Attribut\MembersAttributInterface;
use App\Entity\Attribut\CreatorRelationAttributInterface; use App\Entity\Attribut\CreatorRelationAttributInterface;
use App\Entity\Attribut\MemberRelationAttributInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
interface SourceInterface extends IdAttributInterface, EntityInterface, MembershipsAttributInterface, LawAttributInterface, SlugAttributInterface, MembersAttributInterface, CreatorRelationAttributInterface interface SourceInterface extends IdAttributInterface, EntityInterface, LawAttributInterface, SlugAttributInterface, CreatorRelationAttributInterface, MemberRelationAttributInterface
{ {
} }

View File

@ -0,0 +1,50 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Domain\MemberManagement\MemberManagerInterface;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
use App\Entity\Meta\Relation\Member\MemberRelation;
use App\Domain\MemberManagement\MemberManager;
class MemberManagerTest extends TestCase
{
/**
* @var MemberRelationInterface
*/
private $memberRelation;
/**
* @var MemberManagerInterface
*/
private $MemberManager;
public function setUp(): void
{
$this->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());
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace Tests\Unit\Entity\Attribut;
use PHPUnit\Framework\TestCase;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attribut\MemberRelationAttributInterface;
use App\Entity\Attribut\MemberRelationAttribut;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
class MemberRelationAttributTest extends TestCase
{
/**
* @var MemberRelationAttributInterface
*/
protected $memberRelation;
public function setUp(): void
{
$this->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);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Tests\Unit\Entity\Meta\Relation\Member;
use PHPUnit\Framework\TestCase;
use App\Entity\Meta\Relation\Member\MemberRelation;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
use Doctrine\Common\Collections\Collection;
class MemberRelationTest extends TestCase
{
/**
* @var MemberRelationInterface
*/
private $memberRelation;
public function setUp(): void
{
$this->memberRelation = new MemberRelation();
}
public function testConstructor(): void
{
$this->assertInstanceOf(Collection::class, $this->memberRelation->getMembers());
$this->assertInstanceOf(Collection::class, $this->memberRelation->getMembership());
}
}