Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StockIndexer.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 
26 {
30  private $getAllStockIds;
31 
35  private $indexStructure;
36 
40  private $indexHandler;
41 
45  private $indexNameBuilder;
46 
50  private $indexDataProviderByStockId;
51 
55  private $indexTableSwitcher;
56 
60  private $defaultStockProvider;
61 
73  public function __construct(
74  GetAllStockIds $getAllStockIds,
75  IndexStructureInterface $indexStructureHandler,
76  IndexHandlerInterface $indexHandler,
77  IndexNameBuilder $indexNameBuilder,
78  IndexDataProviderByStockId $indexDataProviderByStockId,
79  IndexTableSwitcherInterface $indexTableSwitcher,
80  DefaultStockProviderInterface $defaultStockProvider
81  ) {
82  $this->getAllStockIds = $getAllStockIds;
83  $this->indexStructure = $indexStructureHandler;
84  $this->indexHandler = $indexHandler;
85  $this->indexNameBuilder = $indexNameBuilder;
86  $this->indexDataProviderByStockId = $indexDataProviderByStockId;
87  $this->indexTableSwitcher = $indexTableSwitcher;
88  $this->defaultStockProvider = $defaultStockProvider;
89  }
90 
94  public function executeFull()
95  {
96  $stockIds = $this->getAllStockIds->execute();
97  $this->executeList($stockIds);
98  }
99 
104  public function executeRow(int $stockId)
105  {
106  $this->executeList([$stockId]);
107  }
108 
113  public function executeList(array $stockIds)
114  {
115  foreach ($stockIds as $stockId) {
116  if ($this->defaultStockProvider->getId() === (int)$stockId) {
117  continue;
118  }
119 
120  $replicaIndexName = $this->indexNameBuilder
121  ->setIndexId(InventoryIndexer::INDEXER_ID)
122  ->addDimension('stock_', (string)$stockId)
123  ->setAlias(Alias::ALIAS_REPLICA)
124  ->build();
125 
126  $mainIndexName = $this->indexNameBuilder
127  ->setIndexId(InventoryIndexer::INDEXER_ID)
128  ->addDimension('stock_', (string)$stockId)
129  ->setAlias(Alias::ALIAS_MAIN)
130  ->build();
131 
132  $this->indexStructure->delete($replicaIndexName, ResourceConnection::DEFAULT_CONNECTION);
133  $this->indexStructure->create($replicaIndexName, ResourceConnection::DEFAULT_CONNECTION);
134 
135  if (!$this->indexStructure->isExist($mainIndexName, ResourceConnection::DEFAULT_CONNECTION)) {
136  $this->indexStructure->create($mainIndexName, ResourceConnection::DEFAULT_CONNECTION);
137  }
138 
139  $this->indexHandler->saveIndex(
140  $replicaIndexName,
141  $this->indexDataProviderByStockId->execute((int)$stockId),
143  );
144  $this->indexTableSwitcher->switch($mainIndexName, ResourceConnection::DEFAULT_CONNECTION);
145  $this->indexStructure->delete($replicaIndexName, ResourceConnection::DEFAULT_CONNECTION);
146  }
147  }
148 }
__construct(GetAllStockIds $getAllStockIds, IndexStructureInterface $indexStructureHandler, IndexHandlerInterface $indexHandler, IndexNameBuilder $indexNameBuilder, IndexDataProviderByStockId $indexDataProviderByStockId, IndexTableSwitcherInterface $indexTableSwitcher, DefaultStockProviderInterface $defaultStockProvider)