7
7
8
8
namespace Magento \PageBuilder \Model \Stage ;
9
9
10
+ use Magento \Framework \App \Area ;
11
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
12
+ use Magento \Framework \App \State ;
13
+ use Magento \Framework \View \Design \Theme \ThemeProviderInterface ;
14
+ use Magento \Framework \View \DesignInterface ;
15
+ use Magento \Store \Model \App \Emulation ;
16
+ use Magento \Store \Model \StoreManagerInterface ;
17
+
10
18
/**
11
19
* Handle placing Magento into Page Builder Preview mode and emulating the store front
12
20
*/
13
21
class Preview
14
22
{
15
23
/**
16
- * @var \Magento\Store\Model\App\ Emulation
24
+ * @var Emulation
17
25
*/
18
26
private $ emulation ;
19
27
20
28
/**
21
- * @var \Magento\Framework\App\ State
29
+ * @var State
22
30
*/
23
31
private $ appState ;
24
32
25
33
/**
26
- * @var \Magento\Framework\View\ DesignInterface
34
+ * @var DesignInterface
27
35
*/
28
36
private $ design ;
29
37
30
38
/**
31
- * @var \Magento\Framework\View\Design\Theme\ ThemeProviderInterface
39
+ * @var ThemeProviderInterface
32
40
*/
33
41
private $ themeProvider ;
34
42
35
43
/**
36
- * @var \Magento\Store\Model\ StoreManagerInterface
44
+ * @var StoreManagerInterface
37
45
*/
38
46
private $ storeManager ;
39
47
40
48
/**
41
- * @var \Magento\Framework\App\Config\ ScopeConfigInterface
49
+ * @var ScopeConfigInterface
42
50
*/
43
51
private $ scopeConfig ;
44
52
@@ -49,20 +57,20 @@ class Preview
49
57
50
58
/**
51
59
* Preview constructor.
52
- * @param \Magento\Store\Model\App\ Emulation $emulation
53
- * @param \Magento\Framework\App\ State $appState
54
- * @param \Magento\Framework\View\ DesignInterface $design
55
- * @param \Magento\Framework\View\Design\Theme\ ThemeProviderInterface $themeProvider
56
- * @param \Magento\Store\Model\ StoreManagerInterface $storeManager
57
- * @param \Magento\Framework\App\Config\ ScopeConfigInterface $scopeConfig
60
+ * @param Emulation $emulation
61
+ * @param State $appState
62
+ * @param DesignInterface $design
63
+ * @param ThemeProviderInterface $themeProvider
64
+ * @param StoreManagerInterface $storeManager
65
+ * @param ScopeConfigInterface $scopeConfig
58
66
*/
59
67
public function __construct (
60
- \ Magento \ Store \ Model \ App \ Emulation $ emulation ,
61
- \ Magento \ Framework \ App \ State $ appState ,
62
- \ Magento \ Framework \ View \ DesignInterface $ design ,
63
- \ Magento \ Framework \ View \ Design \ Theme \ ThemeProviderInterface $ themeProvider ,
64
- \ Magento \ Store \ Model \ StoreManagerInterface $ storeManager ,
65
- \ Magento \ Framework \ App \ Config \ ScopeConfigInterface $ scopeConfig
68
+ Emulation $ emulation ,
69
+ State $ appState ,
70
+ DesignInterface $ design ,
71
+ ThemeProviderInterface $ themeProvider ,
72
+ StoreManagerInterface $ storeManager ,
73
+ ScopeConfigInterface $ scopeConfig
66
74
) {
67
75
$ this ->emulation = $ emulation ;
68
76
$ this ->appState = $ appState ;
@@ -79,7 +87,7 @@ public function __construct(
79
87
*/
80
88
public function getPreviewArea () : string
81
89
{
82
- return \ Magento \ Framework \ App \ Area::AREA_FRONTEND ;
90
+ return Area::AREA_FRONTEND ;
83
91
}
84
92
85
93
/**
@@ -95,16 +103,7 @@ public function startPreviewMode($callback, $storeId = null)
95
103
$ this ->isPreview = true ;
96
104
97
105
if (!$ storeId ) {
98
- $ store = $ this ->storeManager ->getDefaultStoreView ();
99
- if ($ store ) {
100
- $ storeId = $ store ->getId ();
101
- } else {
102
- $ stores = $ this ->storeManager ->getStores ();
103
- if (count ($ stores )) {
104
- $ store = array_shift ($ stores );
105
- $ storeId = $ store ->getId ();
106
- }
107
- }
106
+ $ storeId = $ this ->getStoreId ();
108
107
}
109
108
$ this ->emulation ->startEnvironmentEmulation ($ storeId );
110
109
@@ -140,4 +139,26 @@ public function isPreviewMode() : bool
140
139
{
141
140
return $ this ->isPreview ;
142
141
}
142
+
143
+ /**
144
+ * Returns store id by default store view or store id from the available store if default store view is null
145
+ *
146
+ * @return int|null
147
+ */
148
+ private function getStoreId (): ?int
149
+ {
150
+ $ storeId = null ;
151
+ $ store = $ this ->storeManager ->getDefaultStoreView ();
152
+ if ($ store ) {
153
+ $ storeId = $ store ->getId ();
154
+ } else {
155
+ $ stores = $ this ->storeManager ->getStores ();
156
+ if (count ($ stores )) {
157
+ $ store = array_shift ($ stores );
158
+ $ storeId = $ store ->getId ();
159
+ }
160
+ }
161
+
162
+ return $ storeId ;
163
+ }
143
164
}
0 commit comments