122 lines
4.0 KiB
PHP
122 lines
4.0 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\Entity\EmployeeContractPeriod;
|
|
use App\Repository\EmployeeContractPeriodRepository;
|
|
use DateTimeImmutable;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
|
|
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 EmployeeContractPeriodRepository $periodRepository,
|
|
) {}
|
|
|
|
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');
|
|
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);
|
|
}
|
|
}
|