Commit 5b06db89 authored by Thibaut Vallee's avatar Thibaut Vallee
Browse files

Restaurer manquants

parent 6f9901f8
Loading
Loading
Loading
Loading
+42 −0
Changes for src/State/RestaurerProcessor.php: 42 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Unicaen\Utilisateur\State;

use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use Unicaen\Utilisateur\Model\HistoriqueAwareInterface;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;

class RestaurerProcessor implements ProcessorInterface
{
    use HistoUserTrait;

    public function __construct(
        private Security $security,
        private EntityManagerInterface $em,
        private ClockInterface $clock,
        private ContainerBagInterface $param
    ) {
    }

    /**
     * @throws \Exception
     */
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
    {
        if(!$data instanceof HistoriqueAwareInterface){
            throw new Exception("L'entité doit implémenter HistoriqueAwareInterface pour permettre l'historisation");
        }
        $user = $this->getHistoUser();
        $data->setHistoModificateur($user);
        $data->setHistoModification($this->clock->now());
        $data->dehistoriser();
        $this->em->persist($data);
        $this->em->flush();
        return $data;
    }
}