mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-10-31 09:19:08 +00:00 
			
		
		
		
	Implemented draft for TreeSourceService
This commit is contained in:
		
							
								
								
									
										7
									
								
								application/src/Domain/AbstractDomainService.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								application/src/Domain/AbstractDomainService.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | ||||
| <?php | ||||
| namespace App\Domain; | ||||
|  | ||||
| abstract class AbstractDomainService | ||||
| { | ||||
| } | ||||
|  | ||||
| @@ -0,0 +1,9 @@ | ||||
| <?php | ||||
| namespace App\Domain\SourceManagement; | ||||
|  | ||||
| use App\Domain\AbstractDomainService; | ||||
|  | ||||
| abstract class AbstractSourceService extends AbstractDomainService | ||||
| { | ||||
| } | ||||
|  | ||||
| @@ -0,0 +1,98 @@ | ||||
| <?php | ||||
| namespace App\Domain\SourceManagement; | ||||
|  | ||||
| use App\Entity\Source\Collection\TreeCollectionSourceInterface; | ||||
| use App\Entity\Source\Collection\TreeCollectionSource; | ||||
| use Doctrine\Common\Collections\Collection; | ||||
| use Doctrine\Common\Collections\ArrayCollection; | ||||
| use App\Entity\Source\SourceInterface; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Allows to iterate over a tree | ||||
|  * @author kevinfrantz | ||||
|  * @todo Maybe lazy loading would be helpfull for performance | ||||
|  */ | ||||
| class TreeSourceService extends AbstractSourceService implements TreeSourceServiceInterface | ||||
| { | ||||
|     /** | ||||
|      * @var TreeCollectionSourceInterface | ||||
|      */ | ||||
|     private $source; | ||||
|      | ||||
|     /** | ||||
|      * Containes all branches of the actual level of the tree | ||||
|      * @var Collection | ||||
|      */ | ||||
|     private $branches; | ||||
|      | ||||
|     /** | ||||
|      * Containes all leaves of the actual level of the tree | ||||
|      * @var Collection | ||||
|      */ | ||||
|     private $leaves; | ||||
|      | ||||
|     public function __construct(TreeCollectionSource $source){ | ||||
|         $this->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); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|   | ||||
| } | ||||
| @@ -0,0 +1,32 @@ | ||||
| <?php | ||||
| namespace App\Domain\SourceManagement; | ||||
|  | ||||
| use Doctrine\Common\Collections\Collection; | ||||
|  | ||||
| interface TreeSourceServiceInterface | ||||
| { | ||||
|     /** | ||||
|      * Delivers the branches of the actual tree back | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function getBranches():Collection; | ||||
|      | ||||
|     /** | ||||
|      * Delivers the members of the actual tree back | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function getLeaves():Collection; | ||||
|      | ||||
|     /** | ||||
|      * Delivers all members till a infinite level of the tree back | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function getAllLeaves():Collection; | ||||
|      | ||||
|     /** | ||||
|      * Delivers all branches till a infinite level of the actual tree back | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function getAllBranches():Collection; | ||||
| } | ||||
|  | ||||
							
								
								
									
										6
									
								
								application/src/Exception/NotSortableException.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								application/src/Exception/NotSortableException.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| <?php | ||||
| namespace App\Exception; | ||||
|  | ||||
| class NotSortableException extends \Exception | ||||
| { | ||||
| } | ||||
		Reference in New Issue
	
	Block a user