DateTimeConstraint.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Constraint;
  4. use App\Entity\Access\Access;
  5. use App\Service\Organization\Utils as OrganizationUtils;
  6. use App\Tests\Service\Constraint\DateTimeConstraintTest;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. /**
  10. * Classe DateTimeConstraint qui définie les dates de débuts et fin de périodes
  11. * par rapport au contraintes temporelles choisies par un utilisateur.
  12. */
  13. class DateTimeConstraint extends AbstractTimeConstraintUtils
  14. {
  15. public function __construct(
  16. readonly private EntityManagerInterface $entityManager,
  17. readonly private OrganizationUtils $organizationUtils
  18. )
  19. { }
  20. /**
  21. * Main méthod
  22. * @param int $accessID
  23. * @return array<string, array<string, list<int>>>
  24. * @throws Exception
  25. */
  26. public function invoke(int $accessID): array
  27. {
  28. $access = $this->entityManager->getRepository(Access::class)->find($accessID);
  29. $historical = $access->getHistorical();
  30. $contraints = [
  31. self::START_KEY => [],
  32. self::END_KEY => []
  33. ];
  34. if($this->hasCustomPeriods($historical)){
  35. $periods = $this->getCustomPeriods($historical['dateStart'], $historical['dateEnd']);
  36. //Une période "Custom" reviens à savoir quels sont les éléments actif durant le "présent" de la période custom, donc
  37. //on peut utiliser le presentConstraint avec les periods custom
  38. $contraints = $this->addConstraint($contraints, $this->presentConstraint($periods));
  39. }else{
  40. $periods = $this->getPeriods($access);
  41. if($historical['present']) $contraints = $this->addConstraint($contraints, $this->presentConstraint($periods));
  42. if($historical['past']) $contraints = $this->addConstraint($contraints, $this->pastConstraint($periods));
  43. if($historical['future']) $contraints = $this->addConstraint($contraints, $this->futureConstraint($periods));
  44. }
  45. return $this->cleanConstraints($contraints);
  46. }
  47. /**
  48. * Retourne le tableau des périodes custom
  49. * @param string $dateStart
  50. * @param string $dateEnd
  51. * @return string[]
  52. */
  53. protected function getCustomPeriods(string $dateStart, string $dateEnd): array
  54. {
  55. return [
  56. OrganizationUtils::START_DATE_KEY => $dateStart,
  57. OrganizationUtils::END_DATE_KEY => $dateEnd
  58. ];
  59. }
  60. /**
  61. * Fonction permettant de récupérer les périodes de début et de fin d'affichage
  62. *
  63. * @param Access $access
  64. * @return string[]
  65. * @throws Exception
  66. * @see DateTimeConstraintTest::testGetPeriodsToday()
  67. */
  68. protected function getPeriods(Access $access): array
  69. {
  70. $organization = $access->getOrganization();
  71. $activityYear = $access->getActivityYear();
  72. $currentActivityYear = $this->organizationUtils->getOrganizationCurrentActivityYear($organization);
  73. $periods = $this->organizationUtils->getActivityPeriodsSwitchYear($organization, $activityYear);
  74. // Si l'année courante est l'année d'affichage choisie par l'utilisateur, alors la date de début est aujourd'hui
  75. if($activityYear === $currentActivityYear){
  76. $today = new \DateTime('now');
  77. $periods[OrganizationUtils::START_DATE_KEY] = $today->format('Y-m-d');
  78. }
  79. return $periods;
  80. }
  81. /**
  82. * Une période est dans le présent si :
  83. * - la date de début est plus petite ou égale (<=) à la date de fin de période à afficher
  84. * ET
  85. * - la date de fin est plus grande ou égale (>=) à la date de fin de période à afficher OU que la date de fin n'est pas remplie (NULL)
  86. *
  87. * @param string[] $periods
  88. * @return array<string, int[]>
  89. *
  90. * @see DateTimeConstraintTest::testPresentConstrain()
  91. */
  92. protected function presentConstraint(array $periods): array
  93. {
  94. return [
  95. self::START_KEY => [
  96. $periods[OrganizationUtils::END_DATE_KEY] => self::INF + self::EQUAL
  97. ],
  98. self::END_KEY => [
  99. $periods[OrganizationUtils::START_DATE_KEY] => self::SUP + self::EQUAL,
  100. self::NULL_VALUE => self::NULL
  101. ]
  102. ];
  103. }
  104. /**
  105. * Une période est dans le passé si :
  106. * - la date de fin est plus petite (<) que la date de début de période à afficher
  107. * @param string[] $periods
  108. * @return array<string, int[]>
  109. * @see DateTimeConstraintTest::testPastConstrain()
  110. */
  111. protected function pastConstraint(array $periods): array
  112. {
  113. return [
  114. self::END_KEY => [
  115. $periods[OrganizationUtils::START_DATE_KEY] => self::INF
  116. ]
  117. ];
  118. }
  119. /**
  120. * Une période est dans le futur si :
  121. * - la date de début est plus grande (>) que la date de fin de période à afficher
  122. * @param string[] $periods
  123. * @return array<string, int[]>
  124. * @see DateTimeConstraintTest::testFuturConstrain()
  125. */
  126. protected function futureConstraint(array $periods): array
  127. {
  128. return [
  129. self::START_KEY => [
  130. $periods[OrganizationUtils::END_DATE_KEY] => self::SUP
  131. ]
  132. ];
  133. }
  134. }