src/App/Controller/CRUD/DetailsController.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Controller\CRUD;
  3. use App\Repository\AbstractRepository;
  4. use App\Response\ActionResult;
  5. use App\Response\JsonTextResponse;
  6. use JMS\Serializer\SerializationContext;
  7. use JMS\Serializer\SerializerInterface;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  11. /**
  12.  * @template TEntityClass of object
  13.  * @template-extends EntityController<TEntityClass>
  14.  */
  15. abstract class DetailsController extends EntityController
  16. {
  17.     /**
  18.      * @var class-string<TEntityClass>
  19.      */
  20.     private string $repository;
  21.     private string $template;
  22.     protected SerializerInterface $serializer;
  23.     protected bool $serializeNull false;
  24.     /**
  25.      * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessDocComment
  26.      * @param class-string<TEntityClass> $repository FQCN for entity class
  27.      */
  28.     public function __construct(string $repositorystring $template)
  29.     {
  30.         $this->repository $repository;
  31.         $this->template $template;
  32.     }
  33.     /**
  34.      * @phpstan-param TEntityClass $entity
  35.      * @return string path to template
  36.      */
  37.     protected function getTemplate($entity): string
  38.     {
  39.         return $this->template;
  40.     }
  41.     /**
  42.      * @return AbstractRepository<TEntityClass> Repository for related entity
  43.      */
  44.     protected function getRepository(): AbstractRepository
  45.     {
  46.         /** @var AbstractRepository<TEntityClass> $repository */
  47.         $repository $this->doctrine->getRepository($this->repository);
  48.         return $repository;
  49.     }
  50.     /**
  51.      * @return ActionResult|JsonTextResponse|Response
  52.      */
  53.     public function handleRequest(Request $request)
  54.     {
  55.         $result $this->execute($this->getFilters($request));
  56.         if ($this->isHandlingJson($request) && $this->isAjaxRequest($request)) {
  57.             $entity $result->getParams()['entity'];
  58.             $data = [
  59.                 'entity' => $entity,
  60.             ];
  61.             $context $this->configureSerializationContext(new SerializationContext());
  62.             return new JsonTextResponse(
  63.                 function ($data) use ($context): string {
  64.                     return $this->serializer->serialize($data'json'$context);
  65.                 },
  66.                 $data
  67.             );
  68.         }
  69.         return $result;
  70.     }
  71.     /**
  72.      * @phpstan-return TEntityClass
  73.      * @param array<string, array<mixed>|bool|int|float|object|string|null> $filters
  74.      * @return mixed
  75.      */
  76.     public function provideEntity(array $filters)
  77.     {
  78.         if (!isset($filters['id']) || !is_numeric($filters['id'])) {
  79.             throw $this->createNotFoundException('Id parameter is required.');
  80.         }
  81.         return $this->getEntity((int) $filters['id']);
  82.     }
  83.     /**
  84.      * @return array<string, array<mixed>|bool|int|float|object|string|null>
  85.      */
  86.     protected function getFilters(Request $request): array
  87.     {
  88.         $id $request->attributes->get('id');
  89.         if (null === $id) {
  90.             throw $this->createNotFoundException('Id parameter is required.');
  91.         }
  92.         if (!is_string($id) && !is_int($id)) {
  93.             throw new UnexpectedTypeException($id'string|int');
  94.         }
  95.         return ['id' => $id];
  96.     }
  97.     /**
  98.      * @param array<string, array<mixed>|bool|int|float|object|string|null> $filters
  99.      */
  100.     public function execute(array $filters = []): ActionResult
  101.     {
  102.         $entity $this->provideEntity($filters);
  103.         $parameters = ['entity' => $entity];
  104.         foreach ($this->templateParams as $key => $value) {
  105.             $parameters[$key] = $value;
  106.         }
  107.         return new ActionResult($this->getTemplate($entity), $parameters);
  108.     }
  109.     protected function configureSerializationContext(SerializationContext $serializationContext): SerializationContext
  110.     {
  111.         if ($this->serializeNull) {
  112.             $serializationContext->setSerializeNull(true);
  113.         }
  114.         return $serializationContext;
  115.     }
  116.     /**
  117.      * @required
  118.      */
  119.     public function setSerializer(SerializerInterface $serializer): void
  120.     {
  121.         $this->serializer $serializer;
  122.     }
  123. }