periodValidator->assertPeriodDates($startDate, $endDate, $nature); $covered = $this->periodRepository->findOneCoveringDate($employee, $startDate); if (null !== $covered) { return; } $this->persistNewPeriod($employee, $contract, $startDate, $endDate, $nature); $this->entityManager->flush(); } public function closeCurrentPeriod( ?EmployeeContractPeriod $todayPeriod, DateTimeImmutable $requestedEndDate, bool $paidLeaveSettled, ?string $comment = null ): void { if (null === $todayPeriod) { throw new UnprocessableEntityHttpException('No active contract period to close.'); } $this->periodValidator->assertCloseEndDateCanBeApplied( $todayPeriod->getStartDate(), $todayPeriod->getEndDate(), $requestedEndDate, $todayPeriod->getContractNatureEnum() ); $todayPeriod->setEndDate($requestedEndDate); $todayPeriod->setPaidLeaveSettled($paidLeaveSettled); $todayPeriod->setComment($comment); $this->entityManager->flush(); } public function createNextPeriod( Employee $employee, Contract $contract, DateTimeImmutable $startDate, ?DateTimeImmutable $endDate, ContractNature $nature, ?EmployeeContractPeriod $todayPeriod ): void { $this->periodValidator->assertPeriodDates($startDate, $endDate, $nature); if (null !== $todayPeriod) { $this->periodValidator->assertNextStartDateCompatible($startDate, $todayPeriod); if (null === $todayPeriod->getEndDate()) { $todayPeriod->setEndDate($startDate->modify('-1 day')); } } $this->persistNewPeriod($employee, $contract, $startDate, $endDate, $nature); $this->entityManager->flush(); } private function persistNewPeriod( Employee $employee, Contract $contract, DateTimeImmutable $startDate, ?DateTimeImmutable $endDate, ContractNature $nature, ): void { $period = $this->periodBuilder->build($employee, $contract, $startDate, $endDate, $nature); $this->entityManager->persist($period); } }