Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataProvider.php
Go to the documentation of this file.
1 <?php
7 
23 use \Magento\Framework\Search\Request\IndexScopeResolverInterface;
24 
31 {
35  private $resource;
36 
40  private $range;
41 
45  private $customerSession;
46 
50  private $dataProvider;
51 
55  private $intervalFactory;
56 
60  private $connection;
61 
65  private $storeManager;
66 
70  private $priceTableResolver;
71 
75  private $dimensionFactory;
76 
87  public function __construct(
88  ResourceConnection $resource,
89  Range $range,
90  Session $customerSession,
91  MysqlDataProviderInterface $dataProvider,
92  IntervalFactory $intervalFactory,
93  StoreManager $storeManager = null,
94  IndexScopeResolverInterface $priceTableResolver = null,
95  DimensionFactory $dimensionFactory = null
96  ) {
97  $this->resource = $resource;
98  $this->connection = $resource->getConnection();
99  $this->range = $range;
100  $this->customerSession = $customerSession;
101  $this->dataProvider = $dataProvider;
102  $this->intervalFactory = $intervalFactory;
103  $this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(StoreManager::class);
104  $this->priceTableResolver = $priceTableResolver ?: ObjectManager::getInstance()->get(
105  IndexScopeResolverInterface::class
106  );
107  $this->dimensionFactory = $dimensionFactory ?: ObjectManager::getInstance()->get(DimensionFactory::class);
108  }
109 
113  public function getRange()
114  {
115  return $this->range->getPriceRange();
116  }
117 
121  public function getAggregations(\Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)
122  {
123  $aggregation = [
124  'count' => 'count(main_table.entity_id)',
125  'max' => 'MAX(min_price)',
126  'min' => 'MIN(min_price)',
127  'std' => 'STDDEV_SAMP(min_price)',
128  ];
129 
130  $select = $this->getSelect();
131  $websiteId = $this->storeManager->getStore()->getWebsiteId();
132  $customerGroupId = $this->customerSession->getCustomerGroupId();
133 
134  $tableName = $this->priceTableResolver->resolve(
135  'catalog_product_index_price',
136  [
137  $this->dimensionFactory->create(
139  (string)$websiteId
140  ),
141  $this->dimensionFactory->create(
143  (string)$customerGroupId
144  ),
145  ]
146  );
148  $table = $entityStorage->getSource();
149  $select->from(['main_table' => $tableName], [])
150  ->where('main_table.entity_id in (select entity_id from ' . $table->getName() . ')')
151  ->columns($aggregation);
152 
153  $select->where('customer_group_id = ?', $customerGroupId);
154  $select->where('main_table.website_id = ?', $websiteId);
155 
156  return $this->connection->fetchRow($select);
157  }
158 
162  public function getInterval(
163  BucketInterface $bucket,
164  array $dimensions,
165  \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
166  ) {
167  $select = $this->dataProvider->getDataSet($bucket, $dimensions, $entityStorage->getSource());
168 
169  return $this->intervalFactory->create(['select' => $select]);
170  }
171 
175  public function getAggregation(
176  BucketInterface $bucket,
177  array $dimensions,
178  $range,
179  \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage
180  ) {
181  $select = $this->dataProvider->getDataSet($bucket, $dimensions, $entityStorage->getSource());
182  $column = $select->getPart(Select::COLUMNS)[0];
183  $select->reset(Select::COLUMNS);
184  $rangeExpr = new \Zend_Db_Expr(
185  $this->connection->getIfNullSql(
186  $this->connection->quoteInto('FLOOR(' . $column[1] . ' / ? ) + 1', $range),
187  1
188  )
189  );
190 
191  $select
192  ->columns(['range' => $rangeExpr])
193  ->columns(['metrix' => 'COUNT(*)'])
194  ->group('range')
195  ->order('range');
196  $result = $this->connection->fetchPairs($select);
197 
198  return $result;
199  }
200 
204  public function prepareData($range, array $dbRanges)
205  {
206  $data = [];
207  if (!empty($dbRanges)) {
208  $lastIndex = array_keys($dbRanges);
209  $lastIndex = $lastIndex[count($lastIndex) - 1];
210 
211  foreach ($dbRanges as $index => $count) {
212  $fromPrice = $index == 1 ? '' : ($index - 1) * $range;
213  $toPrice = $index == $lastIndex ? '' : $index * $range;
214 
215  $data[] = [
216  'from' => $fromPrice,
217  'to' => $toPrice,
218  'count' => $count,
219  ];
220  }
221  }
222 
223  return $data;
224  }
225 
229  private function getSelect()
230  {
231  return $this->connection->select();
232  }
233 }
$tableName
Definition: trigger.php:13
$count
Definition: recent.phtml:13
getAggregation(BucketInterface $bucket, array $dimensions, $range, \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)
$resource
Definition: bulk.php:12
__construct(ResourceConnection $resource, Range $range, Session $customerSession, MysqlDataProviderInterface $dataProvider, IntervalFactory $intervalFactory, StoreManager $storeManager=null, IndexScopeResolverInterface $priceTableResolver=null, DimensionFactory $dimensionFactory=null)
const COLUMNS
Definition: Select.php:48
$table
Definition: trigger.php:14
$index
Definition: list.phtml:44
getInterval(BucketInterface $bucket, array $dimensions, \Magento\Framework\Search\Dynamic\EntityStorage $entityStorage)