BulkSourceUnassign.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Inventory\Model\ResourceModel\SourceItem;
  10. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  11. use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
  12. /**
  13. * Implementation of bulk source assignment
  14. *
  15. * This class is not intended to be used directly.
  16. * @see \Magento\InventoryCatalogApi\Api\BulkSourceUnassignInterface
  17. */
  18. class BulkSourceUnassign
  19. {
  20. /**
  21. * @var ResourceConnection
  22. */
  23. private $resourceConnection;
  24. /**
  25. * @var BulkZeroLegacyStockItem
  26. */
  27. private $bulkZeroLegacyStockItem;
  28. /**
  29. * @var DefaultSourceProviderInterface
  30. */
  31. private $defaultSourceProvider;
  32. /**
  33. * @param ResourceConnection $resourceConnection
  34. * @param DefaultSourceProviderInterface $defaultSourceProvider
  35. * @param BulkZeroLegacyStockItem $bulkZeroLegacyStockItem
  36. * @SuppressWarnings(PHPMD.LongVariable)
  37. */
  38. public function __construct(
  39. ResourceConnection $resourceConnection,
  40. DefaultSourceProviderInterface $defaultSourceProvider,
  41. BulkZeroLegacyStockItem $bulkZeroLegacyStockItem
  42. ) {
  43. $this->resourceConnection = $resourceConnection;
  44. $this->bulkZeroLegacyStockItem = $bulkZeroLegacyStockItem;
  45. $this->defaultSourceProvider = $defaultSourceProvider;
  46. }
  47. /**
  48. * Assign sources to products
  49. * @param array $skus
  50. * @param array $sourceCodes
  51. * @return int
  52. * @throws \Magento\Framework\Exception\NoSuchEntityException
  53. */
  54. public function execute(array $skus, array $sourceCodes): int
  55. {
  56. $connection = $this->resourceConnection->getConnection();
  57. $tableName = $this->resourceConnection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM);
  58. $connection->beginTransaction();
  59. $count = (int) $connection->delete($tableName, [
  60. SourceItemInterface::SOURCE_CODE . ' IN (?)' => $sourceCodes,
  61. SourceItemInterface::SKU . ' IN (?)' => $skus,
  62. ]);
  63. // Legacy stock update
  64. if (in_array($this->defaultSourceProvider->getCode(), $sourceCodes)) {
  65. $this->bulkZeroLegacyStockItem->execute($skus);
  66. }
  67. $connection->commit();
  68. return $count;
  69. }
  70. }