vendor/sonata-project/admin-bundle/src/Action/GetShortObjectDescriptionAction.php line 25

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\AdminBundle\Action;
  12. use Sonata\AdminBundle\Exception\BadRequestParamHttpException;
  13. use Sonata\AdminBundle\Request\AdminFetcherInterface;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use Twig\Environment;
  20. final class GetShortObjectDescriptionAction
  21. {
  22.     public function __construct(
  23.         private Environment $twig,
  24.         private AdminFetcherInterface $adminFetcher
  25.     ) {
  26.     }
  27.     /**
  28.      * @throws NotFoundHttpException
  29.      */
  30.     public function __invoke(Request $request): Response
  31.     {
  32.         try {
  33.             $admin $this->adminFetcher->get($request);
  34.         } catch (\InvalidArgumentException $e) {
  35.             throw new NotFoundHttpException($e->getMessage());
  36.         }
  37.         $objectId $request->get('objectId');
  38.         if (!\is_string($objectId) && !\is_int($objectId)) {
  39.             throw new BadRequestParamHttpException('objectId', ['string''int'], $objectId);
  40.         }
  41.         // If the subclass parameter is present it can cause conflict with other admin.
  42.         // The admin do not need subclass parameter to load an existing object.
  43.         $subclass $request->query->get('subclass');
  44.         $request->query->remove('subclass');
  45.         try {
  46.             $object $admin->getObject($objectId);
  47.             if (null === $object) {
  48.                 throw new NotFoundHttpException(sprintf('Could not find subject for id "%s"'$objectId));
  49.             }
  50.         } finally {
  51.             // Restore the subclass if present to reduce impact of the parameter removal above.
  52.             if (null !== $subclass) {
  53.                 $request->query->set('subclass'$subclass);
  54.             }
  55.         }
  56.         if ('json' === $request->get('_format')) {
  57.             return new JsonResponse(['result' => [
  58.                 'id' => $admin->id($object),
  59.                 'label' => $admin->toString($object),
  60.             ]]);
  61.         }
  62.         if ('html' === $request->get('_format')) {
  63.             $templateRegistry $admin->getTemplateRegistry();
  64.             return new Response($this->twig->render($templateRegistry->getTemplate('short_object_description'), [
  65.                 'admin' => $admin,
  66.                 'description' => $admin->toString($object),
  67.                 'object' => $object,
  68.                 'link_parameters' => $request->get('linkParameters', []),
  69.             ]));
  70.         }
  71.         throw new BadRequestHttpException('Invalid format');
  72.     }
  73. }