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'); $changeRequest = $this->changeRequestFactory->fromEmployee($data); if ($isNew) { $startDate = $changeRequest->contractStartDate ?? new DateTimeImmutable('1970-01-01'); $nature = $changeRequest->contractNature ?? ContractNature::CDI; $this->periodManager->ensureContractPeriodExists( employee: $data, contract: $currentContract, startDate: $startDate, endDate: $changeRequest->contractEndDate, nature: $nature ); return $result; } if ($this->isSameContract($previousContract, $currentContract) && !$changeRequest->hasPeriodChangeRequest()) { return $result; } $todayPeriod = $this->periodRepository->findOneCoveringDate($data, $today); $currentPeriodContract = $todayPeriod?->getContract(); $contractChanged = $currentPeriodContract instanceof Contract ? $currentPeriodContract->getId() !== $currentContract->getId() : true; $isCloseOnlyRequest = $changeRequest->isCloseOnlyRequest($contractChanged); if ($isCloseOnlyRequest) { $requestedEndDate = $changeRequest->contractEndDate; if (null === $requestedEndDate) { throw new UnprocessableEntityHttpException('contractEndDate is required for close-only request.'); } $this->periodManager->closeCurrentPeriod( $todayPeriod, $requestedEndDate, $changeRequest->contractPaidLeaveSettled ?? false ); return $result; } $startDate = $changeRequest->contractStartDate ?? $today; $nature = $changeRequest->contractNature ?? $todayPeriod?->getContractNatureEnum() ?? ContractNature::CDI; $this->periodManager->createNextPeriod( employee: $data, contract: $currentContract, startDate: $startDate, endDate: $changeRequest->contractEndDate, nature: $nature, todayPeriod: $todayPeriod ); 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(); } }