BulkSourceAssign.php 3.1 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\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\DuplicateException;
  10. use Magento\Inventory\Model\ResourceModel\SourceItem;
  11. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  12. use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
  13. use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
  14. /**
  15. * Implementation of bulk source assignment
  16. *
  17. * This class is not intended to be used directly.
  18. * @see \Magento\InventoryCatalogApi\Api\BulkSourceAssignInterface
  19. */
  20. class BulkSourceAssign
  21. {
  22. /**
  23. * @var ResourceConnection
  24. */
  25. private $resourceConnection;
  26. /**
  27. * @var IsSourceItemManagementAllowedForProductTypeInterface
  28. */
  29. private $isSourceItemManagementAllowedForProductType;
  30. /**
  31. * @var GetProductTypesBySkusInterface
  32. */
  33. private $getProductTypesBySkus;
  34. /**
  35. * @param ResourceConnection $resourceConnection
  36. * @param GetProductTypesBySkusInterface $getProductTypesBySkus
  37. * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  38. * @SuppressWarnings(PHPMD.LongVariable)
  39. */
  40. public function __construct(
  41. ResourceConnection $resourceConnection,
  42. GetProductTypesBySkusInterface $getProductTypesBySkus,
  43. IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  44. ) {
  45. $this->resourceConnection = $resourceConnection;
  46. $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
  47. $this->getProductTypesBySkus = $getProductTypesBySkus;
  48. }
  49. /**
  50. * Assign sources to products
  51. * @param array $skus
  52. * @param array $sourceCodes
  53. * @return int
  54. */
  55. public function execute(array $skus, array $sourceCodes): int
  56. {
  57. $types = $this->getProductTypesBySkus->execute($skus);
  58. $connection = $this->resourceConnection->getConnection();
  59. $tableName = $this->resourceConnection->getTableName(SourceItem::TABLE_NAME_SOURCE_ITEM);
  60. $count = 0;
  61. foreach ($types as $sku => $type) {
  62. if ($this->isSourceItemManagementAllowedForProductType->execute($type)) {
  63. foreach ($sourceCodes as $sourceCode) {
  64. try {
  65. $connection->insert($tableName, [
  66. SourceItemInterface::SOURCE_CODE => $sourceCode,
  67. SourceItemInterface::SKU => $sku,
  68. SourceItemInterface::QUANTITY => 0,
  69. SourceItemInterface::STATUS => SourceItemInterface::STATUS_OUT_OF_STOCK,
  70. ]);
  71. $count++;
  72. } catch (DuplicateException $e) {
  73. // Skip if source assignment is duplicated
  74. continue;
  75. }
  76. }
  77. }
  78. }
  79. return $count;
  80. }
  81. }