vendor/pimcore/pimcore/bundles/CoreBundle/Request/ParamConverter/DataObjectParamConverter.php line 36

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\CoreBundle\Request\ParamConverter;
  15. use Pimcore\Model\DataObject\AbstractObject;
  16. use Pimcore\Model\DataObject\Concrete;
  17. use Pimcore\Tool;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  19. use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  22. /**
  23.  * @internal
  24.  */
  25. class DataObjectParamConverter implements ParamConverterInterface
  26. {
  27.     /**
  28.      * {@inheritdoc}
  29.      *
  30.      * @throws NotFoundHttpException When invalid data object ID given
  31.      */
  32.     public function apply(Request $requestParamConverter $configuration): bool
  33.     {
  34.         $param $configuration->getName();
  35.         if (!$request->attributes->has($param)) {
  36.             return false;
  37.         }
  38.         $value $request->attributes->get($param);
  39.         if (!$value && $configuration->isOptional()) {
  40.             $request->attributes->set($paramnull);
  41.             return true;
  42.         }
  43.         $class $configuration->getClass();
  44.         $options $configuration->getOptions();
  45.         /** @var Concrete|null $object */
  46.         $object $class::getById($value);
  47.         if (!$object) {
  48.             throw new NotFoundHttpException(sprintf('Invalid data object ID given for parameter "%s".'$param));
  49.         } elseif (!$object->isPublished() && !Tool::isElementRequestByAdmin($request$object) && (!array_key_exists('unpublished'$options) || !$options['unpublished'])) {
  50.             throw new NotFoundHttpException(sprintf('Data object for parameter "%s" is not published.'$param));
  51.         }
  52.         $request->attributes->set($param$object);
  53.         return true;
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      *
  58.      */
  59.     public function supports(ParamConverter $configuration): bool
  60.     {
  61.         if (null === $configuration->getClass()) {
  62.             return false;
  63.         }
  64.         return is_subclass_of($configuration->getClass(), AbstractObject::class);
  65.     }
  66. }