StoreResolver.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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\Store\Model;
  8. /**
  9. * Class used to resolve store from url path or get parameters or cookie.
  10. */
  11. class StoreResolver implements \Magento\Store\Api\StoreResolverInterface
  12. {
  13. /**
  14. * Cache tag
  15. */
  16. const CACHE_TAG = 'store_relations';
  17. /**
  18. * @var \Magento\Store\Api\StoreRepositoryInterface
  19. */
  20. protected $storeRepository;
  21. /**
  22. * @var \Magento\Store\Api\StoreCookieManagerInterface
  23. */
  24. protected $storeCookieManager;
  25. /**
  26. * @deprecated 101.0.0
  27. */
  28. protected $cache;
  29. /**
  30. * @deprecated 101.0.0
  31. */
  32. protected $readerList;
  33. /**
  34. * @var string
  35. */
  36. protected $runMode;
  37. /**
  38. * @var string
  39. */
  40. protected $scopeCode;
  41. /**
  42. * @var \Magento\Framework\App\Request\Http
  43. */
  44. protected $request;
  45. /**
  46. * @var StoresData
  47. */
  48. private $storesData;
  49. /**
  50. * @var \Magento\Store\App\Request\StorePathInfoValidator
  51. */
  52. private $storePathInfoValidator;
  53. /**
  54. * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
  55. * @param \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager
  56. * @param \Magento\Framework\App\Request\Http $request
  57. * @param \Magento\Store\Model\StoresData $storesData
  58. * @param \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator
  59. * @param string|null $runMode
  60. * @param string|null $scopeCode
  61. */
  62. public function __construct(
  63. \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
  64. \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager,
  65. \Magento\Framework\App\Request\Http $request,
  66. \Magento\Store\Model\StoresData $storesData,
  67. \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator,
  68. $runMode = ScopeInterface::SCOPE_STORE,
  69. $scopeCode = null
  70. ) {
  71. $this->storeRepository = $storeRepository;
  72. $this->storeCookieManager = $storeCookieManager;
  73. $this->request = $request;
  74. $this->storePathInfoValidator = $storePathInfoValidator;
  75. $this->storesData = $storesData;
  76. $this->runMode = $scopeCode ? $runMode : ScopeInterface::SCOPE_WEBSITE;
  77. $this->scopeCode = $scopeCode;
  78. }
  79. /**
  80. * @inheritdoc
  81. */
  82. public function getCurrentStoreId()
  83. {
  84. list($stores, $defaultStoreId) = $this->getStoresData();
  85. $storeCode = $this->storePathInfoValidator->getValidStoreCode($this->request);
  86. if (!$storeCode) {
  87. $storeCode = $this->request->getParam(
  88. \Magento\Store\Model\StoreManagerInterface::PARAM_NAME,
  89. $this->storeCookieManager->getStoreCodeFromCookie()
  90. );
  91. }
  92. if (is_array($storeCode)) {
  93. if (!isset($storeCode['_data']['code'])) {
  94. throw new \InvalidArgumentException(__('Invalid store parameter.'));
  95. }
  96. $storeCode = $storeCode['_data']['code'];
  97. }
  98. if ($storeCode) {
  99. try {
  100. $store = $this->getRequestedStoreByCode($storeCode);
  101. } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
  102. $store = $this->getDefaultStoreById($defaultStoreId);
  103. }
  104. if (!in_array($store->getId(), $stores)) {
  105. $store = $this->getDefaultStoreById($defaultStoreId);
  106. }
  107. } else {
  108. $store = $this->getDefaultStoreById($defaultStoreId);
  109. }
  110. return $store->getId();
  111. }
  112. /**
  113. * Get stores data
  114. *
  115. * @return array
  116. */
  117. protected function getStoresData() : array
  118. {
  119. return $this->storesData->getStoresData($this->runMode, $this->scopeCode);
  120. }
  121. /**
  122. * Read stores data. First element is allowed store ids, second is default store id
  123. *
  124. * @return array
  125. * @deprecated 101.0.0
  126. * @see \Magento\Store\Model\StoreResolver::getStoresData
  127. */
  128. protected function readStoresData() : array
  129. {
  130. return $this->getStoresData();
  131. }
  132. /**
  133. * Retrieve active store by code
  134. *
  135. * @param string $storeCode
  136. * @return \Magento\Store\Api\Data\StoreInterface
  137. * @throws \Magento\Framework\Exception\NoSuchEntityException
  138. */
  139. protected function getRequestedStoreByCode($storeCode) : \Magento\Store\Api\Data\StoreInterface
  140. {
  141. try {
  142. $store = $this->storeRepository->getActiveStoreByCode($storeCode);
  143. } catch (StoreIsInactiveException $e) {
  144. throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested store is inactive'));
  145. }
  146. return $store;
  147. }
  148. /**
  149. * Retrieve active store by code
  150. *
  151. * @param int $id
  152. * @return \Magento\Store\Api\Data\StoreInterface
  153. * @throws \Magento\Framework\Exception\NoSuchEntityException
  154. */
  155. protected function getDefaultStoreById($id) : \Magento\Store\Api\Data\StoreInterface
  156. {
  157. try {
  158. $store = $this->storeRepository->getActiveStoreById($id);
  159. } catch (StoreIsInactiveException $e) {
  160. throw new \Magento\Framework\Exception\NoSuchEntityException(__('Default store is inactive'));
  161. }
  162. return $store;
  163. }
  164. }