Added SourceMemberInformation

This commit is contained in:
Kevin Frantz 2018-12-06 20:22:27 +01:00
parent 61a6ea6b36
commit e97003b91d
6 changed files with 122 additions and 10 deletions

View File

@ -0,0 +1,43 @@
<?php
namespace App\Domain\SourceManagement;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
use Doctrine\Common\Collections\ArrayCollection;
final class SourceMemberInformation implements SourceMemberInformationInterface
{
/**
* @var SourceInterface
*/
private $source;
/**
* @var Collection|SourceInterface[]
*/
private $members;
public function __construct(SourceInterface $source)
{
$this->source = $source;
}
private function itterateOverMembers(Collection $members): void
{
foreach ($members as $member) {
if (!$this->members->contains($member)) {
$this->members->add($member);
$this->itterateOverMembers($member->getMemberRelation()->getMembers());
}
}
}
public function getAllMembers(): Collection
{
$this->members = new ArrayCollection();
$this->itterateOverMembers($this->source->getMemberRelation()->getMembers());
return $this->members;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Domain\SourceManagement;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
interface SourceMemberInformationInterface
{
/**
* @return Collection|SourceInterface[] All Members which belong to a source
*/
public function getAllMembers(): Collection;
}

View File

@ -6,7 +6,7 @@ use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
use Doctrine\Common\Collections\ArrayCollection;
class SourceMembershipInformation implements SourceMembershipInformationInterface
final class SourceMembershipInformation implements SourceMembershipInformationInterface
{
/**
* @var SourceInterface
@ -14,7 +14,7 @@ class SourceMembershipInformation implements SourceMembershipInformationInterfac
private $source;
/**
* @var Collection
* @var Collection|SourceInterface[]
*/
private $memberships;

View File

@ -3,8 +3,12 @@
namespace App\Domain\SourceManagement;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
interface SourceMembershipInformationInterface
{
/**
* @return Collection|SourceInterface[] all Sources which a Source belongs to
*/
public function getAllMemberships(): Collection;
}

View File

@ -8,7 +8,6 @@ use App\Entity\AbstractEntity;
use App\Entity\Attribut\LawAttribut;
use App\Entity\Meta\LawInterface;
use App\Entity\Meta\Law;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Attribut\SlugAttribut;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
@ -17,6 +16,7 @@ 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;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
/**
* @author kevinfrantz
@ -68,7 +68,7 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
protected $creatorRelation;
/**
* @var CreatorRelationInterface
* @var MemberRelationInterface
* @ORM\OneToOne(targetEntity="App\Entity\Meta\Relation\Member\MemberRelation",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="member_relation_id", referencedColumnName="id", onDelete="CASCADE")
* @Exclude
@ -92,11 +92,5 @@ abstract class AbstractSource extends AbstractEntity implements SourceInterface
$this->memberRelation->setSource($this);
$this->law = new Law();
$this->law->setSource($this);
/*
*
* @todo Refactor the following attibutes
*/
$this->memberships = new ArrayCollection();
$this->members = new ArrayCollection();
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace Tests\Unit\Domain\SourceManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Source\SourceInterface;
use App\Entity\Source\Complex\UserSource;
use App\Entity\Source\Primitive\Text\TextSource;
use App\Entity\Source\Primitive\Name\FirstNameSource;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Source\Complex\FullPersonNameSource;
use App\Domain\SourceManagement\SourceMemberInformation;
use App\Domain\SourceManagement\SourceMemberInformationInterface;
class SourceMemberInformationTest extends TestCase
{
/**
* @var SourceInterface
*/
protected $source;
/**
* @var SourceMemberInformationInterface
*/
protected $sourceMemberInformation;
public function setUp(): void
{
$this->source = new UserSource();
$this->sourceMemberInformation = new SourceMemberInformation($this->source);
}
public function testOneDimension(): void
{
$this->source->getMemberRelation()->getMembers()->add(new TextSource());
$this->assertEquals(1, $this->sourceMemberInformation->getAllMembers()->count());
}
public function testThreeDimension(): void
{
$source1 = new TextSource();
$source2 = new FirstNameSource();
$source2->getMemberRelation()->setMembers(new ArrayCollection([$source1]));
$source3 = new FullPersonNameSource();
$source3->getMemberRelation()->getMembers()->add($source2);
$this->source->getMemberRelation()->getMembers()->add($source3);
$this->assertEquals(3, $this->sourceMemberInformation->getAllMembers()->count());
}
public function testRecursion(): void
{
$recursiveSource = new UserSource();
$recursiveSource->getMemberRelation()->getMembers()->add($this->source);
$this->source->getMemberRelation()->getMembers()->add($recursiveSource);
$this->assertEquals(2, $this->sourceMemberInformation->getAllMembers()->count());
}
}