/**
  * @param Request $request
  * @param string $scope
  * @return bool
  */
 public function collect(Request $request, $scope)
 {
     if (!$this->paginationManager->isEnabled()) {
         return false;
     }
     $isDataCollected = false;
     $gridNames = array_keys($request->query->get('grid', []));
     foreach ($gridNames as $gridName) {
         // datagrid manager automatically extracts all required parameters from request
         $dataGrid = $this->datagridManager->getDatagridByRequestParams($gridName);
         if (!$this->paginationManager->isDatagridApplicable($dataGrid)) {
             continue;
         }
         $dataSource = $dataGrid->getDatasource();
         $dataGrid->getAcceptor()->acceptDatasource($dataSource);
         $entityName = $this->getEntityName($dataSource);
         $stateHash = $this->generateStateHash($dataGrid);
         // if entities are not in storage
         if (!$this->storage->hasData($entityName, $stateHash, $scope)) {
             $entitiesLimit = $this->getEntitiesLimit();
             $totalCount = $this->getTotalCount($dataSource, $scope);
             // if grid contains allowed number of entities
             if ($totalCount <= $entitiesLimit) {
                 // collect and set entity IDs
                 $entityIds = $this->getAllEntityIds($dataSource, $scope, $entitiesLimit);
                 $this->storage->setData($entityName, $stateHash, $entityIds, $scope);
             } else {
                 // set empty array as a sign that data is collected, but pagination itself must be disabled
                 $this->storage->setData($entityName, $stateHash, [], $scope);
             }
         }
         $isDataCollected = true;
     }
     return $isDataCollected;
 }
 /**
  * @param OrmResultAfter $event
  */
 public function onResultAfter(OrmResultAfter $event)
 {
     if (!$this->paginationManager->isEnabled()) {
         return;
     }
     $dataGrid = $event->getDatagrid();
     if (!$this->paginationManager->isDatagridApplicable($dataGrid)) {
         return;
     }
     // clear all data as long as we can't guarantee that storage data is valid
     $entityName = $this->getEntityName($dataGrid);
     $this->storage->clearData($entityName);
 }
 /**
  * @param bool $expected
  * @param bool $isOrmDatasource
  * @param bool|null $entityPagination
  * @dataProvider isDatagridApplicableDataProvider
  */
 public function testIsDatagridApplicable($expected, $isOrmDatasource, $entityPagination)
 {
     if ($isOrmDatasource) {
         $dataSource = $this->getMockBuilder('Oro\\Bundle\\DataGridBundle\\Datasource\\Orm\\OrmDatasource')->disableOriginalConstructor()->getMock();
     } else {
         $dataSource = $this->getMock('Oro\\Bundle\\DataGridBundle\\Datasource\\DatasourceInterface');
     }
     $config = ['options' => ['entity_pagination' => $entityPagination]];
     $configObject = DatagridConfiguration::create($config);
     $dataGrid = $this->getMock('Oro\\Bundle\\DataGridBundle\\Datagrid\\DatagridInterface');
     $dataGrid->expects($this->any())->method('getDatasource')->will($this->returnValue($dataSource));
     $dataGrid->expects($this->any())->method('getConfig')->will($this->returnValue($configObject));
     $this->assertSame($expected, $this->entityPaginationManager->isDatagridApplicable($dataGrid));
 }