Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Interval.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
16 
20 class Interval implements IntervalInterface
21 {
25  const DELTA = 0.005;
26 
30  private $connectionManager;
31 
35  private $fieldMapper;
36 
40  private $clientConfig;
41 
45  private $fieldName;
46 
50  private $storeId;
51 
55  private $entityIds;
56 
60  private $searchIndexNameResolver;
61 
71  public function __construct(
72  ConnectionManager $connectionManager,
73  FieldMapperInterface $fieldMapper,
74  Config $clientConfig,
75  SearchIndexNameResolver $searchIndexNameResolver,
76  string $fieldName,
77  string $storeId,
78  array $entityIds
79  ) {
80  $this->connectionManager = $connectionManager;
81  $this->fieldMapper = $fieldMapper;
82  $this->clientConfig = $clientConfig;
83  $this->fieldName = $fieldName;
84  $this->storeId = $storeId;
85  $this->entityIds = $entityIds;
86  $this->searchIndexNameResolver = $searchIndexNameResolver;
87  }
88 
92  public function load($limit, $offset = null, $lower = null, $upper = null)
93  {
94  $from = $to = [];
95  if ($lower) {
96  $from = ['gte' => $lower - self::DELTA];
97  }
98  if ($upper) {
99  $to = ['lt' => $upper - self::DELTA];
100  }
101 
102  $requestQuery = $this->prepareBaseRequestQuery($from, $to);
103  $requestQuery = array_merge_recursive(
104  $requestQuery,
105  ['body' => ['stored_fields' => [$this->fieldName], 'size' => $limit]]
106  );
107 
108  if ($offset) {
109  $requestQuery['body']['from'] = $offset;
110  }
111 
112  $queryResult = $this->connectionManager->getConnection()
113  ->query($requestQuery);
114 
115  return $this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName);
116  }
117 
121  public function loadPrevious($data, $index, $lower = null)
122  {
123  if ($lower) {
124  $from = ['gte' => $lower - self::DELTA];
125  }
126  if ($data) {
127  $to = ['lt' => $data - self::DELTA];
128  }
129 
130  $requestQuery = $this->prepareBaseRequestQuery($from, $to);
131  $requestQuery = array_merge_recursive(
132  $requestQuery,
133  ['size' => 0]
134  );
135 
136  $queryResult = $this->connectionManager->getConnection()
137  ->query($requestQuery);
138 
139  $offset = $queryResult['hits']['total'];
140  if (!$offset) {
141  return false;
142  }
143 
144  return $this->load($index - $offset + 1, $offset - 1, $lower);
145  }
146 
150  public function loadNext($data, $rightIndex, $upper = null)
151  {
152  $from = ['gt' => $data + self::DELTA];
153  $to = ['lt' => $data - self::DELTA];
154 
155  $requestCountQuery = $this->prepareBaseRequestQuery($from, $to);
156  $requestCountQuery = array_merge_recursive(
157  $requestCountQuery,
158  ['size' => 0]
159  );
160 
161  $queryCountResult = $this->connectionManager->getConnection()
162  ->query($requestCountQuery);
163 
164  $offset = $queryCountResult['hits']['total'];
165  if (!$offset) {
166  return false;
167  }
168 
169  $from = ['gte' => $data - self::DELTA];
170  if ($upper !== null) {
171  $to = ['lt' => $data - self::DELTA];
172  }
173 
174  $requestQuery = $requestCountQuery;
175 
176  $requestCountQuery['body']['query']['bool']['filter']['bool']['must']['range'] =
177  [$this->fieldName => array_merge($from, $to)];
178  $requestCountQuery['body']['from'] = $offset - 1;
179  $requestCountQuery['body']['size'] = $rightIndex - $offset + 1;
180  $queryResult = $this->connectionManager->getConnection()
181  ->query($requestQuery);
182 
183  return array_reverse($this->arrayValuesToFloat($queryResult['hits']['hits'], $this->fieldName));
184  }
185 
194  private function arrayValuesToFloat(array $hits, string $fieldName): array
195  {
196  $returnPrices = [];
197  foreach ($hits as $hit) {
198  $returnPrices[] = (float)$hit['fields'][$fieldName][0];
199  }
200 
201  return $returnPrices;
202  }
203 
211  private function prepareBaseRequestQuery($from = null, $to = null): array
212  {
213  $requestQuery = [
214  'index' => $this->searchIndexNameResolver->getIndexName($this->storeId, Fulltext::INDEXER_ID),
215  'type' => $this->clientConfig->getEntityType(),
216  'body' => [
217  'stored_fields' => [
218  '_id',
219  ],
220  'query' => [
221  'bool' => [
222  'must' => [
223  'match_all' => new \stdClass(),
224  ],
225  'filter' => [
226  'bool' => [
227  'must' => [
228  [
229  'terms' => [
230  '_id' => $this->entityIds,
231  ],
232  ],
233  [
234  'range' => [
235  $this->fieldName => array_merge($from, $to),
236  ],
237  ],
238  ],
239  ],
240  ],
241  ],
242  ],
243  'sort' => [
244  $this->fieldName,
245  ],
246  ],
247  ];
248 
249  return $requestQuery;
250  }
251 }
__construct(ConnectionManager $connectionManager, FieldMapperInterface $fieldMapper, Config $clientConfig, SearchIndexNameResolver $searchIndexNameResolver, string $fieldName, string $storeId, array $entityIds)
Definition: Interval.php:71
load($limit, $offset=null, $lower=null, $upper=null)
Definition: Interval.php:92
$index
Definition: list.phtml:44