StoreRepository.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\App\Config;
  10. /**
  11. * Information Expert in stores handling
  12. */
  13. class StoreRepository implements \Magento\Store\Api\StoreRepositoryInterface
  14. {
  15. /**
  16. * @var StoreFactory
  17. */
  18. protected $storeFactory;
  19. /**
  20. * @var \Magento\Store\Model\ResourceModel\Store\CollectionFactory
  21. */
  22. protected $storeCollectionFactory;
  23. /**
  24. * @var \Magento\Store\Api\Data\StoreInterface[]
  25. */
  26. protected $entities = [];
  27. /**
  28. * @var \Magento\Store\Api\Data\StoreInterface[]
  29. */
  30. protected $entitiesById = [];
  31. /**
  32. * @var bool
  33. */
  34. protected $allLoaded = false;
  35. /**
  36. * @var Config
  37. */
  38. private $appConfig;
  39. /**
  40. * @param StoreFactory $storeFactory
  41. * @param \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory
  42. */
  43. public function __construct(
  44. StoreFactory $storeFactory,
  45. \Magento\Store\Model\ResourceModel\Store\CollectionFactory $storeCollectionFactory
  46. ) {
  47. $this->storeFactory = $storeFactory;
  48. $this->storeCollectionFactory = $storeCollectionFactory;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function get($code)
  54. {
  55. if (isset($this->entities[$code])) {
  56. return $this->entities[$code];
  57. }
  58. $storeData = $this->getAppConfig()->get('scopes', "stores/$code", []);
  59. $store = $this->storeFactory->create([
  60. 'data' => $storeData
  61. ]);
  62. if ($store->getId() === null) {
  63. throw new NoSuchEntityException(
  64. __("The store that was requested wasn't found. Verify the store and try again.")
  65. );
  66. }
  67. $this->entities[$code] = $store;
  68. $this->entitiesById[$store->getId()] = $store;
  69. return $store;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getActiveStoreByCode($code)
  75. {
  76. $store = $this->get($code);
  77. if (!$store->isActive()) {
  78. throw new StoreIsInactiveException();
  79. }
  80. return $store;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getById($id)
  86. {
  87. if (isset($this->entitiesById[$id])) {
  88. return $this->entitiesById[$id];
  89. }
  90. $storeData = $this->getAppConfig()->get('scopes', "stores/$id", []);
  91. $store = $this->storeFactory->create([
  92. 'data' => $storeData
  93. ]);
  94. if ($store->getId() === null) {
  95. throw new NoSuchEntityException(
  96. __("The store that was requested wasn't found. Verify the store and try again.")
  97. );
  98. }
  99. $this->entitiesById[$id] = $store;
  100. $this->entities[$store->getCode()] = $store;
  101. return $store;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getActiveStoreById($id)
  107. {
  108. $store = $this->getById($id);
  109. if (!$store->isActive()) {
  110. throw new StoreIsInactiveException();
  111. }
  112. return $store;
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getList()
  118. {
  119. if ($this->allLoaded) {
  120. return $this->entities;
  121. }
  122. $stores = $this->getAppConfig()->get('scopes', "stores", []);
  123. foreach ($stores as $data) {
  124. $store = $this->storeFactory->create([
  125. 'data' => $data
  126. ]);
  127. $this->entities[$store->getCode()] = $store;
  128. $this->entitiesById[$store->getId()] = $store;
  129. }
  130. $this->allLoaded = true;
  131. return $this->entities;
  132. }
  133. /**
  134. * Retrieve application config.
  135. *
  136. * @deprecated 100.1.3
  137. * @return Config
  138. */
  139. private function getAppConfig()
  140. {
  141. if (!$this->appConfig) {
  142. $this->appConfig = ObjectManager::getInstance()->get(Config::class);
  143. }
  144. return $this->appConfig;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function clean()
  150. {
  151. $this->entities = [];
  152. $this->entitiesById = [];
  153. $this->allLoaded = false;
  154. }
  155. }