Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerHandler.php
Go to the documentation of this file.
1 <?php
7 
15 
25 {
29  private $indexStructure;
30 
34  private $data;
35 
39  private $fields;
40 
44  private $resource;
45 
49  private $batch;
50 
54  private $eavConfig;
55 
59  private $batchSize;
60 
64  private $indexScopeResolver;
65 
75  public function __construct(
76  IndexStructureInterface $indexStructure,
77  ResourceConnection $resource,
78  Config $eavConfig,
79  Batch $batch,
80  IndexScopeResolverInterface $indexScopeResolver,
81  array $data,
82  $batchSize = 500
83  ) {
84  $this->indexScopeResolver = $indexScopeResolver;
85  $this->indexStructure = $indexStructure;
86  $this->resource = $resource;
87  $this->batch = $batch;
88  $this->eavConfig = $eavConfig;
89  $this->data = $data;
90  $this->fields = [];
91 
92  $this->prepareFields();
93  $this->batchSize = $batchSize;
94  }
95 
99  public function saveIndex($dimensions, \Traversable $documents)
100  {
101  foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
102  $this->insertDocuments($batchDocuments, $dimensions);
103  }
104  }
105 
109  public function deleteIndex($dimensions, \Traversable $documents)
110  {
111  foreach ($this->batch->getItems($documents, $this->batchSize) as $batchDocuments) {
112  $this->resource->getConnection()
113  ->delete($this->getTableName($dimensions), ['entity_id in (?)' => $batchDocuments]);
114  }
115  }
116 
120  public function cleanIndex($dimensions)
121  {
122  $this->indexStructure->delete($this->getIndexName(), $dimensions);
123  $this->indexStructure->create($this->getIndexName(), [], $dimensions);
124  }
125 
129  public function isAvailable($dimensions = [])
130  {
131  if (empty($dimensions)) {
132  return true;
133  }
134 
135  return $this->resource->getConnection()->isTableExists($this->getTableName($dimensions));
136  }
137 
144  private function getTableName($dimensions)
145  {
146  return $this->indexScopeResolver->resolve($this->getIndexName(), $dimensions);
147  }
148 
154  private function getIndexName()
155  {
156  return $this->data['indexer_id'];
157  }
158 
166  private function insertDocuments(array $documents, array $dimensions)
167  {
168  $documents = $this->prepareSearchableFields($documents);
169  if (empty($documents)) {
170  return;
171  }
172  $this->resource->getConnection()->insertOnDuplicate(
173  $this->getTableName($dimensions),
174  $documents,
175  ['data_index']
176  );
177  }
178 
185  private function prepareSearchableFields(array $documents)
186  {
187  $insertDocuments = [];
188  foreach ($documents as $entityId => $document) {
189  foreach ($document as $attributeId => $fieldValue) {
190  $insertDocuments[$entityId . '_' . $attributeId] = [
191  'entity_id' => $entityId,
192  'attribute_id' => $attributeId,
193  'data_index' => $fieldValue,
194  ];
195  }
196  }
197 
198  return $insertDocuments;
199  }
200 
206  private function prepareFields()
207  {
208  foreach ($this->data['fieldsets'] as $fieldset) {
209  $this->fields = array_merge($this->fields, $fieldset['fields']);
210  }
211  }
212 }
$resource
Definition: bulk.php:12
saveIndex($dimensions, \Traversable $documents)
__construct(IndexStructureInterface $indexStructure, ResourceConnection $resource, Config $eavConfig, Batch $batch, IndexScopeResolverInterface $indexScopeResolver, array $data, $batchSize=500)
deleteIndex($dimensions, \Traversable $documents)