11
11
use Magento \Framework \App \RequestInterface ;
12
12
use Magento \Framework \Exception \NoSuchEntityException ;
13
13
use Magento \Framework \Exception \NotFoundException ;
14
+ use Magento \Framework \Session \SessionManagerInterface ;
14
15
use Magento \Store \Api \Data \StoreInterface ;
15
16
use Magento \Store \Api \StoreCookieManagerInterface ;
17
+ use Magento \Store \Model \ScopeInterface ;
18
+ use Magento \Store \Model \StoreManager ;
16
19
use Magento \Store \Model \StoreManagerInterface ;
17
20
18
21
/**
21
24
class Context
22
25
{
23
26
/**
24
- * @var \Magento\Framework\Session\ SessionManagerInterface
27
+ * @var SessionManagerInterface
25
28
*/
26
29
protected $ session ;
27
30
28
31
/**
29
- * @var \Magento\Framework\App\Http\Context
32
+ * @var HttpContext
30
33
*/
31
34
protected $ httpContext ;
32
35
33
36
/**
34
- * @var \Magento\Store\Model\ StoreManagerInterface
37
+ * @var StoreManagerInterface
35
38
*/
36
39
protected $ storeManager ;
37
40
@@ -41,15 +44,15 @@ class Context
41
44
protected $ storeCookieManager ;
42
45
43
46
/**
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
47
50
* @param StoreCookieManagerInterface $storeCookieManager
48
51
*/
49
52
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 ,
53
56
StoreCookieManagerInterface $ storeCookieManager
54
57
) {
55
58
$ this ->session = $ session ;
@@ -78,41 +81,42 @@ public function beforeDispatch(
78
81
79
82
/** @var string|array|null $storeCode */
80
83
$ storeCode = $ request ->getParam (
81
- \ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ,
84
+ StoreManagerInterface::PARAM_NAME ,
82
85
$ this ->storeCookieManager ->getStoreCodeFromCookie ()
83
86
);
84
87
if (is_array ($ storeCode )) {
85
88
if (!isset ($ storeCode ['_data ' ]['code ' ])) {
86
- $ this ->processInvalidStoreRequested ();
89
+ $ this ->processInvalidStoreRequested ($ request );
87
90
}
88
91
$ storeCode = $ storeCode ['_data ' ]['code ' ];
89
92
}
90
93
if ($ storeCode === '' ) {
91
94
//Empty code - is an invalid code and it was given explicitly
92
95
//(the value would be null if the code wasn't found).
93
- $ this ->processInvalidStoreRequested ();
96
+ $ this ->processInvalidStoreRequested ($ request );
94
97
}
95
98
try {
96
99
$ currentStore = $ this ->storeManager ->getStore ($ storeCode );
100
+ $ this ->updateContext ($ request , $ currentStore );
97
101
} catch (NoSuchEntityException $ exception ) {
98
- $ this ->processInvalidStoreRequested ($ exception );
102
+ $ this ->processInvalidStoreRequested ($ request , $ exception );
99
103
}
100
-
101
- $ this ->updateContext ($ currentStore );
102
104
}
103
105
104
106
/**
105
107
* Take action in case of invalid store requested.
106
108
*
107
- * @param \Throwable|null $previousException
109
+ * @param RequestInterface $request
110
+ * @param NoSuchEntityException|null $previousException
108
111
* @return void
109
112
* @throws NotFoundException
110
113
*/
111
114
private function processInvalidStoreRequested (
112
- \Throwable $ previousException = null
115
+ RequestInterface $ request ,
116
+ NoSuchEntityException $ previousException = null
113
117
) {
114
118
$ store = $ this ->storeManager ->getStore ();
115
- $ this ->updateContext ($ store );
119
+ $ this ->updateContext ($ request , $ store );
116
120
117
121
throw new NotFoundException (
118
122
$ previousException
@@ -125,24 +129,34 @@ private function processInvalidStoreRequested(
125
129
/**
126
130
* Update context accordingly to the store found.
127
131
*
132
+ * @param RequestInterface $request
128
133
* @param StoreInterface $store
129
134
* @return void
130
135
* @throws \Magento\Framework\Exception\LocalizedException
131
136
*/
132
- private function updateContext (StoreInterface $ store )
137
+ private function updateContext (RequestInterface $ request , StoreInterface $ store )
133
138
{
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
+ }
134
152
$ this ->httpContext ->setValue (
135
153
StoreManagerInterface::CONTEXT_STORE ,
136
154
$ store ->getCode (),
137
- $ store -> isUseStoreInUrl () ? $ store -> getCode () : $ this -> storeManager -> getDefaultStoreView () ->getCode ()
155
+ $ defaultStore ->getCode ()
138
156
);
139
-
140
- /** @var StoreInterface $defaultStore */
141
- $ defaultStore = $ this ->storeManager ->getWebsite ()->getDefaultStore ();
142
157
$ this ->httpContext ->setValue (
143
158
HttpContext::CONTEXT_CURRENCY ,
144
- $ this ->session ->getCurrencyCode ()
145
- ?: $ store ->getDefaultCurrencyCode (),
159
+ $ this ->session ->getCurrencyCode () ?: $ store ->getDefaultCurrencyCode (),
146
160
$ defaultStore ->getDefaultCurrencyCode ()
147
161
);
148
162
}
0 commit comments