6 declare(strict_types=1);
29 private $resourceConnection;
34 private $isSourceItemManagementAllowedForProductType;
39 private $updateSourceItemBasedOnLegacyStockItem;
44 private $getProductTypeBySku;
49 private $getSkusByProductIds;
54 private $getDefaultSourceItemBySku;
72 $this->updateSourceItemBasedOnLegacyStockItem = $updateSourceItemBasedOnLegacyStockItem;
74 $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
75 $this->getProductTypeBySku = $getProductTypeBySku;
76 $this->getSkusByProductIds = $getSkusByProductIds;
77 $this->getDefaultSourceItemBySku = $getDefaultSourceItemBySku;
91 $connection = $this->resourceConnection->getConnection();
95 $proceed($legacyStockItem);
97 $typeId = $this->getTypeId($legacyStockItem);
98 if ($this->isSourceItemManagementAllowedForProductType->execute($typeId)) {
99 if ($this->shouldAlignDefaultSourceWithLegacy($legacyStockItem)) {
100 $this->updateSourceItemBasedOnLegacyStockItem->execute($legacyStockItem);
107 }
catch (\Exception $e) {
119 private function shouldAlignDefaultSourceWithLegacy(
Item $legacyStockItem): bool
121 $productSku = $this->getSkusByProductIds
125 ((float) $legacyStockItem->
getQty() !== (float) 0) ||
126 ($this->getDefaultSourceItemBySku->execute($productSku) !==
null);
136 private function getTypeId(Item $legacyStockItem): string
138 $typeId = $legacyStockItem->getTypeId();
139 if ($typeId ===
null) {
140 $sku = $legacyStockItem->getSku();
142 $productId = $legacyStockItem->getProductId();
145 $typeId = $this->getProductTypeBySku->execute([$sku])[$sku];
__construct(UpdateSourceItemBasedOnLegacyStockItem $updateSourceItemBasedOnLegacyStockItem, ResourceConnection $resourceConnection, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType, GetProductTypesBySkusInterface $getProductTypeBySku, GetSkusByProductIdsInterface $getSkusByProductIds, GetDefaultSourceItemBySku $getDefaultSourceItemBySku)
aroundSave(ItemResourceModel $subject, callable $proceed, AbstractModel $legacyStockItem)