src/App/Controller/CRUD/ActionController.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Controller\CRUD;
  3. use App\Response\ActionResult;
  4. use InvalidArgumentException;
  5. use RuntimeException;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. abstract class ActionController extends AbstractController
  12. {
  13.     /**
  14.      * @var array<string, string|int|float|bool|object|mixed[]|null>
  15.      */
  16.     protected array $templateParams = [];
  17.     /**
  18.      * @return ActionResult|Response
  19.      */
  20.     abstract public function handleRequest(Request $request);
  21.     /**
  22.      * Do whatever you want with response in this method, don't called for redirects
  23.      *
  24.      * @param ActionResult|Response|null $actionResponse
  25.      */
  26.     protected function handleResponse($actionResponse null, ?Response $response null): Response
  27.     {
  28.         if (null === $response) {
  29.             throw new RuntimeException('There is no response to handle');
  30.         }
  31.         return $response;
  32.     }
  33.     public function executeAction(Request $request): Response
  34.     {
  35.         $response $this->handleRequest($request);
  36.         if ($response instanceof Response) {
  37.             return $this->handleResponse(null$response);
  38.         }
  39.         foreach ($this->templateParams as $key => $value) {
  40.             $response->setParam($key$value);
  41.         }
  42.         if ($response->getRedirect() !== null) {
  43.             return $this->redirect($response->getRedirect());
  44.         }
  45.         $template $response->getTemplate();
  46.         if (null === $template) {
  47.             throw new InvalidArgumentException('Template must be set if you want to render something.');
  48.         }
  49.         return $this->handleResponse(
  50.             $response,
  51.             $this->render($template$response->getParams(), $response->getResponse())
  52.         );
  53.     }
  54.     /**
  55.      * @param string|int|float|bool|object|mixed[]|null $value
  56.      */
  57.     public function addTemplateParam(string $key$value): void
  58.     {
  59.         $this->templateParams[$key] = $value;
  60.     }
  61.     /**
  62.      * @throws AccessDeniedException
  63.      */
  64.     public function getAuthenticatedUser(): UserInterface
  65.     {
  66.         if (null === ($user $this->getUser())) {
  67.             throw $this->createAccessDeniedException();
  68.         }
  69.         return $user;
  70.     }
  71.     protected function isHandlingJson(Request $request): bool
  72.     {
  73.         return true;
  74.     }
  75.     protected function isAjaxRequest(Request $request): bool
  76.     {
  77.         $acceptHeader $request->headers->get('accept');
  78.         return $request->isXmlHttpRequest()
  79.             || (null !== $acceptHeader && strpos($acceptHeader'application/json') !== false);
  80.     }
  81. }