From f1e78e7f5f359630c4299df1c5c27356d82a4b97 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 17 Nov 2018 13:29:43 +0100 Subject: [PATCH] Optimized TreeSourceService --- .../SourceManagement/TreeSourceService.php | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/application/src/Domain/SourceManagement/TreeSourceService.php b/application/src/Domain/SourceManagement/TreeSourceService.php index e00adea..319dabf 100644 --- a/application/src/Domain/SourceManagement/TreeSourceService.php +++ b/application/src/Domain/SourceManagement/TreeSourceService.php @@ -13,7 +13,7 @@ use App\Entity\Source\SourceInterface; * @author kevinfrantz * @todo Maybe lazy loading would be helpfull for performance */ -class TreeSourceService extends AbstractSourceService implements TreeSourceServiceInterface +final class TreeSourceService extends AbstractSourceService implements TreeSourceServiceInterface { /** * @var TreeCollectionSourceInterface @@ -62,22 +62,25 @@ class TreeSourceService extends AbstractSourceService implements TreeSourceServi * {@inheritDoc} * @see \App\Domain\SourceManagement\TreeSourceServiceInterface::getAllBranches() */ - public function getAllBranches(?ArrayCollection $excludedBranches=null): Collection + public function getAllBranches(): Collection { - if(!$excludedBranches){ - $excludedBranches = new ArrayCollection(); - } - $branches = new ArrayCollection($this->branches->toArray()); + $allBranches = new ArrayCollection($this->branches->toArray()); foreach($this->branches->toArray() as $branch){ - if(!$excludedBranches->contains($branch)){ - foreach((new TreeSourceService($branch))->getAllBranches($excludedBranches) as $branchBranch){ - $branches->add($branchBranch); - } + $this->itterateOverBranch($branch, $allBranches); + } + 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 { return $this->leaves; @@ -87,12 +90,13 @@ class TreeSourceService extends AbstractSourceService implements TreeSourceServi { $leaves = new ArrayCollection(); foreach ($this->getAllBranches()->toArray() as $branch){ - foreach ((new TreeSourceService($branch))->getLeaves() as $leave){ + foreach ((new self($branch))->getLeaves() as $leave){ if(!$leaves->contains($leave)){ $leaves->add($leave); } } } + return $leaves; } } \ No newline at end of file