BulkSourceUnassign.php 3.4 KB

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