ContactPointUtils.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Core;
  4. use App\Entity\Access\Access;
  5. use App\Entity\Core\ContactPoint;
  6. use App\Enum\Core\ContactPointTypeEnum;
  7. use App\Repository\Core\ContactPointRepository;
  8. use App\Test\Service\Access\ContactPointUtilsTest;
  9. /**
  10. * Classe ContactPointUtils qui possédant les fonctions utils à l'entité ContactPoint
  11. */
  12. class ContactPointUtils
  13. {
  14. public function __construct(private ContactPointRepository $contactPointRepository){
  15. }
  16. /**
  17. * Renvoie le point de contact principal de l'Access, ou null si aucun trouvé.
  18. *
  19. * @param Access $access
  20. * @return ContactPoint|null
  21. * @throws \Exception
  22. * @see ContactPointUtilsTest::testGetPersonContactPointPrincipal()
  23. */
  24. public function getPersonContactPointPrincipal(Access $access): ?ContactPoint {
  25. $contactPoints = $this->contactPointRepository->getByTypeAndPerson(
  26. ContactPointTypeEnum::PRINCIPAL()->getValue(),
  27. $access->getPerson()
  28. );
  29. if (count($contactPoints) === 0) {
  30. return null;
  31. }
  32. if (count($contactPoints) !== 1){
  33. throw new \RuntimeException('more_than_one_result');
  34. }
  35. return $contactPoints[0];
  36. }
  37. }