<?php
namespace App\V4\Controller\Section;
use App\V4\Service\Section\SectionFormHandler;
use Doctrine\Common\Annotations\AnnotationException;
use Psr\Cache\InvalidArgumentException;
use ReflectionException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class SectionDataPersisterAction
{
/**
* @var SectionFormHandler
*/
private $sectionFormHandler;
/**
* @param SectionFormHandler $sectionFormHandler
*/
public function __construct(SectionFormHandler $sectionFormHandler)
{
$this->sectionFormHandler = $sectionFormHandler;
}
/**
* @param Request $request
* @param null $data
* @param string|null $id
*
* @return object|JsonResponse|null
*
* @throws AnnotationException
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws ExceptionInterface
* @throws InvalidArgumentException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws ReflectionException
*/
public function __invoke(Request $request, $data = null, ?string $id = null)
{
//@todo refacto probablement sécurité a rajouter
if (Request::METHOD_DELETE === $request->getMethod() && null !== $data) {
$this->sectionFormHandler->remove($data);
return new JsonResponse([]);
}
return new JsonResponse($this->sectionFormHandler->handleForm(json_decode($request->getContent(), true), $id));
}
}