Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateSourceItemAtLegacyStockItemSavePlugin.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 
25 {
29  private $resourceConnection;
30 
34  private $isSourceItemManagementAllowedForProductType;
35 
39  private $updateSourceItemBasedOnLegacyStockItem;
40 
44  private $getProductTypeBySku;
45 
49  private $getSkusByProductIds;
50 
54  private $getDefaultSourceItemBySku;
55 
64  public function __construct(
65  UpdateSourceItemBasedOnLegacyStockItem $updateSourceItemBasedOnLegacyStockItem,
66  ResourceConnection $resourceConnection,
67  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
68  GetProductTypesBySkusInterface $getProductTypeBySku,
69  GetSkusByProductIdsInterface $getSkusByProductIds,
70  GetDefaultSourceItemBySku $getDefaultSourceItemBySku
71  ) {
72  $this->updateSourceItemBasedOnLegacyStockItem = $updateSourceItemBasedOnLegacyStockItem;
73  $this->resourceConnection = $resourceConnection;
74  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
75  $this->getProductTypeBySku = $getProductTypeBySku;
76  $this->getSkusByProductIds = $getSkusByProductIds;
77  $this->getDefaultSourceItemBySku = $getDefaultSourceItemBySku;
78  }
79 
89  public function aroundSave(ItemResourceModel $subject, callable $proceed, AbstractModel $legacyStockItem)
90  {
91  $connection = $this->resourceConnection->getConnection();
92  $connection->beginTransaction();
93  try {
94  // need to save configuration
95  $proceed($legacyStockItem);
96 
97  $typeId = $this->getTypeId($legacyStockItem);
98  if ($this->isSourceItemManagementAllowedForProductType->execute($typeId)) {
99  if ($this->shouldAlignDefaultSourceWithLegacy($legacyStockItem)) {
100  $this->updateSourceItemBasedOnLegacyStockItem->execute($legacyStockItem);
101  }
102  }
103 
104  $connection->commit();
105 
106  return $subject;
107  } catch (\Exception $e) {
108  $connection->rollBack();
109  throw $e;
110  }
111  }
112 
119  private function shouldAlignDefaultSourceWithLegacy(Item $legacyStockItem): bool
120  {
121  $productSku = $this->getSkusByProductIds
122  ->execute([$legacyStockItem->getProductId()])[$legacyStockItem->getProductId()];
123 
124  $result = $legacyStockItem->getIsInStock() ||
125  ((float) $legacyStockItem->getQty() !== (float) 0) ||
126  ($this->getDefaultSourceItemBySku->execute($productSku) !== null);
127 
128  return $result;
129  }
130 
136  private function getTypeId(Item $legacyStockItem): string
137  {
138  $typeId = $legacyStockItem->getTypeId();
139  if ($typeId === null) {
140  $sku = $legacyStockItem->getSku();
141  if ($sku === null) {
142  $productId = $legacyStockItem->getProductId();
143  $sku = $this->getSkusByProductIds->execute([$productId])[$productId];
144  }
145  $typeId = $this->getProductTypeBySku->execute([$sku])[$sku];
146  }
147 
148  return $typeId;
149  }
150 }
__construct(UpdateSourceItemBasedOnLegacyStockItem $updateSourceItemBasedOnLegacyStockItem, ResourceConnection $resourceConnection, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType, GetProductTypesBySkusInterface $getProductTypeBySku, GetSkusByProductIdsInterface $getSkusByProductIds, GetDefaultSourceItemBySku $getDefaultSourceItemBySku)
$connection
Definition: bulk.php:13