<?php
namespace App\V4\Controller\Tab;
use App\Service\Cache\Exception\UnableToSaveKeyException;
use App\V4\Service\Tab\TabFormHandler;
use Doctrine\Common\Annotations\AnnotationException;
use Psr\Cache\CacheException;
use Psr\Cache\InvalidArgumentException;
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 TabDataPersisterAction
{
/**
* @var TabFormHandler
*/
private $tabFormHandler;
/**
* @param TabFormHandler $tabFormHandler
*/
public function __construct(TabFormHandler $tabFormHandler)
{
$this->tabFormHandler = $tabFormHandler;
}
/**
* @param Request $request
* @param null $data
* @param string|null $id
*
* @return object|JsonResponse|null
*
* @throws CacheException
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws ExceptionInterface
* @throws InvalidArgumentException
* @throws RedirectionExceptionInterface
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
* @throws UnableToSaveKeyException
* @throws AnnotationException
* @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->tabFormHandler->remove($data);
return new JsonResponse([]);
}
return new JsonResponse($this->tabFormHandler->handleForm(json_decode($request->getContent(), true), $id));
}
}