ProcessSourceItemConfigurationsObserver.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\InventoryLowQuantityNotificationAdminUi\Observer;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Controller\Adminhtml\Product\Save;
  10. use Magento\Framework\Event\ObserverInterface;
  11. use Magento\Framework\Event\Observer as EventObserver;
  12. use Magento\InventoryApi\Api\Data\SourceItemInterface;
  13. use Magento\InventoryCatalogApi\Api\DefaultSourceProviderInterface;
  14. use Magento\InventoryCatalogApi\Model\IsSingleSourceModeInterface;
  15. use Magento\InventoryConfigurationApi\Api\Data\StockItemConfigurationInterface;
  16. use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
  17. use Magento\InventoryLowQuantityNotificationAdminUi\Model\SourceItemsConfigurationProcessor;
  18. /**
  19. * Save source relations (configuration) during product persistence via controller
  20. *
  21. * This needs to be handled in dedicated observer, because there is no pre-defined way of making several API calls for
  22. * Form submission handling
  23. */
  24. class ProcessSourceItemConfigurationsObserver implements ObserverInterface
  25. {
  26. /**
  27. * @var IsSourceItemManagementAllowedForProductTypeInterface
  28. */
  29. private $isSourceItemManagementAllowedForProductType;
  30. /**
  31. * @var SourceItemsConfigurationProcessor
  32. */
  33. private $sourceItemsConfigurationProcessor;
  34. /**
  35. * @var IsSingleSourceModeInterface
  36. */
  37. private $isSingleSourceMode;
  38. /**
  39. * @var DefaultSourceProviderInterface
  40. */
  41. private $defaultSourceProvider;
  42. /**
  43. * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  44. * @param SourceItemsConfigurationProcessor $sourceItemsConfigurationProcessor
  45. */
  46. public function __construct(
  47. IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
  48. SourceItemsConfigurationProcessor $sourceItemsConfigurationProcessor,
  49. IsSingleSourceModeInterface $isSingleSourceMode,
  50. DefaultSourceProviderInterface $defaultSourceProvider
  51. ) {
  52. $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
  53. $this->sourceItemsConfigurationProcessor = $sourceItemsConfigurationProcessor;
  54. $this->isSingleSourceMode = $isSingleSourceMode;
  55. $this->defaultSourceProvider = $defaultSourceProvider;
  56. }
  57. /**
  58. * @param EventObserver $observer
  59. * @return void
  60. */
  61. public function execute(EventObserver $observer)
  62. {
  63. /** @var ProductInterface $product */
  64. $product = $observer->getEvent()->getProduct();
  65. if ($this->isSourceItemManagementAllowedForProductType->execute($product->getTypeId()) === false) {
  66. return;
  67. }
  68. /** @var Save $controller */
  69. $controller = $observer->getEvent()->getController();
  70. $assignedSources = [];
  71. if ($this->isSingleSourceMode->execute()) {
  72. $stockData = $controller->getRequest()->getParam('product', [])['stock_data'] ?? [];
  73. $assignedSources[] = [
  74. SourceItemInterface::SOURCE_CODE => $this->defaultSourceProvider->getCode(),
  75. StockItemConfigurationInterface::NOTIFY_STOCK_QTY =>
  76. $stockData[StockItemConfigurationInterface::NOTIFY_STOCK_QTY] ?? 0,
  77. 'notify_stock_qty_use_default' =>
  78. $stockData[StockItemConfigurationInterface::USE_CONFIG_NOTIFY_STOCK_QTY] ?? 1,
  79. ];
  80. } else {
  81. $sources = $controller->getRequest()->getParam('sources', []);
  82. if (isset($sources['assigned_sources']) && is_array($sources['assigned_sources'])) {
  83. $assignedSources = $sources['assigned_sources'];
  84. }
  85. }
  86. $this->sourceItemsConfigurationProcessor->process($product->getSku(), $assignedSources);
  87. }
  88. }