1111use Magento \Framework \App \RequestInterface ;
1212use Magento \Framework \Exception \NoSuchEntityException ;
1313use Magento \Framework \Exception \NotFoundException ;
14+ use Magento \Framework \Session \SessionManagerInterface ;
1415use Magento \Store \Api \Data \StoreInterface ;
1516use Magento \Store \Api \StoreCookieManagerInterface ;
17+ use Magento \Store \Model \ScopeInterface ;
18+ use Magento \Store \Model \StoreManager ;
1619use Magento \Store \Model \StoreManagerInterface ;
1720
1821/**
2124class Context
2225{
2326 /**
24- * @var \Magento\Framework\Session\ SessionManagerInterface
27+ * @var SessionManagerInterface
2528 */
2629 protected $ session ;
2730
2831 /**
29- * @var \Magento\Framework\App\Http\Context
32+ * @var HttpContext
3033 */
3134 protected $ httpContext ;
3235
3336 /**
34- * @var \Magento\Store\Model\ StoreManagerInterface
37+ * @var StoreManagerInterface
3538 */
3639 protected $ storeManager ;
3740
@@ -41,15 +44,15 @@ class Context
4144 protected $ storeCookieManager ;
4245
4346 /**
44- * @param \Magento\Framework\Session\ SessionManagerInterface $session
45- * @param \Magento\Framework\App\Http\Context $httpContext
46- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
47+ * @param SessionManagerInterface $session
48+ * @param HttpContext $httpContext
49+ * @param StoreManagerInterface $storeManager
4750 * @param StoreCookieManagerInterface $storeCookieManager
4851 */
4952 public function __construct (
50- \ Magento \ Framework \ Session \ SessionManagerInterface $ session ,
51- \ Magento \ Framework \ App \ Http \ Context $ httpContext ,
52- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
53+ SessionManagerInterface $ session ,
54+ HttpContext $ httpContext ,
55+ StoreManagerInterface $ storeManager ,
5356 StoreCookieManagerInterface $ storeCookieManager
5457 ) {
5558 $ this ->session = $ session ;
@@ -78,41 +81,42 @@ public function beforeDispatch(
7881
7982 /** @var string|array|null $storeCode */
8083 $ storeCode = $ request ->getParam (
81- \ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ,
84+ StoreManagerInterface::PARAM_NAME ,
8285 $ this ->storeCookieManager ->getStoreCodeFromCookie ()
8386 );
8487 if (is_array ($ storeCode )) {
8588 if (!isset ($ storeCode ['_data ' ]['code ' ])) {
86- $ this ->processInvalidStoreRequested ();
89+ $ this ->processInvalidStoreRequested ($ request );
8790 }
8891 $ storeCode = $ storeCode ['_data ' ]['code ' ];
8992 }
9093 if ($ storeCode === '' ) {
9194 //Empty code - is an invalid code and it was given explicitly
9295 //(the value would be null if the code wasn't found).
93- $ this ->processInvalidStoreRequested ();
96+ $ this ->processInvalidStoreRequested ($ request );
9497 }
9598 try {
9699 $ currentStore = $ this ->storeManager ->getStore ($ storeCode );
100+ $ this ->updateContext ($ request , $ currentStore );
97101 } catch (NoSuchEntityException $ exception ) {
98- $ this ->processInvalidStoreRequested ($ exception );
102+ $ this ->processInvalidStoreRequested ($ request , $ exception );
99103 }
100-
101- $ this ->updateContext ($ currentStore );
102104 }
103105
104106 /**
105107 * Take action in case of invalid store requested.
106108 *
107- * @param \Throwable|null $previousException
109+ * @param RequestInterface $request
110+ * @param NoSuchEntityException|null $previousException
108111 * @return void
109112 * @throws NotFoundException
110113 */
111114 private function processInvalidStoreRequested (
112- \Throwable $ previousException = null
115+ RequestInterface $ request ,
116+ NoSuchEntityException $ previousException = null
113117 ) {
114118 $ store = $ this ->storeManager ->getStore ();
115- $ this ->updateContext ($ store );
119+ $ this ->updateContext ($ request , $ store );
116120
117121 throw new NotFoundException (
118122 $ previousException
@@ -125,24 +129,34 @@ private function processInvalidStoreRequested(
125129 /**
126130 * Update context accordingly to the store found.
127131 *
132+ * @param RequestInterface $request
128133 * @param StoreInterface $store
129134 * @return void
130135 * @throws \Magento\Framework\Exception\LocalizedException
131136 */
132- private function updateContext (StoreInterface $ store )
137+ private function updateContext (RequestInterface $ request , StoreInterface $ store )
133138 {
139+ switch (true ) {
140+ case $ store ->isUseStoreInUrl ():
141+ $ defaultStore = $ store ;
142+ break ;
143+ case ScopeInterface::SCOPE_STORE == $ request ->getServerValue (StoreManager::PARAM_RUN_TYPE ):
144+ $ defaultStoreCode = $ request ->getServerValue (StoreManager::PARAM_RUN_CODE );
145+ $ defaultStore = $ this ->storeManager ->getStore ($ defaultStoreCode );
146+ break ;
147+ default :
148+ $ defaultStoreCode = $ this ->storeManager ->getDefaultStoreView ()->getCode ();
149+ $ defaultStore = $ this ->storeManager ->getStore ($ defaultStoreCode );
150+ break ;
151+ }
134152 $ this ->httpContext ->setValue (
135153 StoreManagerInterface::CONTEXT_STORE ,
136154 $ store ->getCode (),
137- $ store -> isUseStoreInUrl () ? $ store -> getCode () : $ this -> storeManager -> getDefaultStoreView () ->getCode ()
155+ $ defaultStore ->getCode ()
138156 );
139-
140- /** @var StoreInterface $defaultStore */
141- $ defaultStore = $ this ->storeManager ->getWebsite ()->getDefaultStore ();
142157 $ this ->httpContext ->setValue (
143158 HttpContext::CONTEXT_CURRENCY ,
144- $ this ->session ->getCurrencyCode ()
145- ?: $ store ->getDefaultCurrencyCode (),
159+ $ this ->session ->getCurrencyCode () ?: $ store ->getDefaultCurrencyCode (),
146160 $ defaultStore ->getDefaultCurrencyCode ()
147161 );
148162 }
0 commit comments