Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StoreResolver.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
8 namespace Magento\Store\Model;
9 
14 {
18  const CACHE_TAG = 'store_relations';
19 
23  protected $storeRepository;
24 
29 
33  protected $cache;
34 
38  protected $readerList;
39 
43  protected $runMode;
44 
48  protected $scopeCode;
49 
53  protected $request;
54 
58  private $storesData;
59 
63  private $storePathInfoValidator;
64 
74  public function __construct(
75  \Magento\Store\Api\StoreRepositoryInterface $storeRepository,
77  \Magento\Framework\App\Request\Http $request,
78  \Magento\Store\Model\StoresData $storesData,
79  \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator,
81  $scopeCode = null
82  ) {
83  $this->storeRepository = $storeRepository;
84  $this->storeCookieManager = $storeCookieManager;
85  $this->request = $request;
86  $this->storePathInfoValidator = $storePathInfoValidator;
87  $this->storesData = $storesData;
89  $this->scopeCode = $scopeCode;
90  }
91 
95  public function getCurrentStoreId()
96  {
97  list($stores, $defaultStoreId) = $this->getStoresData();
98 
99  $storeCode = $this->storePathInfoValidator->getValidStoreCode($this->request);
100 
101  if (!$storeCode) {
102  $storeCode = $this->request->getParam(
104  $this->storeCookieManager->getStoreCodeFromCookie()
105  );
106  }
107 
108  if (is_array($storeCode)) {
109  if (!isset($storeCode['_data']['code'])) {
110  throw new \InvalidArgumentException(__('Invalid store parameter.'));
111  }
112  $storeCode = $storeCode['_data']['code'];
113  }
114 
115  if ($storeCode) {
116  try {
118  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
119  $store = $this->getDefaultStoreById($defaultStoreId);
120  }
121 
122  if (!in_array($store->getId(), $stores)) {
123  $store = $this->getDefaultStoreById($defaultStoreId);
124  }
125  } else {
126  $store = $this->getDefaultStoreById($defaultStoreId);
127  }
128  return $store->getId();
129  }
130 
136  protected function getStoresData() : array
137  {
138  return $this->storesData->getStoresData($this->runMode, $this->scopeCode);
139  }
140 
148  protected function readStoresData() : array
149  {
150  return $this->getStoresData();
151  }
152 
161  {
162  try {
163  $store = $this->storeRepository->getActiveStoreByCode($storeCode);
164  } catch (StoreIsInactiveException $e) {
165  throw new \Magento\Framework\Exception\NoSuchEntityException(__('Requested store is inactive'));
166  }
167 
168  return $store;
169  }
170 
179  {
180  try {
181  $store = $this->storeRepository->getActiveStoreById($id);
182  } catch (StoreIsInactiveException $e) {
183  throw new \Magento\Framework\Exception\NoSuchEntityException(__('Default store is inactive'));
184  }
185 
186  return $store;
187  }
188 }
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
__construct(\Magento\Store\Api\StoreRepositoryInterface $storeRepository, \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager, \Magento\Framework\App\Request\Http $request, \Magento\Store\Model\StoresData $storesData, \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator, $runMode=ScopeInterface::SCOPE_STORE, $scopeCode=null)
$storeCode
Definition: indexer.php:15