BulkInventoryTransfer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Framework\Validation\ValidationException;
  9. use Magento\InventoryCatalog\Model\ResourceModel\BulkInventoryTransfer as BulkInventoryTransferResource;
  10. use Magento\InventoryCatalogApi\Api\BulkInventoryTransferInterface;
  11. use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
  12. use Magento\InventoryCatalogApi\Model\BulkInventoryTransferValidatorInterface;
  13. use Magento\InventoryCatalogApi\Model\GetProductIdsBySkusInterface;
  14. use Magento\CatalogInventory\Model\Indexer\Stock as LegacyIndexer;
  15. use Magento\InventoryIndexer\Indexer\Source\SourceIndexer;
  16. /**
  17. * @inheritdoc
  18. */
  19. class BulkInventoryTransfer implements BulkInventoryTransferInterface
  20. {
  21. /**
  22. * @var BulkInventoryTransferValidatorInterface
  23. */
  24. private $bulkInventoryTransferValidator;
  25. /**
  26. * @var BulkInventoryTransfer
  27. */
  28. private $bulkInventoryTransfer;
  29. /**
  30. * @var GetProductIdsBySkusInterface
  31. */
  32. private $getProductIdsBySkus;
  33. /**
  34. * @var LegacyIndexer
  35. */
  36. private $legacyIndexer;
  37. /**
  38. * @var DefaultSourceProviderInterface
  39. */
  40. private $defaultSourceProvider;
  41. /**
  42. * @var SourceIndexer
  43. */
  44. private $sourceIndexer;
  45. /**
  46. * MassProductSourceAssign constructor.
  47. * @param BulkInventoryTransferValidatorInterface $inventoryTransferValidator
  48. * @param BulkInventoryTransferResource $bulkInventoryTransfer
  49. * @param SourceIndexer $sourceIndexer
  50. * @param DefaultSourceProviderInterface $defaultSourceProvider
  51. * @param GetProductIdsBySkusInterface $getProductIdsBySkus
  52. * @param LegacyIndexer $legacyIndexer
  53. * @SuppressWarnings(PHPMD.LongVariable)
  54. */
  55. public function __construct(
  56. BulkInventoryTransferValidatorInterface $inventoryTransferValidator,
  57. BulkInventoryTransferResource $bulkInventoryTransfer,
  58. SourceIndexer $sourceIndexer,
  59. DefaultSourceProviderInterface $defaultSourceProvider,
  60. GetProductIdsBySkusInterface $getProductIdsBySkus,
  61. LegacyIndexer $legacyIndexer
  62. ) {
  63. $this->bulkInventoryTransferValidator = $inventoryTransferValidator;
  64. $this->bulkInventoryTransfer = $bulkInventoryTransfer;
  65. $this->getProductIdsBySkus = $getProductIdsBySkus;
  66. $this->legacyIndexer = $legacyIndexer;
  67. $this->defaultSourceProvider = $defaultSourceProvider;
  68. $this->sourceIndexer = $sourceIndexer;
  69. }
  70. /**
  71. * Reindex legacy stock (for default source)
  72. * @param array $productIds
  73. */
  74. private function reindexLegacy(array $productIds): void
  75. {
  76. $this->legacyIndexer->executeList($productIds);
  77. }
  78. /**
  79. * @inheritdoc
  80. * @throws \Magento\Framework\Exception\NoSuchEntityException
  81. */
  82. public function execute(
  83. array $skus,
  84. string $originSource,
  85. string $destinationSource,
  86. bool $unassignFromOrigin
  87. ): bool {
  88. $validationResult = $this->bulkInventoryTransferValidator->validate(
  89. $skus,
  90. $originSource,
  91. $destinationSource
  92. );
  93. if (!$validationResult->isValid()) {
  94. throw new ValidationException(__('Validation Failed'), null, 0, $validationResult);
  95. }
  96. $this->bulkInventoryTransfer->execute(
  97. $skus,
  98. $originSource,
  99. $destinationSource,
  100. $unassignFromOrigin
  101. );
  102. $this->sourceIndexer->executeList([$originSource, $destinationSource]);
  103. if (($this->defaultSourceProvider->getCode() === $originSource) ||
  104. ($this->defaultSourceProvider->getCode() === $destinationSource)) {
  105. $productIds = array_values($this->getProductIdsBySkus->execute($skus));
  106. $this->reindexLegacy($productIds);
  107. }
  108. return true;
  109. }
  110. }