| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Constraint;
- use App\Entity\Access\Access;
- use App\Service\Organization\Utils as OrganizationUtils;
- use Doctrine\ORM\EntityManagerInterface;
- /**
- * Classe ActivityYearConstraint qui définie l'année de début (et de fin dans le cas d'une période custom)
- * par rapport aux contraintes temporelles choisies par un utilisateur.
- */
- class ActivityYearConstraint extends AbstractTimeConstraintUtils implements TimeConstraintInterface
- {
- public function __construct(
- private EntityManagerInterface $entityManager,
- private OrganizationUtils $organizationUtils
- ) {
- }
- /**
- * Main method.
- *
- * @return array<string, array<string, list<int>>>
- *
- * @throws \Exception
- */
- public function invoke(int $accessId): array
- {
- $access = $this->entityManager->getRepository(Access::class)->find($accessId);
- $historical = $access->getHistorical();
- $contraints = [
- self::START_KEY => [],
- self::END_KEY => [],
- ];
- if ($this->hasCustomPeriods($historical)) {
- $periods = $this->getRangeYear($access, $historical['dateStart'], $historical['dateEnd']);
- $contraints = $this->addConstraint($contraints, $this->customConstraint($periods));
- } else {
- $year = $access->getActivityYear();
- if ($historical['present']) {
- $contraints = $this->addConstraint($contraints, $this->presentConstraint($year));
- }
- if ($historical['past']) {
- $contraints = $this->addConstraint($contraints, $this->pastConstraint($year));
- }
- if ($historical['future']) {
- $contraints = $this->addConstraint($contraints, $this->futureConstraint($year));
- }
- }
- return $this->cleanConstraints($contraints);
- }
- /**
- * Retourne le tableau des années comprises dans la période custom.
- *
- * @return int[]
- */
- protected function getRangeYear(Access $access, string $dateStart, string $dateEnd): array
- {
- $organization = $access->getOrganization();
- return [
- OrganizationUtils::START_DATE_KEY => $this->organizationUtils->getActivityYearSwitchDate($organization, new \DateTime($dateStart)),
- OrganizationUtils::END_DATE_KEY => $this->organizationUtils->getActivityYearSwitchDate($organization, new \DateTime($dateEnd)),
- ];
- }
- /**
- * Une période est dans le présent si :
- * - l'année de début est égale (=) à l'année à afficher
- *
- * @return array<string, list<int>>
- */
- protected function presentConstraint(int $year): array
- {
- return [
- self::START_KEY => [
- $year => self::EQUAL,
- ],
- ];
- }
- /**
- * Une période est dans le passée si :
- * - l'année de début est plus petite (<) à l'année à afficher
- *
- * @return array<string, list<int>>
- */
- protected function pastConstraint(int $year): array
- {
- return [
- self::END_KEY => [
- $year => self::INF,
- ],
- ];
- }
- /**
- * Une période est dans le futur si :
- * - l'année de début est plus grande (>) à l'année à afficher
- *
- * @return array<string, list<int>>
- */
- protected function futureConstraint(int $year): array
- {
- return [
- self::START_KEY => [
- $year => self::SUP,
- ],
- ];
- }
- /**
- * Une période est dans une contrainte custom si :
- *
- * - l'année de début est plus grande ou égale (>=) à l'année de départ
- * - l'année de début est plus petite ou égale (<=) à l'année de fin
- *
- * @param array<int> $years
- *
- * @return array<string, list<int>>
- */
- protected function customConstraint(array $years): array
- {
- return [
- self::START_KEY => [
- $years[OrganizationUtils::START_DATE_KEY] => self::SUP + self::EQUAL,
- $years[OrganizationUtils::END_DATE_KEY] => self::INF + self::EQUAL,
- ],
- ];
- }
- }
|