Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SetDataToLegacyCatalogInventory.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
19 use Magento\InventoryCatalogApi\Model\SourceItemsSaveSynchronizationInterface;
20 
25 {
29  private $setDataToLegacyStockItem;
30 
34  private $legacyStockItemCriteriaFactory;
35 
39  private $legacyStockItemRepository;
40 
44  private $getProductIdsBySkus;
45 
49  private $stockStateProvider;
50 
54  private $indexerProcessor;
55 
64  public function __construct(
65  SetDataToLegacyStockItem $setDataToLegacyStockItem,
66  StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory,
67  StockItemRepositoryInterface $legacyStockItemRepository,
68  GetProductIdsBySkusInterface $getProductIdsBySkus,
69  StockStateProviderInterface $stockStateProvider,
70  Processor $indexerProcessor
71  ) {
72  $this->setDataToLegacyStockItem = $setDataToLegacyStockItem;
73  $this->legacyStockItemCriteriaFactory = $legacyStockItemCriteriaFactory;
74  $this->legacyStockItemRepository = $legacyStockItemRepository;
75  $this->getProductIdsBySkus = $getProductIdsBySkus;
76  $this->stockStateProvider = $stockStateProvider;
77  $this->indexerProcessor = $indexerProcessor;
78  }
79 
84  public function execute(array $sourceItems): void
85  {
86  $productIds = [];
87  foreach ($sourceItems as $sourceItem) {
88  $sku = $sourceItem->getSku();
89 
90  try {
91  $productId = (int)$this->getProductIdsBySkus->execute([$sku])[$sku];
92  } catch (NoSuchEntityException $e) {
93  // Skip synchronization of for not existed product
94  continue;
95  }
96 
97  $legacyStockItem = $this->getLegacyStockItem($productId);
98  if (null === $legacyStockItem) {
99  continue;
100  }
101 
102  $isInStock = (int)$sourceItem->getStatus();
103 
104  if ($legacyStockItem->getManageStock()) {
105  $legacyStockItem->setIsInStock($isInStock);
106  $legacyStockItem->setQty((float)$sourceItem->getQuantity());
107 
108  if (false === $this->stockStateProvider->verifyStock($legacyStockItem)) {
109  $isInStock = 0;
110  }
111  }
112 
113  $this->setDataToLegacyStockItem->execute(
114  (string)$sourceItem->getSku(),
115  (float)$sourceItem->getQuantity(),
116  $isInStock
117  );
119  }
120 
121  if ($productIds) {
122  $this->indexerProcessor->reindexList($productIds);
123  }
124  }
125 
130  private function getLegacyStockItem(int $productId): ?StockItemInterface
131  {
132  $searchCriteria = $this->legacyStockItemCriteriaFactory->create();
133 
136 
137  $stockItemCollection = $this->legacyStockItemRepository->getList($searchCriteria);
138  if ($stockItemCollection->getTotalCount() === 0) {
139  return null;
140  }
141 
142  $stockItems = $stockItemCollection->getItems();
143  $stockItem = reset($stockItems);
144  return $stockItem;
145  }
146 }
__construct(SetDataToLegacyStockItem $setDataToLegacyStockItem, StockItemCriteriaInterfaceFactory $legacyStockItemCriteriaFactory, StockItemRepositoryInterface $legacyStockItemRepository, GetProductIdsBySkusInterface $getProductIdsBySkus, StockStateProviderInterface $stockStateProvider, Processor $indexerProcessor)
$searchCriteria
$sourceItems