Tree.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Network;
  4. use App\Entity\Organization\Organization;
  5. use App\Enum\Organization\PrincipalTypeEnum;
  6. use App\Repository\Network\NetworkOrganizationRepository;
  7. use App\Tests\Service\Network\TreeTest;
  8. /**
  9. * Class Tree : service rassemblant des fonction pour répondre aux poblématique d'arbre du réseau
  10. * @package App\Service\Network
  11. */
  12. class Tree
  13. {
  14. public function __construct(private NetworkOrganizationRepository $networkOrganizationRepository)
  15. { }
  16. /**
  17. * Retrouve tous les parents d'une structure et les tries selon leur type principal
  18. * @param Organization $organization
  19. * @return array
  20. */
  21. public function findAllParentsAndSortByType(Organization $organization): array {
  22. return $this->sortByType($this->networkOrganizationRepository->findAllParents($organization));
  23. }
  24. /**
  25. * Trie les organisations par rapport à leur type principal :
  26. * DELEGATION, GROUPMENT, LOCAL_FEDERATION, DEPARTEMENTAL_FEDERATION, REGIONAL_FEDERATION, NATIONAL_FEDERATION
  27. * @param array $organizations
  28. * @return array
  29. * @see TreeTest::testSortByType()
  30. */
  31. public function sortByType(array $organizations): array {
  32. $typeOrder = [
  33. PrincipalTypeEnum::DELEGATION(),
  34. PrincipalTypeEnum::GROUPMENT(),
  35. PrincipalTypeEnum::LOCAL_FEDERATION(),
  36. PrincipalTypeEnum::DEPARTEMENTAL_FEDERATION(),
  37. PrincipalTypeEnum::REGIONAL_FEDERATION(),
  38. PrincipalTypeEnum::NATIONAL_FEDERATION()
  39. ];
  40. usort($organizations, function(Organization $organization1, Organization $organization2) use($typeOrder){
  41. return array_keys($typeOrder, $organization1->getPrincipalType()) <=> array_keys($typeOrder, $organization2->getPrincipalType());
  42. });
  43. return $organizations;
  44. }
  45. }