ActivityYearConstraint.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. * @param int $accessId
  22. * @return array<string, array<string, list<int>>>
  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']) $contraints = $this->addConstraint($contraints, $this->presentConstraint($year));
  39. if($historical['past']) $contraints = $this->addConstraint($contraints, $this->pastConstraint($year));
  40. if($historical['future']) $contraints = $this->addConstraint($contraints, $this->futureConstraint($year));
  41. }
  42. return $this->cleanConstraints($contraints);
  43. }
  44. /**
  45. * Retourne le tableau des années comprises dans la période custom
  46. * @param Access $access
  47. * @param string $dateStart
  48. * @param string $dateEnd
  49. * @return int[]
  50. */
  51. protected function getRangeYear(Access $access, string $dateStart, string $dateEnd): array
  52. {
  53. $organization = $access->getOrganization();
  54. return [
  55. OrganizationUtils::START_DATE_KEY => $this->organizationUtils->getActivityYearSwitchDate($organization, new \DateTime($dateStart)),
  56. OrganizationUtils::END_DATE_KEY => $this->organizationUtils->getActivityYearSwitchDate($organization, new \DateTime($dateEnd))
  57. ];
  58. }
  59. /**
  60. * Une période est dans le présent si :
  61. * - l'année de début est égale (=) à l'année à afficher
  62. * @param int $year
  63. * @return array<string, list<int>>
  64. */
  65. protected function presentConstraint(int $year): array
  66. {
  67. return [
  68. self::START_KEY => [
  69. $year => self::EQUAL
  70. ]
  71. ];
  72. }
  73. /**
  74. * Une période est dans le passée si :
  75. * - l'année de début est plus petite (<) à l'année à afficher
  76. * @param int $year
  77. * @return array<string, list<int>>
  78. */
  79. protected function pastConstraint(int $year): array
  80. {
  81. return [
  82. self::END_KEY => [
  83. $year => self::INF
  84. ]
  85. ];
  86. }
  87. /**
  88. * Une période est dans le futur si :
  89. * - l'année de début est plus grande (>) à l'année à afficher
  90. * @param int $year
  91. * @return array<string, list<int>>
  92. */
  93. protected function futureConstraint(int $year): array
  94. {
  95. return [
  96. self::START_KEY => [
  97. $year => self::SUP
  98. ]
  99. ];
  100. }
  101. /**
  102. * Une période est dans une contrainte custom si :
  103. *
  104. * - l'année de début est plus grande ou égale (>=) à l'année de départ
  105. * - l'année de début est plus petite ou égale (<=) à l'année de fin
  106. *
  107. * @param array<int> $years
  108. * @return array<string, list<int>>
  109. */
  110. protected function customConstraint(array $years): array
  111. {
  112. return [
  113. self::START_KEY => [
  114. $years[OrganizationUtils::START_DATE_KEY] => self::SUP + self::EQUAL,
  115. $years[OrganizationUtils::END_DATE_KEY] => self::INF + self::EQUAL
  116. ]
  117. ];
  118. }
  119. }