Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Interval.php
Go to the documentation of this file.
1 <?php
7 
14 
15 class Interval implements IntervalInterface
16 {
20  const DELTA = 0.005;
21 
25  protected $connectionManager;
26 
30  protected $fieldMapper;
31 
35  protected $clientConfig;
36 
40  private $fieldName;
41 
45  private $storeId;
46 
50  private $entityIds;
51 
55  private $searchIndexNameResolver;
56 
66  public function __construct(
70  SearchIndexNameResolver $searchIndexNameResolver,
71  $fieldName,
72  $storeId,
73  $entityIds
74  ) {
75  $this->connectionManager = $connectionManager;
76  $this->fieldMapper = $fieldMapper;
77  $this->clientConfig = $clientConfig;
78  $this->fieldName = $fieldName;
79  $this->storeId = $storeId;
80  $this->entityIds = $entityIds;
81  $this->searchIndexNameResolver = $searchIndexNameResolver;
82  }
83 
87  public function load($limit, $offset = null, $lower = null, $upper = null)
88  {
89  $from = $to = [];
90  if ($lower) {
91  $from = ['gte' => $lower - self::DELTA];
92  }
93  if ($upper) {
94  $to = ['lt' => $upper - self::DELTA];
95  }
96 
97  $requestQuery = [
98  'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
99  'type' => $this->clientConfig->getEntityType(),
100  'body' => [
101  'fields' => [
102  '_id',
103  $this->fieldName,
104  ],
105  'query' => [
106  'filtered' => [
107  'query' => [
108  'match_all' => [],
109  ],
110  'filter' => [
111  'bool' => [
112  'must' => [
113  [
114  'terms' => [
115  '_id' => $this->entityIds,
116  ],
117  ],
118  [
119  'range' => [
120  $this->fieldName => array_merge($from, $to),
121  ],
122  ],
123  ],
124  ],
125  ],
126  ],
127  ],
128  'sort' => [
129  $this->fieldName,
130  ],
131  'size' => $limit,
132  ],
133  ];
134  if ($offset) {
135  $requestQuery['body']['from'] = $offset;
136  }
137  $queryResult = $this->connectionManager->getConnection()
138  ->query($requestQuery);
139 
140  return $this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName);
141  }
142 
146  public function loadPrevious($data, $index, $lower = null)
147  {
148  if ($lower) {
149  $from = ['gte' => $lower - self::DELTA];
150  }
151  if ($data) {
152  $to = ['lt' => $data - self::DELTA];
153  }
154 
155  $requestQuery = [
156  'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
157  'type' => $this->clientConfig->getEntityType(),
158  'search_type' => 'count',
159  'body' => [
160  'fields' => [
161  '_id'
162  ],
163  'query' => [
164  'filtered' => [
165  'query' => [
166  'match_all' => [],
167  ],
168  'filter' => [
169  'bool' => [
170  'must' => [
171  [
172  'terms' => [
173  '_id' => $this->entityIds,
174  ],
175  ],
176  [
177  'range' => [
178  $this->fieldName => array_merge($from, $to),
179  ],
180  ],
181  ],
182  ],
183  ],
184  ],
185  ],
186  'sort' => [
187  $this->fieldName,
188  ],
189  ],
190  ];
191  $queryResult = $this->connectionManager->getConnection()
192  ->query($requestQuery);
193 
194  $offset = $queryResult['hits']['total'];
195  if (!$offset) {
196  return false;
197  }
198 
199  return $this->load($index - $offset + 1, $offset - 1, $lower);
200  }
201 
205  public function loadNext($data, $rightIndex, $upper = null)
206  {
207  $from = ['gt' => $data + self::DELTA];
208  $to = ['lt' => $data - self::DELTA];
209 
210  $requestCountQuery = [
211  'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
212  'type' => $this->clientConfig->getEntityType(),
213  'search_type' => 'count',
214  'body' => [
215  'fields' => [
216  '_id'
217  ],
218  'query' => [
219  'filtered' => [
220  'query' => [
221  'match_all' => [],
222  ],
223  'filter' => [
224  'bool' => [
225  'must' => [
226  [
227  'terms' => [
228  '_id' => $this->entityIds,
229  ],
230  ],
231  [
232  'range' => [
233  $this->fieldName => array_merge($from, $to),
234  ],
235  ],
236  ],
237  ],
238  ],
239  ],
240  ],
241  'sort' => [
242  $this->fieldName,
243  ],
244  ],
245  ];
246  $queryCountResult = $this->connectionManager->getConnection()
247  ->query($requestCountQuery);
248 
249  $offset = $queryCountResult['hits']['total'];
250  if (!$offset) {
251  return false;
252  }
253 
254  $from = ['gte' => $data - self::DELTA];
255  if ($upper !== null) {
256  $to = ['lt' => $data - self::DELTA];
257  }
258 
259  $requestQuery = $requestCountQuery;
260  $requestCountQuery['body']['query']['filtered']['filter']['bool']['must']['range'] =
261  [$this->fieldName => array_merge($from, $to)];
262 
263  $requestCountQuery['body']['from'] = $offset - 1;
264  $requestCountQuery['body']['size'] = $rightIndex - $offset + 1;
265 
266  $queryResult = $this->connectionManager->getConnection()
267  ->query($requestQuery);
268 
269  return array_reverse($this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName));
270  }
271 
278  private function arrayValuesToFloat($hits, $fieldName)
279  {
280  $returnPrices = [];
281  foreach ($hits as $hit) {
282  $returnPrices[] = (float) $hit['fields'][$fieldName][0];
283  }
284 
285  return $returnPrices;
286  }
287 }
load($limit, $offset=null, $lower=null, $upper=null)
Definition: Interval.php:87
$index
Definition: list.phtml:44
__construct(ConnectionManager $connectionManager, FieldMapperInterface $fieldMapper, Config $clientConfig, SearchIndexNameResolver $searchIndexNameResolver, $fieldName, $storeId, $entityIds)
Definition: Interval.php:66