BulkConfigurationAssign.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\InventoryLowQuantityNotification\Model\ResourceModel;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\DuplicateException;
  10. use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
  11. use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
  12. use Magento\InventoryLowQuantityNotification\Model\SourceItemConfiguration;
  13. /**
  14. * Bulk configuration assign resource model
  15. */
  16. class BulkConfigurationAssign
  17. {
  18. /**
  19. * @var ResourceConnection
  20. */
  21. private $resourceConnection;
  22. /**
  23. * @var IsSourceItemManagementAllowedForProductTypeInterface
  24. */
  25. private $isSourceItemManagementAllowedForProductType;
  26. /**
  27. * @var GetProductTypesBySkusInterface
  28. */
  29. private $getProductTypesBySkus;
  30. /**
  31. * @param ResourceConnection $resourceConnection
  32. * @param GetProductTypesBySkusInterface $getProductTypesBySkus
  33. * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  34. * @SuppressWarnings(PHPMD.LongVariable)
  35. */
  36. public function __construct(
  37. ResourceConnection $resourceConnection,
  38. GetProductTypesBySkusInterface $getProductTypesBySkus,
  39. IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  40. ) {
  41. $this->resourceConnection = $resourceConnection;
  42. $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
  43. $this->getProductTypesBySkus = $getProductTypesBySkus;
  44. }
  45. /**
  46. * Bulk assign source items configurations to source items
  47. *
  48. * @param array $skus
  49. * @param array $sources
  50. */
  51. public function execute(
  52. array $skus,
  53. array $sources
  54. ) {
  55. $tableName = $this->resourceConnection->getTableName('inventory_low_stock_notification_configuration');
  56. $connection = $this->resourceConnection->getConnection();
  57. $types = $this->getProductTypesBySkus->execute($skus);
  58. foreach ($types as $sku => $type) {
  59. if ($this->isSourceItemManagementAllowedForProductType->execute($type)) {
  60. foreach ($sources as $sourceCode) {
  61. try {
  62. $connection->insert($tableName, [
  63. SourceItemConfiguration::SOURCE_CODE => $sourceCode,
  64. SourceItemConfiguration::INVENTORY_NOTIFY_QTY => null,
  65. SourceItemConfiguration::SKU => $sku,
  66. ]);
  67. } catch (DuplicateException $e) {
  68. // Skip if source assignment is duplicated
  69. continue;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }