Files
SIRH/src/State/EmployeeWriteProcessor.php
tristan 339d650b41
All checks were successful
Auto Tag Develop / tag (push) Successful in 8s
feat : ajout de la gestion des heures chauffeurs
2026-03-15 19:04:52 +01:00

140 lines
5.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\DeleteOperationInterface;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Contract;
use App\Entity\Employee;
use App\Enum\ContractNature;
use App\Repository\Contract\EmployeeContractPeriodReadRepositoryInterface;
use App\Service\Contracts\EmployeeContractChangeRequestFactory;
use App\Service\Contracts\EmployeeContractPeriodManagerInterface;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
final readonly class EmployeeWriteProcessor implements ProcessorInterface
{
public function __construct(
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
private ProcessorInterface $persistProcessor,
#[Autowire(service: 'api_platform.doctrine.orm.state.remove_processor')]
private ProcessorInterface $removeProcessor,
private EntityManagerInterface $entityManager,
private EmployeeContractPeriodReadRepositoryInterface $periodRepository,
private EmployeeContractChangeRequestFactory $changeRequestFactory,
private EmployeeContractPeriodManagerInterface $periodManager,
) {}
public function process(
mixed $data,
Operation $operation,
array $uriVariables = [],
array $context = []
): mixed {
if ($operation instanceof DeleteOperationInterface) {
return $this->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,
isDriver: $changeRequest->isDriver ?? false,
);
$data->setEntryDate($startDate);
$this->entityManager->flush();
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,
$changeRequest->contractComment
);
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,
isDriver: $changeRequest->isDriver ?? false,
);
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();
}
}