Optimized TreeSourceService

This commit is contained in:
Kevin Frantz 2018-11-17 13:29:43 +01:00
parent c993a1864c
commit f1e78e7f5f

View File

@ -13,7 +13,7 @@ use App\Entity\Source\SourceInterface;
* @author kevinfrantz * @author kevinfrantz
* @todo Maybe lazy loading would be helpfull for performance * @todo Maybe lazy loading would be helpfull for performance
*/ */
class TreeSourceService extends AbstractSourceService implements TreeSourceServiceInterface final class TreeSourceService extends AbstractSourceService implements TreeSourceServiceInterface
{ {
/** /**
* @var TreeCollectionSourceInterface * @var TreeCollectionSourceInterface
@ -62,22 +62,25 @@ class TreeSourceService extends AbstractSourceService implements TreeSourceServi
* {@inheritDoc} * {@inheritDoc}
* @see \App\Domain\SourceManagement\TreeSourceServiceInterface::getAllBranches() * @see \App\Domain\SourceManagement\TreeSourceServiceInterface::getAllBranches()
*/ */
public function getAllBranches(?ArrayCollection $excludedBranches=null): Collection public function getAllBranches(): Collection
{ {
if(!$excludedBranches){ $allBranches = new ArrayCollection($this->branches->toArray());
$excludedBranches = new ArrayCollection();
}
$branches = new ArrayCollection($this->branches->toArray());
foreach($this->branches->toArray() as $branch){ foreach($this->branches->toArray() as $branch){
if(!$excludedBranches->contains($branch)){ $this->itterateOverBranch($branch, $allBranches);
foreach((new TreeSourceService($branch))->getAllBranches($excludedBranches) as $branchBranch){ }
$branches->add($branchBranch); return $allBranches;
} }
private function itterateOverBranch(TreeCollectionSourceInterface $branch,ArrayCollection $allBranches):void{
if(!$allBranches->contains($branch)){
$allBranches->add($branch);
foreach((new self($branch))->getBranches() as $branchBranch){
$allBranches->add($branchBranch);
} }
} }
return $branches;
} }
public function getLeaves(): Collection public function getLeaves(): Collection
{ {
return $this->leaves; return $this->leaves;
@ -87,12 +90,13 @@ class TreeSourceService extends AbstractSourceService implements TreeSourceServi
{ {
$leaves = new ArrayCollection(); $leaves = new ArrayCollection();
foreach ($this->getAllBranches()->toArray() as $branch){ foreach ($this->getAllBranches()->toArray() as $branch){
foreach ((new TreeSourceService($branch))->getLeaves() as $leave){ foreach ((new self($branch))->getLeaves() as $leave){
if(!$leaves->contains($leave)){ if(!$leaves->contains($leave)){
$leaves->add($leave); $leaves->add($leave);
} }
} }
} }
return $leaves;
} }
} }