ActivityYearConstraint.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Doctrine\ORM\EntityManagerInterface;
  7. /**
  8. * Classe ActivityYearConstraint qui définie l'année de début (et de fin dans le cas d'une période custom)
  9. * par rapport aux contraintes temporelles choisies par un utilisateur.
  10. */
  11. class ActivityYearConstraint extends AbstractTimeConstraintUtils implements TimeConstraintInterface
  12. {
  13. public function __construct(
  14. private EntityManagerInterface $entityManager,
  15. private OrganizationUtils $organizationUtils
  16. ) {
  17. }
  18. /**
  19. * Main method.
  20. *
  21. * @return array<string, array<string, list<int>>>
  22. *
  23. * @throws \Exception
  24. */
  25. public function invoke(int $accessId): array
  26. {
  27. $access = $this->entityManager->getRepository(Access::class)->find($accessId);
  28. $historical = $access->getHistorical();
  29. $contraints = [
  30. self::START_KEY => [],
  31. self::END_KEY => [],
  32. ];
  33. if ($this->hasCustomPeriods($historical)) {
  34. $periods = $this->getRangeYear($access, $historical['dateStart'], $historical['dateEnd']);
  35. $contraints = $this->addConstraint($contraints, $this->customConstraint($periods));
  36. } else {
  37. $year = $access->getActivityYear();
  38. if ($historical['present']) {
  39. $contraints = $this->addConstraint($contraints, $this->presentConstraint($year));
  40. }
  41. if ($historical['past']) {
  42. $contraints = $this->addConstraint($contraints, $this->pastConstraint($year));
  43. }
  44. if ($historical['future']) {
  45. $contraints = $this->addConstraint($contraints, $this->futureConstraint($year));
  46. }
  47. }
  48. return $this->cleanConstraints($contraints);
  49. }
  50. /**
  51. * Retourne le tableau des années comprises dans la période custom.
  52. *
  53. * @return int[]
  54. */
  55. protected function getRangeYear(Access $access, string $dateStart, string $dateEnd): array
  56. {
  57. $organization = $access->getOrganization();
  58. return [
  59. OrganizationUtils::START_DATE_KEY => $this->organizationUtils->getActivityYearSwitchDate($organization, new \DateTime($dateStart)),
  60. OrganizationUtils::END_DATE_KEY => $this->organizationUtils->getActivityYearSwitchDate($organization, new \DateTime($dateEnd)),
  61. ];
  62. }
  63. /**
  64. * Une période est dans le présent si :
  65. * - l'année de début est égale (=) à l'année à afficher
  66. *
  67. * @return array<string, list<int>>
  68. */
  69. protected function presentConstraint(int $year): array
  70. {
  71. return [
  72. self::START_KEY => [
  73. $year => self::EQUAL,
  74. ],
  75. ];
  76. }
  77. /**
  78. * Une période est dans le passée si :
  79. * - l'année de début est plus petite (<) à l'année à afficher
  80. *
  81. * @return array<string, list<int>>
  82. */
  83. protected function pastConstraint(int $year): array
  84. {
  85. return [
  86. self::END_KEY => [
  87. $year => self::INF,
  88. ],
  89. ];
  90. }
  91. /**
  92. * Une période est dans le futur si :
  93. * - l'année de début est plus grande (>) à l'année à afficher
  94. *
  95. * @return array<string, list<int>>
  96. */
  97. protected function futureConstraint(int $year): array
  98. {
  99. return [
  100. self::START_KEY => [
  101. $year => self::SUP,
  102. ],
  103. ];
  104. }
  105. /**
  106. * Une période est dans une contrainte custom si :
  107. *
  108. * - l'année de début est plus grande ou égale (>=) à l'année de départ
  109. * - l'année de début est plus petite ou égale (<=) à l'année de fin
  110. *
  111. * @param array<int> $years
  112. *
  113. * @return array<string, list<int>>
  114. */
  115. protected function customConstraint(array $years): array
  116. {
  117. return [
  118. self::START_KEY => [
  119. $years[OrganizationUtils::START_DATE_KEY] => self::SUP + self::EQUAL,
  120. $years[OrganizationUtils::END_DATE_KEY] => self::INF + self::EQUAL,
  121. ],
  122. ];
  123. }
  124. }