StockRegistryProvider.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Model;
  7. use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
  8. use Magento\CatalogInventory\Api\StockRepositoryInterface;
  9. use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
  10. use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
  11. use Magento\CatalogInventory\Api\Data\StockInterfaceFactory;
  12. use Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory;
  13. use Magento\CatalogInventory\Api\Data\StockStatusInterfaceFactory;
  14. use Magento\CatalogInventory\Api\StockCriteriaInterfaceFactory;
  15. use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
  16. use Magento\CatalogInventory\Api\StockStatusCriteriaInterfaceFactory;
  17. use Magento\Store\Model\StoreManagerInterface;
  18. /**
  19. * Class StockRegistryProvider
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. */
  22. class StockRegistryProvider implements StockRegistryProviderInterface
  23. {
  24. /**
  25. * @var StockRepositoryInterface
  26. */
  27. protected $stockRepository;
  28. /**
  29. * @var StockInterfaceFactory
  30. */
  31. protected $stockFactory;
  32. /**
  33. * @var StockItemRepositoryInterface
  34. */
  35. protected $stockItemRepository;
  36. /**
  37. * @var StockItemInterfaceFactory
  38. */
  39. protected $stockItemFactory;
  40. /**
  41. * @var StockStatusRepositoryInterface
  42. */
  43. protected $stockStatusRepository;
  44. /**
  45. * @var StockStatusInterfaceFactory
  46. */
  47. protected $stockStatusFactory;
  48. /**
  49. * @var StockCriteriaInterfaceFactory
  50. */
  51. protected $stockCriteriaFactory;
  52. /**
  53. * @var StockItemCriteriaInterfaceFactory
  54. */
  55. protected $stockItemCriteriaFactory;
  56. /**
  57. * @var StockStatusCriteriaInterfaceFactory
  58. */
  59. protected $stockStatusCriteriaFactory;
  60. /**
  61. * @var StockRegistryStorage
  62. */
  63. protected $stockRegistryStorage;
  64. /**
  65. * @param StockRepositoryInterface $stockRepository
  66. * @param StockInterfaceFactory $stockFactory
  67. * @param StockItemRepositoryInterface $stockItemRepository
  68. * @param StockItemInterfaceFactory $stockItemFactory
  69. * @param StockStatusRepositoryInterface $stockStatusRepository
  70. * @param StockStatusInterfaceFactory $stockStatusFactory
  71. * @param StockCriteriaInterfaceFactory $stockCriteriaFactory
  72. * @param StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory
  73. * @param StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory
  74. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  75. */
  76. public function __construct(
  77. StockRepositoryInterface $stockRepository,
  78. StockInterfaceFactory $stockFactory,
  79. StockItemRepositoryInterface $stockItemRepository,
  80. StockItemInterfaceFactory $stockItemFactory,
  81. StockStatusRepositoryInterface $stockStatusRepository,
  82. StockStatusInterfaceFactory $stockStatusFactory,
  83. StockCriteriaInterfaceFactory $stockCriteriaFactory,
  84. StockItemCriteriaInterfaceFactory $stockItemCriteriaFactory,
  85. StockStatusCriteriaInterfaceFactory $stockStatusCriteriaFactory
  86. ) {
  87. $this->stockRepository = $stockRepository;
  88. $this->stockFactory = $stockFactory;
  89. $this->stockItemRepository = $stockItemRepository;
  90. $this->stockItemFactory = $stockItemFactory;
  91. $this->stockStatusRepository = $stockStatusRepository;
  92. $this->stockStatusFactory = $stockStatusFactory;
  93. $this->stockCriteriaFactory = $stockCriteriaFactory;
  94. $this->stockItemCriteriaFactory = $stockItemCriteriaFactory;
  95. $this->stockStatusCriteriaFactory = $stockStatusCriteriaFactory;
  96. }
  97. /**
  98. * @param int|null $scopeId
  99. * @return \Magento\CatalogInventory\Api\Data\StockInterface
  100. */
  101. public function getStock($scopeId)
  102. {
  103. $stock = $this->getStockRegistryStorage()->getStock($scopeId);
  104. if (null === $stock) {
  105. $criteria = $this->stockCriteriaFactory->create();
  106. $criteria->setScopeFilter($scopeId);
  107. $collection = $this->stockRepository->getList($criteria);
  108. $stock = current($collection->getItems());
  109. if ($stock && $stock->getStockId()) {
  110. $this->getStockRegistryStorage()->setStock($scopeId, $stock);
  111. } else {
  112. $stock = $this->stockFactory->create();
  113. }
  114. }
  115. return $stock;
  116. }
  117. /**
  118. * @param int $productId
  119. * @param int $scopeId
  120. * @return \Magento\CatalogInventory\Api\Data\StockItemInterface
  121. */
  122. public function getStockItem($productId, $scopeId)
  123. {
  124. $stockItem = $this->getStockRegistryStorage()->getStockItem($productId, $scopeId);
  125. if (null === $stockItem) {
  126. $criteria = $this->stockItemCriteriaFactory->create();
  127. $criteria->setProductsFilter($productId);
  128. $collection = $this->stockItemRepository->getList($criteria);
  129. $stockItem = current($collection->getItems());
  130. if ($stockItem && $stockItem->getItemId()) {
  131. $this->getStockRegistryStorage()->setStockItem($productId, $scopeId, $stockItem);
  132. } else {
  133. $stockItem = $this->stockItemFactory->create();
  134. }
  135. }
  136. return $stockItem;
  137. }
  138. /**
  139. * @param int $productId
  140. * @param int $scopeId
  141. * @return \Magento\CatalogInventory\Api\Data\StockStatusInterface
  142. */
  143. public function getStockStatus($productId, $scopeId)
  144. {
  145. $stockStatus = $this->getStockRegistryStorage()->getStockStatus($productId, $scopeId);
  146. if (null === $stockStatus) {
  147. $criteria = $this->stockStatusCriteriaFactory->create();
  148. $criteria->setProductsFilter($productId);
  149. $criteria->setScopeFilter($scopeId);
  150. $collection = $this->stockStatusRepository->getList($criteria);
  151. $stockStatus = current($collection->getItems());
  152. if ($stockStatus && $stockStatus->getProductId()) {
  153. $this->getStockRegistryStorage()->setStockStatus($productId, $scopeId, $stockStatus);
  154. } else {
  155. $stockStatus = $this->stockStatusFactory->create();
  156. }
  157. }
  158. return $stockStatus;
  159. }
  160. /**
  161. * @return StockRegistryStorage
  162. */
  163. private function getStockRegistryStorage()
  164. {
  165. if (null === $this->stockRegistryStorage) {
  166. $this->stockRegistryStorage = \Magento\Framework\App\ObjectManager::getInstance()
  167. ->get(\Magento\CatalogInventory\Model\StockRegistryStorage::class);
  168. }
  169. return $this->stockRegistryStorage;
  170. }
  171. }