From c993a1864c73c8b1045a38b08062751f236c5ff0 Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Fri, 16 Nov 2018 20:44:54 +0100 Subject: [PATCH] Implemented draft for TreeSourceService --- .../src/Domain/AbstractDomainService.php | 7 ++ .../AbstractSourceService.php | 9 ++ .../SourceManagement/TreeSourceService.php | 98 +++++++++++++++++++ .../TreeSourceServiceInterface.php | 32 ++++++ .../src/Exception/NotSortableException.php | 6 ++ 5 files changed, 152 insertions(+) create mode 100644 application/src/Domain/AbstractDomainService.php create mode 100644 application/src/Domain/SourceManagement/AbstractSourceService.php create mode 100644 application/src/Domain/SourceManagement/TreeSourceService.php create mode 100644 application/src/Domain/SourceManagement/TreeSourceServiceInterface.php create mode 100644 application/src/Exception/NotSortableException.php diff --git a/application/src/Domain/AbstractDomainService.php b/application/src/Domain/AbstractDomainService.php new file mode 100644 index 0000000..8992f3a --- /dev/null +++ b/application/src/Domain/AbstractDomainService.php @@ -0,0 +1,7 @@ +source = $source; + $this->branches = new ArrayCollection(); + $this->leaves = new ArrayCollection(); + } + + private function sortMember(SourceInterface $member):bool{ + if($member instanceof TreeCollectionSource){ + return $this->branches->add($member); + } + return $this->leaves->add($member); + } + + private function basicSort():void{ + foreach($this->source->getCollection() as $member){ + $this->sortMember($member); + } + } + + public function getBranches(): Collection + { + return $this->branches; + } + + /** + * @todo Remove the optional parameter and put the logic in a private funtion. + * @todo Remove the getAllBranches use inside the function. + * {@inheritDoc} + * @see \App\Domain\SourceManagement\TreeSourceServiceInterface::getAllBranches() + */ + public function getAllBranches(?ArrayCollection $excludedBranches=null): Collection + { + if(!$excludedBranches){ + $excludedBranches = new ArrayCollection(); + } + $branches = 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); + } + } + } + return $branches; + } + + public function getLeaves(): Collection + { + return $this->leaves; + } + + public function getAllLeaves(): Collection + { + $leaves = new ArrayCollection(); + foreach ($this->getAllBranches()->toArray() as $branch){ + foreach ((new TreeSourceService($branch))->getLeaves() as $leave){ + if(!$leaves->contains($leave)){ + $leaves->add($leave); + } + } + } + } + +} \ No newline at end of file diff --git a/application/src/Domain/SourceManagement/TreeSourceServiceInterface.php b/application/src/Domain/SourceManagement/TreeSourceServiceInterface.php new file mode 100644 index 0000000..8aa7da9 --- /dev/null +++ b/application/src/Domain/SourceManagement/TreeSourceServiceInterface.php @@ -0,0 +1,32 @@ +