18
18
namespace Meta \Catalog \Block \Adminhtml ;
19
19
20
20
use Exception ;
21
+ use Magento \Store \Api \Data \StoreInterface ;
21
22
use Meta \BusinessExtension \Helper \FBEHelper ;
22
23
use Meta \BusinessExtension \Helper \GraphAPIAdapter ;
23
24
use Meta \BusinessExtension \Model \System \Config as SystemConfig ;
27
28
use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
28
29
use Magento \Backend \Block \Template ;
29
30
use Psr \Log \LoggerInterface ;
31
+ use Magento \Store \Api \StoreRepositoryInterface ;
30
32
31
33
/**
32
34
* @api
@@ -38,16 +40,6 @@ class Diagnostics extends Template
38
40
*/
39
41
private $ fbeHelper ;
40
42
41
- /**
42
- * @var int
43
- */
44
- private $ storeId ;
45
-
46
- /**
47
- * @var mixed|null
48
- */
49
- private $ catalogId ;
50
-
51
43
/**
52
44
* @var SystemConfig
53
45
*/
@@ -69,12 +61,20 @@ class Diagnostics extends Template
69
61
private $ productCollectionFactory ;
70
62
71
63
/**
64
+ * @var StoreRepositoryInterface
65
+ */
66
+ public $ storeRepo ;
67
+
68
+ /**
69
+ * Construct
70
+ *
72
71
* @param Context $context
73
72
* @param SystemConfig $systemConfig
74
73
* @param GraphAPIAdapter $graphApiAdapter
75
74
* @param FBEHelper $fbeHelper
76
75
* @param LoggerInterface $logger
77
76
* @param CollectionFactory $productCollectionFactory
77
+ * @param StoreRepositoryInterface $storeRepo
78
78
* @param array $data
79
79
*/
80
80
public function __construct (
@@ -84,44 +84,57 @@ public function __construct(
84
84
FBEHelper $ fbeHelper ,
85
85
LoggerInterface $ logger ,
86
86
CollectionFactory $ productCollectionFactory ,
87
+ StoreRepositoryInterface $ storeRepo ,
87
88
array $ data = []
88
89
) {
89
90
$ this ->systemConfig = $ systemConfig ;
90
91
$ this ->graphApiAdapter = $ graphApiAdapter ;
91
92
$ this ->fbeHelper = $ fbeHelper ;
92
- $ this ->storeId = $ this ->fbeHelper ->getStore ()->getId ();
93
- $ this ->catalogId = $ this ->systemConfig ->getCatalogId ($ this ->storeId );
94
93
$ this ->logger = $ logger ;
95
94
$ this ->productCollectionFactory = $ productCollectionFactory ;
95
+ $ this ->storeRepo = $ storeRepo ;
96
96
parent ::__construct ($ context , $ data );
97
97
}
98
98
99
99
/**
100
- * Get report
100
+ * Get reports
101
101
*
102
102
* @return array
103
103
*/
104
- public function getReport ()
104
+ public function getReports ()
105
105
{
106
- $ report = [];
106
+ $ reports = [];
107
+ $ stores = $ this ->getStores ();
107
108
try {
108
- $ response = $ this ->graphApiAdapter ->getCatalogDiagnostics ($ this ->catalogId );
109
- if (isset ($ response ['diagnostics ' ]['data ' ])) {
110
- $ report = $ response ['diagnostics ' ]['data ' ];
109
+ foreach ($ stores as $ key => $ store ) {
110
+ if ($ key === 'admin ' ) {
111
+ continue ;
112
+ }
113
+ $ catalogId = $ this ->systemConfig ->getCatalogId ($ store ->getId ());
114
+ $ response = $ this ->graphApiAdapter ->getCatalogDiagnostics ($ catalogId );
115
+ if (isset ($ response ['diagnostics ' ]['data ' ])) {
116
+ $ reports [$ key ] = [];
117
+ $ reports [$ key ]['data ' ] = $ response ['diagnostics ' ]['data ' ];
118
+ $ reports [$ key ]['catalog_id ' ] = $ catalogId ;
119
+ $ reports [$ key ]['store_id ' ] = $ store ->getId ();
120
+ $ reports [$ key ]['store_name ' ] = $ store ->getName ();
121
+ }
111
122
}
112
123
} catch (Exception $ e ) {
113
124
$ this ->logger ->critical ($ e ->getMessage ());
114
125
}
115
- return $ report ;
126
+ return $ reports ;
116
127
}
117
128
118
129
/**
119
130
* Get sample affected items
120
131
*
121
132
* @param array $diagnosticItem
133
+ * @param int $catalogId
134
+ * @param int $storeId
122
135
* @return array
123
136
*/
124
- public function getSampleAffectedItems (array $ diagnosticItem )
137
+ public function getSampleAffectedItems (array $ diagnosticItem, int $ catalogId , int $ storeId )
125
138
{
126
139
if (!array_key_exists ('sample_affected_items ' , $ diagnosticItem )) {
127
140
return [];
@@ -132,12 +145,12 @@ public function getSampleAffectedItems(array $diagnosticItem)
132
145
return $ a ['id ' ];
133
146
}, $ diagnosticItem ['sample_affected_items ' ]);
134
147
135
- $ fbProducts = $ this ->graphApiAdapter ->getProductsByFacebookProductIds ($ this -> catalogId , $ fbIds );
148
+ $ fbProducts = $ this ->graphApiAdapter ->getProductsByFacebookProductIds ($ catalogId , $ fbIds );
136
149
$ retailerIds = array_map (function ($ a ) {
137
150
return $ a ['retailer_id ' ];
138
151
}, $ fbProducts ['data ' ]);
139
152
140
- return $ this ->getProducts ($ retailerIds );
153
+ return $ this ->getProducts ($ retailerIds, $ storeId );
141
154
} catch (Exception $ e ) {
142
155
$ this ->logger ->critical ($ e ->getMessage ());
143
156
}
@@ -149,13 +162,14 @@ public function getSampleAffectedItems(array $diagnosticItem)
149
162
* Get admin url
150
163
*
151
164
* @param ProductInterface $product
165
+ * @param int $store
152
166
* @return string
153
167
*/
154
- public function getAdminUrl (ProductInterface $ product )
168
+ public function getAdminUrl (ProductInterface $ product, int $ store = null )
155
169
{
156
170
$ params = ['id ' => $ product ->getId ()];
157
- if ($ this -> getRequest ()-> getParam ( ' store ' ) ) {
158
- $ params ['store ' ] = $ this -> getRequest ()-> getParam ( ' store ' ) ;
171
+ if ($ store ) {
172
+ $ params ['store ' ] = $ store ;
159
173
}
160
174
return $ this ->getUrl ('catalog/product/edit ' , $ params );
161
175
}
@@ -164,16 +178,17 @@ public function getAdminUrl(ProductInterface $product)
164
178
* Get products
165
179
*
166
180
* @param array $retailerIds
181
+ * @param int $storeId
167
182
* @return array
168
183
*/
169
- private function getProducts (array $ retailerIds )
184
+ private function getProducts (array $ retailerIds, int $ storeId )
170
185
{
171
186
$ collection = $ this ->productCollectionFactory ->create ();
172
187
$ collection ->addAttributeToSelect ('* ' )
173
- ->addStoreFilter ($ this -> storeId )
174
- ->setStoreId ($ this -> storeId );
188
+ ->addStoreFilter ($ storeId )
189
+ ->setStoreId ($ storeId );
175
190
176
- $ productIdentifierAttr = $ this ->systemConfig ->getProductIdentifierAttr ($ this -> storeId );
191
+ $ productIdentifierAttr = $ this ->systemConfig ->getProductIdentifierAttr ($ storeId );
177
192
if ($ productIdentifierAttr === IdentifierConfig::PRODUCT_IDENTIFIER_SKU ) {
178
193
$ collection ->addAttributeToFilter ('sku ' , ['in ' => $ retailerIds ]);
179
194
} elseif ($ productIdentifierAttr === IdentifierConfig::PRODUCT_IDENTIFIER_ID ) {
@@ -184,4 +199,14 @@ private function getProducts(array $retailerIds)
184
199
185
200
return $ collection ->getItems ();
186
201
}
202
+
203
+ /**
204
+ * Get stores
205
+ *
206
+ * @return StoreInterface[]
207
+ */
208
+ private function getStores ()
209
+ {
210
+ return $ this ->storeRepo ->getList ();
211
+ }
187
212
}
0 commit comments