ContactPointUtils.php 1.1 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. /**
  18. * Renvoie le point de contact principal de l'Access, ou null si aucun trouvé.
  19. *
  20. * @see ContactPointUtilsTest::testGetPersonContactPointPrincipal()
  21. */
  22. public function getPersonContactPointPrincipal(Access $access): ?ContactPoint
  23. {
  24. $contactPoints = $this->contactPointRepository->getByTypeAndPerson(
  25. ContactPointTypeEnum::PRINCIPAL,
  26. $access->getPerson()
  27. );
  28. if (count($contactPoints) === 0) {
  29. return null;
  30. }
  31. if (count($contactPoints) !== 1) {
  32. throw new \RuntimeException('more_than_one_result');
  33. }
  34. return $contactPoints[0];
  35. }
  36. }