WebsiteRepository.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\Config;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. use Magento\Store\Model\ResourceModel\Website\CollectionFactory;
  11. /**
  12. * Information Expert in store websites handling
  13. */
  14. class WebsiteRepository implements \Magento\Store\Api\WebsiteRepositoryInterface
  15. {
  16. /**
  17. * @var WebsiteFactory
  18. */
  19. protected $factory;
  20. /**
  21. * @var CollectionFactory
  22. */
  23. protected $websiteCollectionFactory;
  24. /**
  25. * @var \Magento\Store\Api\Data\WebsiteInterface[]
  26. */
  27. protected $entities = [];
  28. /**
  29. * @var \Magento\Store\Api\Data\WebsiteInterface[]
  30. */
  31. protected $entitiesById = [];
  32. /**
  33. * @var bool
  34. */
  35. protected $allLoaded = false;
  36. /**
  37. * @var \Magento\Store\Api\Data\WebsiteInterface[]
  38. */
  39. protected $default;
  40. /**
  41. * @var Config
  42. */
  43. private $appConfig;
  44. /**
  45. * @param WebsiteFactory $factory
  46. * @param CollectionFactory $websiteCollectionFactory
  47. */
  48. public function __construct(
  49. WebsiteFactory $factory,
  50. CollectionFactory $websiteCollectionFactory
  51. ) {
  52. $this->factory = $factory;
  53. $this->websiteCollectionFactory = $websiteCollectionFactory;
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function get($code)
  59. {
  60. if (isset($this->entities[$code])) {
  61. return $this->entities[$code];
  62. }
  63. $websiteData = $this->getAppConfig()->get('scopes', "websites/$code", []);
  64. $website = $this->factory->create([
  65. 'data' => $websiteData
  66. ]);
  67. if ($website->getId() === null) {
  68. throw new NoSuchEntityException(
  69. __(
  70. sprintf(
  71. "The website with code %s that was requested wasn't found. Verify the website and try again.",
  72. $code
  73. )
  74. )
  75. );
  76. }
  77. $this->entities[$code] = $website;
  78. $this->entitiesById[$website->getId()] = $website;
  79. return $website;
  80. }
  81. /**
  82. * @inheritdoc
  83. */
  84. public function getById($id)
  85. {
  86. if (isset($this->entitiesById[$id])) {
  87. return $this->entitiesById[$id];
  88. }
  89. $websiteData = $this->getAppConfig()->get('scopes', "websites/$id", []);
  90. $website = $this->factory->create([
  91. 'data' => $websiteData
  92. ]);
  93. if ($website->getId() === null) {
  94. throw new NoSuchEntityException(
  95. __(
  96. sprintf(
  97. "The website with id %s that was requested wasn't found. Verify the website and try again.",
  98. $id
  99. )
  100. )
  101. );
  102. }
  103. $this->entities[$website->getCode()] = $website;
  104. $this->entitiesById[$id] = $website;
  105. return $website;
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function getList()
  111. {
  112. if (!$this->allLoaded) {
  113. $websites = $this->getAppConfig()->get('scopes', 'websites', []);
  114. foreach ($websites as $data) {
  115. $website = $this->factory->create([
  116. 'data' => $data
  117. ]);
  118. $this->entities[$website->getCode()] = $website;
  119. $this->entitiesById[$website->getId()] = $website;
  120. }
  121. $this->allLoaded = true;
  122. }
  123. return $this->entities;
  124. }
  125. /**
  126. * @inheritdoc
  127. */
  128. public function getDefault()
  129. {
  130. if (!$this->default) {
  131. foreach ($this->entities as $entity) {
  132. if ($entity->getIsDefault()) {
  133. $this->default = $entity;
  134. return $this->default;
  135. }
  136. }
  137. if (!$this->allLoaded) {
  138. $this->initDefaultWebsite();
  139. }
  140. if (!$this->default) {
  141. throw new \DomainException(__("The default website isn't defined. Set the website and try again."));
  142. }
  143. }
  144. return $this->default;
  145. }
  146. /**
  147. * @inheritdoc
  148. */
  149. public function clean()
  150. {
  151. $this->entities = [];
  152. $this->entitiesById = [];
  153. $this->default = null;
  154. $this->allLoaded = false;
  155. }
  156. /**
  157. * Retrieve application config.
  158. *
  159. * @deprecated 100.1.3
  160. * @return Config
  161. */
  162. private function getAppConfig()
  163. {
  164. if (!$this->appConfig) {
  165. $this->appConfig = ObjectManager::getInstance()->get(Config::class);
  166. }
  167. return $this->appConfig;
  168. }
  169. /**
  170. * Initialize default website.
  171. *
  172. * @return void
  173. */
  174. private function initDefaultWebsite()
  175. {
  176. $websites = (array) $this->getAppConfig()->get('scopes', 'websites', []);
  177. foreach ($websites as $data) {
  178. if (isset($data['is_default']) && $data['is_default'] == 1) {
  179. if ($this->default) {
  180. throw new \DomainException(
  181. __(
  182. 'The default website is invalid. '
  183. . 'Make sure no more than one default is defined and try again.'
  184. )
  185. );
  186. }
  187. $website = $this->factory->create([
  188. 'data' => $data
  189. ]);
  190. $this->default = $website;
  191. $this->entities[$this->default->getCode()] = $this->default;
  192. $this->entitiesById[$this->default->getId()] = $this->default;
  193. }
  194. }
  195. }
  196. }