StockRegistryStorage.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Api\Data\StockInterface;
  8. use Magento\CatalogInventory\Api\Data\StockItemInterface;
  9. use Magento\CatalogInventory\Api\Data\StockStatusInterface;
  10. /**
  11. * Class StockRegistryStorage
  12. */
  13. class StockRegistryStorage
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $stocks = [];
  19. /**
  20. * @var array
  21. */
  22. private $stockItems = [];
  23. /**
  24. * @var array
  25. */
  26. private $stockStatuses = [];
  27. /**
  28. * @param int $scopeId
  29. * @return StockInterface
  30. */
  31. public function getStock($scopeId)
  32. {
  33. return isset($this->stocks[$scopeId]) ? $this->stocks[$scopeId] : null;
  34. }
  35. /**
  36. * @param int $scopeId
  37. * @param StockInterface $value
  38. * @return void
  39. */
  40. public function setStock($scopeId, StockInterface $value)
  41. {
  42. $this->stocks[$scopeId] = $value;
  43. }
  44. /**
  45. * @param int|null $scopeId
  46. * @return void
  47. */
  48. public function removeStock($scopeId = null)
  49. {
  50. if (null === $scopeId) {
  51. $this->stocks = [];
  52. } else {
  53. unset($this->stocks[$scopeId]);
  54. }
  55. }
  56. /**
  57. * @param int $productId
  58. * @param int $scopeId
  59. * @return StockItemInterface
  60. */
  61. public function getStockItem($productId, $scopeId)
  62. {
  63. return $this->stockItems[$productId][$scopeId] ?? null;
  64. }
  65. /**
  66. * @param int $productId
  67. * @param int $scopeId
  68. * @param StockItemInterface $value
  69. * @return void
  70. */
  71. public function setStockItem($productId, $scopeId, StockItemInterface $value)
  72. {
  73. $this->stockItems[$productId][$scopeId] = $value;
  74. }
  75. /**
  76. * @param int $productId
  77. * @param int|null $scopeId
  78. * @return void
  79. */
  80. public function removeStockItem($productId, $scopeId = null)
  81. {
  82. if (null === $scopeId) {
  83. unset($this->stockItems[$productId]);
  84. } else {
  85. unset($this->stockItems[$productId][$scopeId]);
  86. }
  87. }
  88. /**
  89. * @param int $productId
  90. * @param int $scopeId
  91. * @return StockStatusInterface
  92. */
  93. public function getStockStatus($productId, $scopeId)
  94. {
  95. return $this->stockStatuses[$productId][$scopeId] ?? null;
  96. }
  97. /**
  98. * @param int $productId
  99. * @param int $scopeId
  100. * @param StockStatusInterface $value
  101. * @return void
  102. */
  103. public function setStockStatus($productId, $scopeId, StockStatusInterface $value)
  104. {
  105. $this->stockStatuses[$productId][$scopeId] = $value;
  106. }
  107. /**
  108. * @param int $productId
  109. * @param int|null $scopeId
  110. * @return void
  111. */
  112. public function removeStockStatus($productId, $scopeId = null)
  113. {
  114. if (null === $scopeId) {
  115. unset($this->stockStatuses[$productId]);
  116. } else {
  117. unset($this->stockStatuses[$productId][$scopeId]);
  118. }
  119. }
  120. /**
  121. * Clear cached entities
  122. *
  123. * @return void
  124. */
  125. public function clean()
  126. {
  127. $this->stockItems = [];
  128. $this->stocks = [];
  129. $this->stockStatuses = [];
  130. }
  131. }