StockSourceLinkProcessor.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\InventoryAdminUi\Model\Stock;
  8. use Magento\Framework\Api\DataObjectHelper;
  9. use Magento\Framework\Exception\InputException;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. use Magento\InventoryApi\Api\Data\StockSourceLinkInterfaceFactory;
  12. use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
  13. use Magento\InventoryApi\Api\GetStockSourceLinksInterface;
  14. use Magento\InventoryApi\Api\StockSourceLinksDeleteInterface;
  15. use Magento\InventoryApi\Api\StockSourceLinksSaveInterface;
  16. /**
  17. * At the time of processing Stock save form this class used to save links correctly
  18. * Performs replace strategy of sources for the stock
  19. */
  20. class StockSourceLinkProcessor
  21. {
  22. /**
  23. * @var SearchCriteriaBuilder
  24. */
  25. private $searchCriteriaBuilder;
  26. /**
  27. * @var StockSourceLinkInterfaceFactory
  28. */
  29. private $stockSourceLinkFactory;
  30. /**
  31. * @var StockSourceLinksSaveInterface
  32. */
  33. private $stockSourceLinksSave;
  34. /**
  35. * @var StockSourceLinksDeleteInterface
  36. */
  37. private $stockSourceLinksDelete;
  38. /**
  39. * @var GetStockSourceLinksInterface
  40. */
  41. private $getStockSourceLinks;
  42. /**
  43. * @var DataObjectHelper
  44. */
  45. private $dataObjectHelper;
  46. /**
  47. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  48. * @param StockSourceLinkInterfaceFactory $stockSourceLinkFactory
  49. * @param StockSourceLinksSaveInterface $stockSourceLinksSave
  50. * @param StockSourceLinksDeleteInterface $stockSourceLinksDelete
  51. * @param GetStockSourceLinksInterface $getStockSourceLinks
  52. * @param DataObjectHelper $dataObjectHelper
  53. */
  54. public function __construct(
  55. SearchCriteriaBuilder $searchCriteriaBuilder,
  56. StockSourceLinkInterfaceFactory $stockSourceLinkFactory,
  57. StockSourceLinksSaveInterface $stockSourceLinksSave,
  58. StockSourceLinksDeleteInterface $stockSourceLinksDelete,
  59. GetStockSourceLinksInterface $getStockSourceLinks,
  60. DataObjectHelper $dataObjectHelper
  61. ) {
  62. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  63. $this->stockSourceLinkFactory = $stockSourceLinkFactory;
  64. $this->stockSourceLinksSave = $stockSourceLinksSave;
  65. $this->stockSourceLinksDelete = $stockSourceLinksDelete;
  66. $this->getStockSourceLinks = $getStockSourceLinks;
  67. $this->dataObjectHelper = $dataObjectHelper;
  68. }
  69. /**
  70. * @param int $stockId
  71. * @param array $linksData
  72. * @return void
  73. * @throws InputException
  74. */
  75. public function process(int $stockId, array $linksData)
  76. {
  77. $linksForDelete = $this->getAssignedLinks($stockId);
  78. $linksForSave = [];
  79. foreach ($linksData as $linkData) {
  80. $sourceCode = $linkData[StockSourceLinkInterface::SOURCE_CODE];
  81. if (isset($linksForDelete[$sourceCode])) {
  82. $link = $linksForDelete[$sourceCode];
  83. } else {
  84. /** @var StockSourceLinkInterface $link */
  85. $link = $this->stockSourceLinkFactory->create();
  86. }
  87. $linkData[StockSourceLinkInterface::STOCK_ID] = $stockId;
  88. $this->dataObjectHelper->populateWithArray($link, $linkData, StockSourceLinkInterface::class);
  89. $linksForSave[] = $link;
  90. unset($linksForDelete[$sourceCode]);
  91. }
  92. if (count($linksForSave) > 0) {
  93. $this->stockSourceLinksSave->execute($linksForSave);
  94. }
  95. if (count($linksForDelete) > 0) {
  96. $this->stockSourceLinksDelete->execute($linksForDelete);
  97. }
  98. }
  99. /**
  100. * Retrieves links that are assigned to $stockId
  101. *
  102. * @param int $stockId
  103. * @return StockSourceLinkInterface[]
  104. */
  105. private function getAssignedLinks(int $stockId): array
  106. {
  107. $searchCriteria = $this->searchCriteriaBuilder
  108. ->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
  109. ->create();
  110. $result = [];
  111. foreach ($this->getStockSourceLinks->execute($searchCriteria)->getItems() as $link) {
  112. $result[$link->getSourceCode()] = $link;
  113. }
  114. return $result;
  115. }
  116. }