src/V4/Controller/CustomAction/CustomActionDataPersisterAction.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\V4\Controller\CustomAction;
  3. use App\V4\Entity\CustomAction;
  4. use App\V4\Service\CustomAction\CustomActionFormHandler;
  5. use App\V4\Voters\CustomActionVoter;
  6. use Psr\Cache\InvalidArgumentException;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. class CustomActionDataPersisterAction extends AbstractController
  11. {
  12.     /**
  13.      * @var CustomActionFormHandler
  14.      */
  15.     private $customActionFormHandler;
  16.     /**
  17.      * @param CustomActionFormHandler $customActionFormHandler
  18.      */
  19.     public function __construct(CustomActionFormHandler $customActionFormHandler)
  20.     {
  21.         $this->customActionFormHandler $customActionFormHandler;
  22.     }
  23.     /**
  24.      * @param Request           $request
  25.      * @param CustomAction|null $data
  26.      *
  27.      * @return JsonResponse
  28.      *
  29.      * @throws InvalidArgumentException
  30.      */
  31.     public function __invoke(Request $request, ?CustomAction $data null): JsonResponse
  32.     {
  33.         $this->denyAccessUnlessGranted(CustomActionVoter::CUSTOM_ACTION_ADD_EDIT);
  34.         if (Request::METHOD_DELETE === $request->getMethod() && $data instanceof CustomAction) {
  35.             $this->customActionFormHandler->remove($data);
  36.             return new JsonResponse([]);
  37.         }
  38.         return new JsonResponse(
  39.             $this->customActionFormHandler->handleForm(json_decode($request->getContent(), true), $data)
  40.         );
  41.     }
  42. }