removeProcessor->process($data, $operation, $uriVariables, $context); } if (!$data instanceof Employee) { return $this->persistProcessor->process($data, $operation, $uriVariables, $context); } $isNew = null === $data->getId(); $previousContract = $this->resolvePreviousContract($data); $result = $this->persistProcessor->process($data, $operation, $uriVariables, $context); $currentContract = $data->getContract(); if (!$currentContract instanceof Contract) { return $result; } $today = new DateTimeImmutable('today'); if ($isNew) { $this->ensureContractPeriodExists($data, $currentContract, new DateTimeImmutable('1970-01-01')); return $result; } if ($this->isSameContract($previousContract, $currentContract)) { return $result; } $todayPeriod = $this->periodRepository->findOneCoveringDate($data, $today); if (null !== $todayPeriod && null === $todayPeriod->getEndDate() && $todayPeriod->getStartDate()->format('Y-m-d') === $today->format('Y-m-d')) { $todayPeriod->setContract($currentContract); $this->entityManager->flush(); return $result; } $this->periodRepository->closeOpenPeriods($data, $today->modify('-1 day')); $this->createPeriod($data, $currentContract, $today); $this->entityManager->flush(); return $result; } private function resolvePreviousContract(Employee $employee): ?Contract { if (null === $employee->getId()) { return null; } $originalData = $this->entityManager->getUnitOfWork()->getOriginalEntityData($employee); $original = $originalData['contract'] ?? null; return $original instanceof Contract ? $original : null; } private function isSameContract(?Contract $first, ?Contract $second): bool { if (null === $first || null === $second) { return $first === $second; } return $first->getId() === $second->getId(); } private function ensureContractPeriodExists(Employee $employee, Contract $contract, DateTimeImmutable $startDate): void { $covered = $this->periodRepository->findOneCoveringDate($employee, $startDate); if (null !== $covered) { return; } $this->createPeriod($employee, $contract, $startDate); $this->entityManager->flush(); } private function createPeriod(Employee $employee, Contract $contract, DateTimeImmutable $startDate): void { $period = new EmployeeContractPeriod() ->setEmployee($employee) ->setContract($contract) ->setStartDate($startDate) ->setEndDate(null) ; $this->entityManager->persist($period); } }