15
15
use Magento \Store \Api \StoreCookieManagerInterface ;
16
16
use Magento \Framework \App \ObjectManager ;
17
17
use Magento \Framework \Locale \ResolverInterface ;
18
+ use Psr \Log \LoggerInterface ;
18
19
19
20
/**
20
21
* Process the "Store" header entry
@@ -41,22 +42,30 @@ class StoreProcessor implements HttpHeaderProcessorInterface
41
42
*/
42
43
private $ localeResolver ;
43
44
45
+ /**
46
+ * @var LoggerInterface
47
+ */
48
+ private $ logger ;
49
+
44
50
/**
45
51
* @param StoreManagerInterface $storeManager
46
52
* @param HttpContext $httpContext
47
53
* @param StoreCookieManagerInterface $storeCookieManager
48
54
* @param ResolverInterface $localeResolver
55
+ * @param LoggerInterface $logger
49
56
*/
50
57
public function __construct (
51
58
StoreManagerInterface $ storeManager ,
52
59
HttpContext $ httpContext ,
53
60
StoreCookieManagerInterface $ storeCookieManager ,
54
- ResolverInterface $ localeResolver = null
61
+ ResolverInterface $ localeResolver = null ,
62
+ LoggerInterface $ logger = null
55
63
) {
56
64
$ this ->storeManager = $ storeManager ;
57
65
$ this ->httpContext = $ httpContext ;
58
66
$ this ->storeCookieManager = $ storeCookieManager ;
59
67
$ this ->localeResolver = $ localeResolver ?: ObjectManager::getInstance ()->get (ResolverInterface::class);
68
+ $ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
60
69
}
61
70
62
71
/**
@@ -71,10 +80,14 @@ public function processHeaderValue(string $headerValue): void
71
80
{
72
81
if (!empty ($ headerValue )) {
73
82
$ storeCode = ltrim (rtrim ($ headerValue ));
74
- if ( $ this -> isStoreValid ( $ storeCode )) {
83
+ try {
75
84
$ this ->localeResolver ->emulate ($ this ->storeManager ->getStore ($ storeCode )->getId ());
85
+ // $this->storeManager->getStore($storeCode) throws error with non existing stores
86
+ // and logged in the catch
76
87
$ this ->storeManager ->setCurrentStore ($ storeCode );
77
88
$ this ->updateContext ($ storeCode );
89
+ } catch (\Exception $ e ) {
90
+ $ this ->logger ->error ($ e ->getMessage ());
78
91
}
79
92
} elseif (!$ this ->isAlreadySet ()) {
80
93
$ storeCode = $ this ->storeCookieManager ->getStoreCodeFromCookie ()
@@ -110,19 +123,4 @@ private function isAlreadySet(): bool
110
123
111
124
return $ this ->httpContext ->getValue ($ storeKey ) !== null ;
112
125
}
113
-
114
- /**
115
- * Check if provided store code exist
116
- *
117
- * @param string $storeCode
118
- * @return bool
119
- */
120
- private function isStoreValid (string $ storeCode ): bool
121
- {
122
- $ stores = $ this ->storeManager ->getStores (true , true );
123
- if (isset ($ stores [$ storeCode ])) {
124
- return true ;
125
- }
126
- return false ;
127
- }
128
126
}
0 commit comments