mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 22:17:26 +01:00
Optimized SourceMembershipInformation
This commit is contained in:
parent
beb6cb3e90
commit
e30145d792
@ -32,8 +32,7 @@ final class SourceMemberInformation implements SourceMemberInformationInterface
|
||||
foreach ($members as $member) {
|
||||
if (!$this->members->contains($member->getSource())) {
|
||||
$this->members->add($member->getSource());
|
||||
$memberMembers = $member->getMembers();
|
||||
$this->itterateOverMembers($memberMembers);
|
||||
$this->itterateOverMembers($member->getMembers());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ namespace App\Domain\SourceManagement;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use App\Entity\Source\SourceInterface;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use App\Entity\Meta\Relation\Member\MemberRelationInterface;
|
||||
|
||||
final class SourceMembershipInformation implements SourceMembershipInformationInterface
|
||||
{
|
||||
@ -23,16 +24,24 @@ final class SourceMembershipInformation implements SourceMembershipInformationIn
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection|MemberRelationInterface[] $memberships
|
||||
*/
|
||||
private function itterateOverMemberships(Collection $memberships): void
|
||||
{
|
||||
foreach ($memberships as $membership) {
|
||||
if (!$this->memberships->contains($membership)) {
|
||||
$this->memberships->add($membership);
|
||||
$this->itterateOverMemberships($membership->getMemberRelation()->getMemberships());
|
||||
if (!$this->memberships->contains($membership->getSource())) {
|
||||
$this->memberships->add($membership->getSource());
|
||||
$this->itterateOverMemberships($membership->getMemberships());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @see \App\Domain\SourceManagement\SourceMembershipInformationInterface::getAllMemberships()
|
||||
*/
|
||||
public function getAllMemberships(): Collection
|
||||
{
|
||||
$this->memberships = new ArrayCollection();
|
||||
|
@ -8,6 +8,7 @@ use App\Domain\SourceManagement\SourceMemberManagerInterface;
|
||||
use App\Entity\Source\AbstractSource;
|
||||
use App\Domain\SourceManagement\SourceMemberManager;
|
||||
use App\Domain\SourceManagement\SourceMemberInformation;
|
||||
use App\Domain\SourceManagement\SourceMembershipInformation;
|
||||
|
||||
class SourceMemberManagerIntegrationTest extends TestCase
|
||||
{
|
||||
@ -42,4 +43,14 @@ class SourceMemberManagerIntegrationTest extends TestCase
|
||||
$this->sourceMemberManager->removeMember($childSource);
|
||||
$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());
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ class SourceMembershipInformationTest extends TestCase
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
@ -40,18 +40,25 @@ class SourceMembershipInformationTest extends TestCase
|
||||
{
|
||||
$source1 = new TextSource();
|
||||
$source2 = new FirstNameSource();
|
||||
$source2->getMemberRelation()->setMemberships(new ArrayCollection([$source1]));
|
||||
$source2->getMemberRelation()->setMemberships(new ArrayCollection([$source1->getMemberRelation()]));
|
||||
$source3 = new FullPersonNameSource();
|
||||
$source3->getMemberRelation()->getMemberships()->add($source2);
|
||||
$this->source->getMemberRelation()->getMemberships()->add($source3);
|
||||
$source3->getMemberRelation()->getMemberships()->add($source2->getMemberRelation());
|
||||
$this->source->getMemberRelation()->getMemberships()->add($source3->getMemberRelation());
|
||||
$this->assertEquals(3, $this->sourceMembershipInformation->getAllMemberships()->count());
|
||||
}
|
||||
|
||||
public function testRecursion(): void
|
||||
{
|
||||
$recursiveSource = new UserSource();
|
||||
$recursiveSource->getMemberRelation()->getMemberships()->add($this->source);
|
||||
$this->source->getMemberRelation()->getMemberships()->add($recursiveSource);
|
||||
$recursiveSource->getMemberRelation()->getMemberships()->add($this->source->getMemberRelation());
|
||||
$this->source->getMemberRelation()->getMemberships()->add($recursiveSource->getMemberRelation());
|
||||
$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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user