UpdateSourceItemBasedOnLegacyStockItem.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryCatalog\Model;
  8. use Magento\CatalogInventory\Model\Stock\Item;
  9. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  10. use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
  11. use Magento\InventoryApi\Api\SourceItemsSaveInterface;
  12. use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
  13. use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
  14. class UpdateSourceItemBasedOnLegacyStockItem
  15. {
  16. /**
  17. * @var SourceItemInterfaceFactory
  18. */
  19. private $sourceItemFactory;
  20. /**
  21. * @var SourceItemsSaveInterface
  22. */
  23. private $sourceItemsSave;
  24. /**
  25. * @var DefaultSourceProviderInterface
  26. */
  27. private $defaultSourceProvider;
  28. /**
  29. * @var GetSkusByProductIdsInterface
  30. */
  31. private $getSkusByProductIds;
  32. /**
  33. * @var GetDefaultSourceItemBySku
  34. */
  35. private $getDefaultSourceItemBySku;
  36. /**
  37. * @param SourceItemInterfaceFactory $sourceItemFactory
  38. * @param SourceItemsSaveInterface $sourceItemsSave
  39. * @param DefaultSourceProviderInterface $defaultSourceProvider
  40. * @param GetSkusByProductIdsInterface $getSkusByProductIds
  41. * @param GetDefaultSourceItemBySku $getDefaultSourceItemBySku
  42. * @SuppressWarnings(PHPMD.LongVariable)
  43. */
  44. public function __construct(
  45. SourceItemInterfaceFactory $sourceItemFactory,
  46. SourceItemsSaveInterface $sourceItemsSave,
  47. DefaultSourceProviderInterface $defaultSourceProvider,
  48. GetSkusByProductIdsInterface $getSkusByProductIds,
  49. GetDefaultSourceItemBySku $getDefaultSourceItemBySku
  50. ) {
  51. $this->sourceItemFactory = $sourceItemFactory;
  52. $this->sourceItemsSave = $sourceItemsSave;
  53. $this->getSkusByProductIds = $getSkusByProductIds;
  54. $this->getDefaultSourceItemBySku = $getDefaultSourceItemBySku;
  55. $this->defaultSourceProvider = $defaultSourceProvider;
  56. }
  57. /**
  58. * @param Item $legacyStockItem
  59. * @return void
  60. * @throws \Magento\Framework\Exception\CouldNotSaveException
  61. * @throws \Magento\Framework\Exception\InputException
  62. * @throws \Magento\Framework\Validation\ValidationException
  63. */
  64. public function execute(Item $legacyStockItem)
  65. {
  66. $productSku = $this->getSkusByProductIds
  67. ->execute([$legacyStockItem->getProductId()])[$legacyStockItem->getProductId()];
  68. $sourceItem = $this->getDefaultSourceItemBySku->execute($productSku);
  69. if ($sourceItem === null) {
  70. /** @var SourceItemInterface $sourceItem */
  71. $sourceItem = $this->sourceItemFactory->create();
  72. $sourceItem->setSourceCode($this->defaultSourceProvider->getCode());
  73. $sourceItem->setSku($productSku);
  74. }
  75. $sourceItem->setQuantity((float)$legacyStockItem->getQty());
  76. $sourceItem->setStatus((int)$legacyStockItem->getIsInStock());
  77. $this->sourceItemsSave->execute([$sourceItem]);
  78. }
  79. }