Tree.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. private NetworkOrganizationRepository $networkOrganizationRepository;
  15. public function __construct(NetworkOrganizationRepository $networkOrganizationRepository)
  16. {
  17. $this->networkOrganizationRepository = $networkOrganizationRepository;
  18. }
  19. /**
  20. * Retrouve tous les parents d'une structure et les tries selon leur type principal
  21. * @param Organization $organization
  22. * @return array
  23. */
  24. public function findAllParentsAndSortByType(Organization $organization): array {
  25. return $this->sortByType($this->networkOrganizationRepository->findAllParents($organization));
  26. }
  27. /**
  28. * Trie les organisations par rapport à leur type principal :
  29. * DELEGATION, GROUPMENT, LOCAL_FEDERATION, DEPARTEMENTAL_FEDERATION, REGIONAL_FEDERATION, NATIONAL_FEDERATION
  30. * @param array $organizations
  31. * @return array
  32. * @see TreeTest::testSortByType()
  33. */
  34. public function sortByType(array $organizations): array {
  35. $typeOrder = [
  36. PrincipalTypeEnum::DELEGATION(),
  37. PrincipalTypeEnum::GROUPMENT(),
  38. PrincipalTypeEnum::LOCAL_FEDERATION(),
  39. PrincipalTypeEnum::DEPARTEMENTAL_FEDERATION(),
  40. PrincipalTypeEnum::REGIONAL_FEDERATION(),
  41. PrincipalTypeEnum::NATIONAL_FEDERATION()
  42. ];
  43. usort($organizations, function(Organization $organization1, Organization $organization2) use($typeOrder){
  44. $orderOrganization1 = array_keys($typeOrder, $organization1->getPrincipalType());
  45. $orderOrganization2 = array_keys($typeOrder, $organization2->getPrincipalType());
  46. if ($orderOrganization1 == $orderOrganization2) {
  47. return 0;
  48. }
  49. return ($orderOrganization1 < $orderOrganization2) ? -1 : 1;
  50. });
  51. return $organizations;
  52. }
  53. }