45 lines
1.5 KiB
PHP
45 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State\Bovin;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\Entity\BovineMovement;
|
|
use App\Repository\BovineMovementRepository;
|
|
use DateTimeImmutable;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
|
|
final class BovineMovementProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private readonly BovineMovementRepository $movementRepository,
|
|
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
|
private readonly ProcessorInterface $persistProcessor,
|
|
) {}
|
|
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
|
{
|
|
if (!$data instanceof BovineMovement) {
|
|
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
|
}
|
|
|
|
$enteredAt = $data->hasEnteredAt() ? $data->getEnteredAt() : new DateTimeImmutable();
|
|
$data->setEnteredAt($enteredAt);
|
|
$data->setLeftAt(null);
|
|
$data->setBuilding(null);
|
|
|
|
$bovine = $data->getBovine();
|
|
|
|
$openMovement = $this->movementRepository->findOpenMovement($bovine);
|
|
if (null !== $openMovement) {
|
|
$openMovement->setLeftAt($enteredAt);
|
|
}
|
|
|
|
$bovine->setBuildingCase($data->getBuildingCase());
|
|
|
|
return $this->persistProcessor->process($data, $operation, $uriVariables, $context);
|
|
}
|
|
}
|