Optimized SourceMembershipInformation

This commit is contained in:
Kevin Frantz 2018-12-30 17:09:44 +01:00
parent beb6cb3e90
commit e30145d792
4 changed files with 37 additions and 11 deletions

View File

@ -32,8 +32,7 @@ final class SourceMemberInformation implements SourceMemberInformationInterface
foreach ($members as $member) { foreach ($members as $member) {
if (!$this->members->contains($member->getSource())) { if (!$this->members->contains($member->getSource())) {
$this->members->add($member->getSource()); $this->members->add($member->getSource());
$memberMembers = $member->getMembers(); $this->itterateOverMembers($member->getMembers());
$this->itterateOverMembers($memberMembers);
} }
} }
} }

View File

@ -5,6 +5,7 @@ namespace App\Domain\SourceManagement;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface; use App\Entity\Source\SourceInterface;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
final class SourceMembershipInformation implements SourceMembershipInformationInterface final class SourceMembershipInformation implements SourceMembershipInformationInterface
{ {
@ -23,16 +24,24 @@ final class SourceMembershipInformation implements SourceMembershipInformationIn
$this->source = $source; $this->source = $source;
} }
/**
* @param Collection|MemberRelationInterface[] $memberships
*/
private function itterateOverMemberships(Collection $memberships): void private function itterateOverMemberships(Collection $memberships): void
{ {
foreach ($memberships as $membership) { foreach ($memberships as $membership) {
if (!$this->memberships->contains($membership)) { if (!$this->memberships->contains($membership->getSource())) {
$this->memberships->add($membership); $this->memberships->add($membership->getSource());
$this->itterateOverMemberships($membership->getMemberRelation()->getMemberships()); $this->itterateOverMemberships($membership->getMemberships());
} }
} }
} }
/**
* {@inheritdoc}
*
* @see \App\Domain\SourceManagement\SourceMembershipInformationInterface::getAllMemberships()
*/
public function getAllMemberships(): Collection public function getAllMemberships(): Collection
{ {
$this->memberships = new ArrayCollection(); $this->memberships = new ArrayCollection();

View File

@ -8,6 +8,7 @@ use App\Domain\SourceManagement\SourceMemberManagerInterface;
use App\Entity\Source\AbstractSource; use App\Entity\Source\AbstractSource;
use App\Domain\SourceManagement\SourceMemberManager; use App\Domain\SourceManagement\SourceMemberManager;
use App\Domain\SourceManagement\SourceMemberInformation; use App\Domain\SourceManagement\SourceMemberInformation;
use App\Domain\SourceManagement\SourceMembershipInformation;
class SourceMemberManagerIntegrationTest extends TestCase class SourceMemberManagerIntegrationTest extends TestCase
{ {
@ -42,4 +43,14 @@ class SourceMemberManagerIntegrationTest extends TestCase
$this->sourceMemberManager->removeMember($childSource); $this->sourceMemberManager->removeMember($childSource);
$this->assertEquals(0, $sourceMemberInformation->getAllMembers()->count()); $this->assertEquals(0, $sourceMemberInformation->getAllMembers()->count());
} }
public function testSourceMembershipInformationIntegration(): void
{
$parentSource = $this->createSource();
$sourceMemberInformation = new SourceMembershipInformation($this->source);
$this->sourceMemberManager->addMembership($parentSource);
$this->assertEquals($parentSource, $sourceMemberInformation->getAllMemberships()->get(0));
$this->sourceMemberManager->removeMembership($parentSource);
$this->assertEquals(0, $sourceMemberInformation->getAllMemberships()->count());
}
} }

View File

@ -32,7 +32,7 @@ class SourceMembershipInformationTest extends TestCase
public function testOneDimension(): void public function testOneDimension(): void
{ {
$this->source->getMemberRelation()->getMemberships()->add(new TextSource()); $this->source->getMemberRelation()->getMemberships()->add((new TextSource())->getMemberRelation());
$this->assertEquals(1, $this->sourceMembershipInformation->getAllMemberships()->count()); $this->assertEquals(1, $this->sourceMembershipInformation->getAllMemberships()->count());
} }
@ -40,18 +40,25 @@ class SourceMembershipInformationTest extends TestCase
{ {
$source1 = new TextSource(); $source1 = new TextSource();
$source2 = new FirstNameSource(); $source2 = new FirstNameSource();
$source2->getMemberRelation()->setMemberships(new ArrayCollection([$source1])); $source2->getMemberRelation()->setMemberships(new ArrayCollection([$source1->getMemberRelation()]));
$source3 = new FullPersonNameSource(); $source3 = new FullPersonNameSource();
$source3->getMemberRelation()->getMemberships()->add($source2); $source3->getMemberRelation()->getMemberships()->add($source2->getMemberRelation());
$this->source->getMemberRelation()->getMemberships()->add($source3); $this->source->getMemberRelation()->getMemberships()->add($source3->getMemberRelation());
$this->assertEquals(3, $this->sourceMembershipInformation->getAllMemberships()->count()); $this->assertEquals(3, $this->sourceMembershipInformation->getAllMemberships()->count());
} }
public function testRecursion(): void public function testRecursion(): void
{ {
$recursiveSource = new UserSource(); $recursiveSource = new UserSource();
$recursiveSource->getMemberRelation()->getMemberships()->add($this->source); $recursiveSource->getMemberRelation()->getMemberships()->add($this->source->getMemberRelation());
$this->source->getMemberRelation()->getMemberships()->add($recursiveSource); $this->source->getMemberRelation()->getMemberships()->add($recursiveSource->getMemberRelation());
$this->assertEquals(2, $this->sourceMembershipInformation->getAllMemberships()->count()); $this->assertEquals(2, $this->sourceMembershipInformation->getAllMemberships()->count());
} }
public function testError(): void
{
$this->expectException(\Error::class);
$this->source->getMemberRelation()->getMemberships()->add($this->createSourceMock());
$this->sourceMembershipInformation->getAllMemberships();
}
} }