1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2015 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
+
6
7
declare (strict_types=1 );
7
8
8
9
namespace Magento \Store \Model ;
9
10
11
+ use Magento \Framework \App \ObjectManager ;
12
+ use Magento \Framework \App \Request \Http ;
13
+ use Magento \Framework \Cache \FrontendInterface ;
14
+ use Magento \Framework \Exception \NoSuchEntityException ;
15
+ use Magento \Store \Api \Data \StoreInterface ;
16
+ use Magento \Store \Api \StoreCookieManagerInterface ;
17
+ use Magento \Store \Api \StoreRepositoryInterface ;
18
+ use Magento \Store \Api \StoreResolverInterface ;
19
+ use Magento \Store \App \Request \StorePathInfoValidator ;
20
+ use Magento \Framework \Stdlib \CookieManagerInterface ;
21
+
10
22
/**
11
23
* Class used to resolve store from url path or get parameters or cookie.
24
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25
+ * @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
12
26
*/
13
- class StoreResolver implements \ Magento \ Store \ Api \ StoreResolverInterface
27
+ class StoreResolver implements StoreResolverInterface
14
28
{
15
29
/**
16
30
* Cache tag
17
31
*/
18
- const CACHE_TAG = 'store_relations ' ;
32
+ public const CACHE_TAG = 'store_relations ' ;
19
33
20
34
/**
21
- * @var \Magento\Store\Api\ StoreRepositoryInterface
35
+ * @var StoreRepositoryInterface
22
36
*/
23
37
protected $ storeRepository ;
24
38
25
39
/**
26
- * @var \Magento\Store\Api\ StoreCookieManagerInterface
40
+ * @var StoreCookieManagerInterface
27
41
*/
28
42
protected $ storeCookieManager ;
29
43
30
44
/**
31
45
* @deprecated 101.0.0
46
+ * @see No longer needed
47
+ *
48
+ * @var FrontendInterface
32
49
*/
33
50
protected $ cache ;
34
51
35
52
/**
36
53
* @deprecated 101.0.0
54
+ * @see No longer needed
55
+ *
56
+ * @var StoreResolver\ReaderList
37
57
*/
38
58
protected $ readerList ;
39
59
@@ -48,7 +68,7 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface
48
68
protected $ scopeCode ;
49
69
50
70
/**
51
- * @var \Magento\Framework\App\Request\ Http
71
+ * @var Http
52
72
*/
53
73
protected $ request ;
54
74
@@ -58,27 +78,34 @@ class StoreResolver implements \Magento\Store\Api\StoreResolverInterface
58
78
private $ storesData ;
59
79
60
80
/**
61
- * @var \Magento\Store\App\Request\ StorePathInfoValidator
81
+ * @var StorePathInfoValidator
62
82
*/
63
83
private $ storePathInfoValidator ;
64
84
65
85
/**
66
- * @param \Magento\Store\Api\StoreRepositoryInterface $storeRepository
67
- * @param \Magento\Store\Api\StoreCookieManagerInterface $storeCookieManager
68
- * @param \Magento\Framework\App\Request\Http $request
69
- * @param \Magento\Store\Model\StoresData $storesData
70
- * @param \Magento\Store\App\Request\StorePathInfoValidator $storePathInfoValidator
71
- * @param string|null $runMode
86
+ * @var CookieManagerInterface
87
+ */
88
+ private $ cookieManagerInterface ;
89
+
90
+ /**
91
+ * @param StoreRepositoryInterface $storeRepository
92
+ * @param StoreCookieManagerInterface $storeCookieManager
93
+ * @param Http $request
94
+ * @param StoresData $storesData
95
+ * @param StorePathInfoValidator $storePathInfoValidator
96
+ * @param string $runMode
72
97
* @param string|null $scopeCode
98
+ * @param CookieManagerInterface|null $cookieManagerInterface
73
99
*/
74
100
public function __construct (
75
- \ Magento \ Store \ Api \ StoreRepositoryInterface $ storeRepository ,
76
- \ Magento \ Store \ Api \ StoreCookieManagerInterface $ storeCookieManager ,
77
- \ Magento \ Framework \ App \ Request \ Http $ request ,
78
- \ Magento \ Store \ Model \ StoresData $ storesData ,
79
- \ Magento \ Store \ App \ Request \ StorePathInfoValidator $ storePathInfoValidator ,
101
+ StoreRepositoryInterface $ storeRepository ,
102
+ StoreCookieManagerInterface $ storeCookieManager ,
103
+ Http $ request ,
104
+ StoresData $ storesData ,
105
+ StorePathInfoValidator $ storePathInfoValidator ,
80
106
$ runMode = ScopeInterface::SCOPE_STORE ,
81
- $ scopeCode = null
107
+ $ scopeCode = null ,
108
+ ?CookieManagerInterface $ cookieManagerInterface = null
82
109
) {
83
110
$ this ->storeRepository = $ storeRepository ;
84
111
$ this ->storeCookieManager = $ storeCookieManager ;
@@ -87,6 +114,8 @@ public function __construct(
87
114
$ this ->storesData = $ storesData ;
88
115
$ this ->runMode = $ scopeCode ? $ runMode : ScopeInterface::SCOPE_WEBSITE ;
89
116
$ this ->scopeCode = $ scopeCode ;
117
+ $ this ->cookieManagerInterface = $ cookieManagerInterface ?:
118
+ ObjectManager::getInstance ()->get (CookieManagerInterface::class);
90
119
}
91
120
92
121
/**
@@ -95,12 +124,11 @@ public function __construct(
95
124
public function getCurrentStoreId ()
96
125
{
97
126
list ($ stores , $ defaultStoreId ) = $ this ->getStoresData ();
98
-
99
127
$ storeCode = $ this ->storePathInfoValidator ->getValidStoreCode ($ this ->request );
100
128
101
129
if (!$ storeCode ) {
102
130
$ storeCode = $ this ->request ->getParam (
103
- \ Magento \ Store \ Model \ StoreManagerInterface::PARAM_NAME ,
131
+ StoreManagerInterface::PARAM_NAME ,
104
132
$ this ->storeCookieManager ->getStoreCodeFromCookie ()
105
133
);
106
134
}
@@ -115,7 +143,9 @@ public function getCurrentStoreId()
115
143
if ($ storeCode ) {
116
144
try {
117
145
$ store = $ this ->getRequestedStoreByCode ($ storeCode );
118
- } catch (\Magento \Framework \Exception \NoSuchEntityException $ e ) {
146
+ } catch (NoSuchEntityException $ e ) {
147
+ $ this ->request ->setQueryValue (StoreManagerInterface::PARAM_NAME );
148
+ $ this ->cookieManagerInterface ->deleteCookie (StoreCookieManager::COOKIE_NAME );
119
149
$ store = $ this ->getDefaultStoreById ($ defaultStoreId );
120
150
}
121
151
@@ -143,7 +173,7 @@ protected function getStoresData() : array
143
173
*
144
174
* @return array
145
175
* @deprecated 101.0.0
146
- * @see \Magento\Store\Model\ StoreResolver::getStoresData
176
+ * @see StoreResolver::getStoresData
147
177
*/
148
178
protected function readStoresData () : array
149
179
{
@@ -154,15 +184,15 @@ protected function readStoresData() : array
154
184
* Retrieve active store by code
155
185
*
156
186
* @param string $storeCode
157
- * @return \Magento\Store\Api\Data\ StoreInterface
158
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
187
+ * @return StoreInterface
188
+ * @throws NoSuchEntityException
159
189
*/
160
- protected function getRequestedStoreByCode ($ storeCode ) : \ Magento \ Store \ Api \ Data \ StoreInterface
190
+ protected function getRequestedStoreByCode ($ storeCode ) : StoreInterface
161
191
{
162
192
try {
163
193
$ store = $ this ->storeRepository ->getActiveStoreByCode ($ storeCode );
164
194
} catch (StoreIsInactiveException $ e ) {
165
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (__ ('Requested store is inactive ' ));
195
+ throw new NoSuchEntityException (__ ('Requested store is inactive ' ));
166
196
}
167
197
168
198
return $ store ;
@@ -172,15 +202,15 @@ protected function getRequestedStoreByCode($storeCode) : \Magento\Store\Api\Data
172
202
* Retrieve active store by code
173
203
*
174
204
* @param int $id
175
- * @return \Magento\Store\Api\Data\ StoreInterface
176
- * @throws \Magento\Framework\Exception\ NoSuchEntityException
205
+ * @return StoreInterface
206
+ * @throws NoSuchEntityException
177
207
*/
178
- protected function getDefaultStoreById ($ id ) : \ Magento \ Store \ Api \ Data \ StoreInterface
208
+ protected function getDefaultStoreById ($ id ) : StoreInterface
179
209
{
180
210
try {
181
211
$ store = $ this ->storeRepository ->getActiveStoreById ($ id );
182
212
} catch (StoreIsInactiveException $ e ) {
183
- throw new \ Magento \ Framework \ Exception \ NoSuchEntityException (__ ('Default store is inactive ' ));
213
+ throw new NoSuchEntityException (__ ('Default store is inactive ' ));
184
214
}
185
215
186
216
return $ store ;
0 commit comments