1
1
<?php
2
+
2
3
/**
3
4
* Copyright © Magento, Inc. All rights reserved.
4
5
* See COPYING.txt for license details.
5
6
*/
7
+
6
8
declare (strict_types=1 );
7
9
8
10
namespace Magento \StoreGraphQl \Controller \HttpHeaderProcessor ;
11
13
use Magento \Store \Model \StoreManagerInterface ;
12
14
use Magento \Framework \App \Http \Context as HttpContext ;
13
15
use Magento \Store \Api \StoreCookieManagerInterface ;
16
+ use Magento \Framework \App \ObjectManager ;
17
+ use Magento \Framework \Locale \ResolverInterface ;
18
+ use Psr \Log \LoggerInterface ;
14
19
15
20
/**
16
21
* Process the "Store" header entry
@@ -32,19 +37,35 @@ class StoreProcessor implements HttpHeaderProcessorInterface
32
37
*/
33
38
private $ storeCookieManager ;
34
39
40
+ /**
41
+ * @var ResolverInterface
42
+ */
43
+ private $ localeResolver ;
44
+
45
+ /**
46
+ * @var LoggerInterface
47
+ */
48
+ private $ logger ;
49
+
35
50
/**
36
51
* @param StoreManagerInterface $storeManager
37
52
* @param HttpContext $httpContext
38
53
* @param StoreCookieManagerInterface $storeCookieManager
54
+ * @param ResolverInterface $localeResolver
55
+ * @param LoggerInterface $logger
39
56
*/
40
57
public function __construct (
41
58
StoreManagerInterface $ storeManager ,
42
59
HttpContext $ httpContext ,
43
- StoreCookieManagerInterface $ storeCookieManager
60
+ StoreCookieManagerInterface $ storeCookieManager ,
61
+ ResolverInterface $ localeResolver = null ,
62
+ LoggerInterface $ logger = null
44
63
) {
45
64
$ this ->storeManager = $ storeManager ;
46
65
$ this ->httpContext = $ httpContext ;
47
66
$ this ->storeCookieManager = $ storeCookieManager ;
67
+ $ this ->localeResolver = $ localeResolver ?: ObjectManager::getInstance ()->get (ResolverInterface::class);
68
+ $ this ->logger = $ logger ?: ObjectManager::getInstance ()->get (LoggerInterface::class);
48
69
}
49
70
50
71
/**
@@ -55,12 +76,19 @@ public function __construct(
55
76
* @param string $headerValue
56
77
* @return void
57
78
*/
58
- public function processHeaderValue (string $ headerValue ) : void
79
+ public function processHeaderValue (string $ headerValue ): void
59
80
{
60
81
if (!empty ($ headerValue )) {
61
82
$ storeCode = ltrim (rtrim ($ headerValue ));
62
- $ this ->storeManager ->setCurrentStore ($ storeCode );
63
- $ this ->updateContext ($ storeCode );
83
+ try {
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
87
+ $ this ->storeManager ->setCurrentStore ($ storeCode );
88
+ $ this ->updateContext ($ storeCode );
89
+ } catch (\Exception $ e ) {
90
+ $ this ->logger ->error ($ e ->getMessage ());
91
+ }
64
92
} elseif (!$ this ->isAlreadySet ()) {
65
93
$ storeCode = $ this ->storeCookieManager ->getStoreCodeFromCookie ()
66
94
?: $ this ->storeManager ->getDefaultStoreView ()->getCode ();
@@ -75,7 +103,7 @@ public function processHeaderValue(string $headerValue) : void
75
103
* @param string $storeCode
76
104
* @return void
77
105
*/
78
- private function updateContext (string $ storeCode ) : void
106
+ private function updateContext (string $ storeCode ): void
79
107
{
80
108
$ this ->httpContext ->setValue (
81
109
StoreManagerInterface::CONTEXT_STORE ,
0 commit comments