vendor/sulu/sulu/src/Sulu/Component/SmartContent/Orm/BaseDataProvider.php line 234

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\SmartContent\Orm;
  11. use JMS\Serializer\SerializationContext;
  12. use Sulu\Bundle\WebsiteBundle\ReferenceStore\ReferenceStoreInterface;
  13. use Sulu\Component\Content\Compat\PropertyParameter;
  14. use Sulu\Component\Security\Authorization\PermissionTypes;
  15. use Sulu\Component\Serializer\ArraySerializerInterface;
  16. use Sulu\Component\SmartContent\ArrayAccessItem;
  17. use Sulu\Component\SmartContent\Configuration\Builder;
  18. use Sulu\Component\SmartContent\Configuration\BuilderInterface;
  19. use Sulu\Component\SmartContent\Configuration\ProviderConfiguration;
  20. use Sulu\Component\SmartContent\Configuration\ProviderConfigurationInterface;
  21. use Sulu\Component\SmartContent\DataProviderInterface;
  22. use Sulu\Component\SmartContent\DataProviderResult;
  23. use Sulu\Component\SmartContent\ItemInterface;
  24. use Sulu\Component\Webspace\Analyzer\RequestAnalyzerInterface;
  25. use Symfony\Component\Security\Core\Security;
  26. /**
  27.  * Provides basic functionality for contact and account providers.
  28.  */
  29. abstract class BaseDataProvider implements DataProviderInterface
  30. {
  31.     /**
  32.      * Creates a new configuration object.
  33.      *
  34.      * @return BuilderInterface
  35.      */
  36.     protected static function createConfigurationBuilder()
  37.     {
  38.         return Builder::create();
  39.     }
  40.     /**
  41.      * @var DataProviderRepositoryInterface
  42.      */
  43.     protected $repository;
  44.     /**
  45.      * @var ProviderConfigurationInterface
  46.      */
  47.     protected $configuration;
  48.     /**
  49.      * @var ArraySerializerInterface
  50.      */
  51.     private $serializer;
  52.     /**
  53.      * @var ReferenceStoreInterface
  54.      */
  55.     private $referenceStore;
  56.     /**
  57.      * @var ?Security
  58.      */
  59.     private $security;
  60.     /**
  61.      * @var ?RequestAnalyzerInterface
  62.      */
  63.     private $requestAnalyzer;
  64.     /**
  65.      * @var ?array
  66.      */
  67.     private $permissions;
  68.     public function __construct(
  69.         DataProviderRepositoryInterface $repository,
  70.         ArraySerializerInterface $serializer,
  71.         ReferenceStoreInterface $referenceStore null,
  72.         Security $security null,
  73.         RequestAnalyzerInterface $requestAnalyzer null,
  74.         $permissions null
  75.     ) {
  76.         $this->repository $repository;
  77.         $this->serializer $serializer;
  78.         $this->referenceStore $referenceStore;
  79.         $this->security $security;
  80.         $this->requestAnalyzer $requestAnalyzer;
  81.         $this->permissions $permissions;
  82.     }
  83.     public function getDefaultPropertyParameter()
  84.     {
  85.         return [];
  86.     }
  87.     public function getConfiguration()
  88.     {
  89.         return $this->configuration;
  90.     }
  91.     public function resolveDatasource($datasource, array $propertyParameter, array $options)
  92.     {
  93.         return;
  94.     }
  95.     public function resolveDataItems(
  96.         array $filters,
  97.         array $propertyParameter,
  98.         array $options = [],
  99.         $limit null,
  100.         $page 1,
  101.         $pageSize null
  102.     ) {
  103.         list($result$hasNextPage) = $this->resolveFilters(
  104.             $filters,
  105.             $options['locale'],
  106.             $limit,
  107.             $page,
  108.             $pageSize,
  109.             $this->getOptions($propertyParameter$options)
  110.         );
  111.         return new DataProviderResult($this->decorateDataItems($result), $hasNextPage);
  112.     }
  113.     public function resolveResourceItems(
  114.         array $filters,
  115.         array $propertyParameter,
  116.         array $options = [],
  117.         $limit null,
  118.         $page 1,
  119.         $pageSize null
  120.     ) {
  121.         list($result$hasNextPage) = $this->resolveFilters(
  122.             $filters,
  123.             $options['locale'],
  124.             $limit,
  125.             $page,
  126.             $pageSize,
  127.             $this->getOptions($propertyParameter$options)
  128.         );
  129.         return new DataProviderResult($this->decorateResourceItems($result$options['locale']), $hasNextPage);
  130.     }
  131.     /**
  132.      * Resolves filters.
  133.      *
  134.      * @param string $locale
  135.      * @param int|null $limit
  136.      * @param int $page
  137.      * @param int|null $pageSize
  138.      * @param array $options
  139.      *
  140.      * @return array
  141.      */
  142.     private function resolveFilters(
  143.         array $filters,
  144.         $locale,
  145.         $limit null,
  146.         $page 1,
  147.         $pageSize null,
  148.         $options = []
  149.     ) {
  150.         $webspace $this->requestAnalyzer $this->requestAnalyzer->getWebspace() : null;
  151.         $result $this->repository->findByFilters(
  152.             $filters,
  153.             $page,
  154.             $pageSize,
  155.             $limit,
  156.             $locale,
  157.             $options,
  158.             $this->security && $webspace && $webspace->hasWebsiteSecurity() ? $this->security->getUser() : null,
  159.             $webspace && $webspace->hasWebsiteSecurity() && $this->permissions
  160.                 $this->permissions[PermissionTypes::VIEW]
  161.                 : null
  162.         );
  163.         $hasNextPage false;
  164.         if (null !== $pageSize && \count($result) > $pageSize) {
  165.             $hasNextPage true;
  166.             $result \array_splice($result0$pageSize);
  167.         }
  168.         return [$result$hasNextPage];
  169.     }
  170.     /**
  171.      * Initiate configuration.
  172.      *
  173.      * @return ProviderConfigurationInterface
  174.      *
  175.      * @deprecated use self::createConfigurationBuilder instead
  176.      */
  177.     protected function initConfiguration($tags$categories$limit$presentAs$paginated$sorting)
  178.     {
  179.         $configuration = new ProviderConfiguration();
  180.         $configuration->setTags($tags);
  181.         $configuration->setCategories($categories);
  182.         $configuration->setLimit($limit);
  183.         $configuration->setPresentAs($presentAs);
  184.         $configuration->setPaginated($paginated);
  185.         $configuration->setSorting($sorting);
  186.         return $configuration;
  187.     }
  188.     /**
  189.      * Decorates result as resource item.
  190.      *
  191.      * @param string $locale
  192.      *
  193.      * @return ArrayAccessItem[]
  194.      */
  195.     protected function decorateResourceItems(array $data$locale)
  196.     {
  197.         return \array_map(
  198.             function($item) {
  199.                 $itemData $this->serializer->serialize($item$this->getSerializationContext());
  200.                 $id $this->getIdForItem($item);
  201.                 if ($this->referenceStore) {
  202.                     $this->referenceStore->add($id);
  203.                 }
  204.                 return new ArrayAccessItem($id$itemData$item);
  205.             },
  206.             $data
  207.         );
  208.     }
  209.     /**
  210.      * Returns id for given entity.
  211.      *
  212.      * @param object $entity
  213.      *
  214.      * @return int
  215.      */
  216.     protected function getIdForItem($entity)
  217.     {
  218.         return $entity->getId();
  219.     }
  220.     /**
  221.      * Creates serialization context. Can be used to add own groups.
  222.      *
  223.      * @return SerializationContext
  224.      */
  225.     protected function getSerializationContext()
  226.     {
  227.         return SerializationContext::create()->setSerializeNull(true);
  228.     }
  229.     /**
  230.      * Returns additional options for query creation.
  231.      *
  232.      * @param PropertyParameter[] $propertyParameter
  233.      *
  234.      * @return array
  235.      */
  236.     protected function getOptions(
  237.         array $propertyParameter,
  238.         array $options = []
  239.     ) {
  240.         return [];
  241.     }
  242.     /**
  243.      * Decorates result as data item.
  244.      *
  245.      * @return ItemInterface[]
  246.      */
  247.     abstract protected function decorateDataItems(array $data);
  248. }