<?php
namespace App\V4\Controller\CustomAction;
use App\V4\Entity\CustomAction;
use App\V4\Service\CustomAction\CustomActionFormHandler;
use App\V4\Voters\CustomActionVoter;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class CustomActionDataPersisterAction extends AbstractController
{
/**
* @var CustomActionFormHandler
*/
private $customActionFormHandler;
/**
* @param CustomActionFormHandler $customActionFormHandler
*/
public function __construct(CustomActionFormHandler $customActionFormHandler)
{
$this->customActionFormHandler = $customActionFormHandler;
}
/**
* @param Request $request
* @param CustomAction|null $data
*
* @return JsonResponse
*
* @throws InvalidArgumentException
*/
public function __invoke(Request $request, ?CustomAction $data = null): JsonResponse
{
$this->denyAccessUnlessGranted(CustomActionVoter::CUSTOM_ACTION_ADD_EDIT);
if (Request::METHOD_DELETE === $request->getMethod() && $data instanceof CustomAction) {
$this->customActionFormHandler->remove($data);
return new JsonResponse([]);
}
return new JsonResponse(
$this->customActionFormHandler->handleForm(json_decode($request->getContent(), true), $data)
);
}
}