10
10
use Magento \Framework \Exception \NoSuchEntityException ;
11
11
use Magento \Framework \Filter \FilterManager ;
12
12
use Magento \Framework \Serialize \Serializer \Json ;
13
- use Magmodules \Sooqr \Api \Config \RepositoryInterface as DataConfigRepository ;
13
+ use Magmodules \Sooqr \Api \Config \RepositoryInterface as ConfigProvider ;
14
14
use Magmodules \Sooqr \Api \ProductData \RepositoryInterface as ProductData ;
15
15
use Magmodules \Sooqr \Model \Config \Source \FeedType ;
16
16
use Magmodules \Sooqr \Service \ProductData \AttributeCollector \Data \Image ;
@@ -30,24 +30,14 @@ class Repository implements ProductData
30
30
'brand '
31
31
];
32
32
33
- /**
34
- * Base attributes map to pull from product
35
- *
36
- * @var array
37
- */
38
- private $ attributeMap = [
33
+ private array $ attributeMap = [
39
34
'product_id ' => 'entity_id ' ,
40
35
'visibility ' => 'visibility ' ,
41
36
'type_id ' => 'type_id ' ,
42
37
'status ' => 'status '
43
38
];
44
39
45
- /**
46
- * Base map of feed structure data. Values as magento data, keys as data for feed
47
- *
48
- * @var array
49
- */
50
- private $ resultMap = [
40
+ private array $ resultMap = [
51
41
'sqr:content_type ' => 'content_type ' ,
52
42
'sqr:id ' => 'product_id ' ,
53
43
'sqr:title ' => 'name ' ,
@@ -73,64 +63,27 @@ class Repository implements ProductData
73
63
'sqr:rating ' => 'rating_summary '
74
64
];
75
65
76
- /**
77
- * @var DataConfigRepository
78
- */
79
- private $ dataConfigRepository ;
80
- /**
81
- * @var array
82
- */
83
- private $ entityIds ;
84
- /**
85
- * @var array
86
- */
87
- private $ parentSimples ;
88
- /**
89
- * @var Type
90
- */
91
- private $ type ;
92
- /**
93
- * @var Filter
94
- */
95
- private $ filter ;
96
- /**
97
- * @var Image
98
- */
99
- private $ image ;
100
- /**
101
- * @var array
102
- */
103
- private $ staticFields ;
104
- /**
105
- * @var array
106
- */
107
- private $ imageData ;
108
- /**
109
- * @var FilterManager
110
- */
111
- private $ filterManager ;
112
- /**
113
- * @var Json
114
- */
115
- private $ json ;
66
+ private array $ entityIds ;
67
+ private array $ parentSimples ;
68
+ private array $ staticFields ;
69
+ private array $ imageData ;
70
+
71
+ private Type $ type ;
72
+ private Filter $ filter ;
73
+ private Image $ image ;
74
+ private ConfigProvider $ configProvider ;
75
+ private FilterManager $ filterManager ;
76
+ private Json $ json ;
116
77
117
- /**
118
- * Repository constructor.
119
- * @param DataConfigRepository $dataConfigRepository
120
- * @param FilterManager $filterManager
121
- * @param Filter $filter
122
- * @param Type $type
123
- * @param Image $image
124
- */
125
78
public function __construct (
126
- DataConfigRepository $ dataConfigRepository ,
79
+ ConfigProvider $ configProvider ,
127
80
Json $ json ,
128
81
FilterManager $ filterManager ,
129
82
Filter $ filter ,
130
83
Type $ type ,
131
84
Image $ image
132
85
) {
133
- $ this ->dataConfigRepository = $ dataConfigRepository ;
86
+ $ this ->configProvider = $ configProvider ;
134
87
$ this ->json = $ json ;
135
88
$ this ->filterManager = $ filterManager ;
136
89
$ this ->filter = $ filter ;
@@ -146,23 +99,35 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $
146
99
$ this ->collectIds ($ storeId , $ entityIds );
147
100
$ this ->collectAttributes ($ storeId );
148
101
$ this ->parentSimples = [];
149
- $ this ->staticFields = $ this ->dataConfigRepository ->getStaticFields ($ storeId );
150
- $ this ->imageData = $ this ->image ->execute ($ this ->entityIds , $ storeId );
102
+ $ this ->staticFields = $ this ->configProvider ->getStaticFields ($ storeId );
151
103
152
104
$ result = [];
153
- foreach ($ this ->collectProductData ($ storeId , $ type ) as $ entityId => $ productData ) {
154
- if (empty ($ productData ['product_id ' ]) || $ productData ['status ' ] == 2 ) {
105
+
106
+ $ totalIds = count ($ this ->entityIds );
107
+ $ batchSize = $ this ->configProvider ->getBatchSize ();
108
+ $ batches = $ type !== FeedType::FULL ? 1 : (int ) ceil ($ totalIds / $ batchSize );
109
+
110
+ for ($ batch = 0 ; $ batch < $ batches ; $ batch ++) {
111
+ $ batchIds = array_slice ($ this ->entityIds , $ batch * $ batchSize , $ batchSize );
112
+ if (empty ($ batchIds )) {
155
113
continue ;
156
114
}
157
- $ this ->addImageData ($ storeId , (int )$ entityId , $ productData );
158
- $ this ->addStaticFields ($ productData );
159
- foreach ($ this ->resultMap as $ index => $ attr ) {
160
- $ result [$ entityId ][$ index ] = $ this ->prepareAttribute ($ attr , $ productData );
161
- }
162
- $ result [$ entityId ] += $ this ->categoryData ($ productData );
163
115
164
- if (!empty ($ productData ['parent_id ' ])) {
165
- $ this ->parentSimples [$ productData ['parent_id ' ]][] = $ productData ['product_id ' ];
116
+ $ this ->imageData = $ this ->image ->execute ($ batchIds , $ storeId );
117
+ foreach ($ this ->collectProductData ($ storeId , $ batchIds ) as $ entityId => $ productData ) {
118
+ if (empty ($ productData ['product_id ' ]) || $ productData ['status ' ] == 2 ) {
119
+ continue ;
120
+ }
121
+ $ this ->addImageData ($ storeId , (int )$ entityId , $ productData );
122
+ $ this ->addStaticFields ($ productData );
123
+ foreach ($ this ->resultMap as $ index => $ attr ) {
124
+ $ result [$ entityId ][$ index ] = $ this ->prepareAttribute ($ attr , $ productData );
125
+ }
126
+ $ result [$ entityId ] += $ this ->categoryData ($ productData );
127
+
128
+ if (!empty ($ productData ['parent_id ' ])) {
129
+ $ this ->parentSimples [$ productData ['parent_id ' ]][] = $ productData ['product_id ' ];
130
+ }
166
131
}
167
132
}
168
133
@@ -178,7 +143,7 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $
178
143
private function collectIds (int $ storeId , ?array $ entityIds = null ): void
179
144
{
180
145
$ this ->entityIds = $ this ->filter ->execute (
181
- $ this ->dataConfigRepository ->getFilters ($ storeId ),
146
+ $ this ->configProvider ->getFilters ($ storeId ),
182
147
$ storeId
183
148
);
184
149
if ($ entityIds !== null ) {
@@ -193,7 +158,7 @@ private function collectIds(int $storeId, ?array $entityIds = null): void
193
158
*/
194
159
private function collectAttributes (int $ storeId = 0 ): void
195
160
{
196
- $ attributes = $ this ->dataConfigRepository ->getAttributes ($ storeId );
161
+ $ attributes = $ this ->configProvider ->getAttributes ($ storeId );
197
162
$ this ->attributeMap += $ attributes ;
198
163
199
164
$ extraAttributes = array_diff_key ($ attributes , array_flip (self ::ATTRIBUTES ));
@@ -208,38 +173,38 @@ private function collectAttributes(int $storeId = 0): void
208
173
* Collect all product data
209
174
*
210
175
* @param int $storeId
211
- * @param int $type
176
+ * @param array $batchIds
212
177
* @return array
213
178
* @throws NoSuchEntityException
214
179
*/
215
- private function collectProductData (int $ storeId , int $ type = 3 ): array
180
+ private function collectProductData (int $ storeId , array $ batchIds ): array
216
181
{
217
182
$ extraParameters = [
218
183
'filters ' => [
219
184
'exclude_attribute ' => null ,
220
- 'exclude_disabled ' => !$ this ->dataConfigRepository ->getFilters ($ storeId )['add_disabled_products ' ],
185
+ 'exclude_disabled ' => !$ this ->configProvider ->getFilters ($ storeId )['add_disabled_products ' ],
221
186
'custom ' => []
222
187
],
223
188
'stock ' => [
224
189
'inventory ' => true ,
225
190
'inventory_fields ' => ['qty ' , 'is_in_stock ' , 'salable_qty ' ]
226
191
],
227
192
'rating_summary ' => [
228
- 'enabled ' => $ this ->dataConfigRepository ->addRatingSummary ($ storeId ),
193
+ 'enabled ' => $ this ->configProvider ->addRatingSummary ($ storeId ),
229
194
],
230
195
'category ' => [
231
196
'exclude_attribute ' => ['code ' => 'sooqr_cat_disable_export ' , 'value ' => 1 ],
232
197
'include_anchor ' => true
233
198
],
234
199
'behaviour ' => [
235
- 'configurable ' => $ this ->dataConfigRepository ->getConfigProductsBehaviour ($ storeId ),
236
- 'bundle ' => $ this ->dataConfigRepository ->getBundleProductsBehaviour ($ storeId ),
237
- 'grouped ' => $ this ->dataConfigRepository ->getGroupedProductsBehaviour ($ storeId )
200
+ 'configurable ' => $ this ->configProvider ->getConfigProductsBehaviour ($ storeId ),
201
+ 'bundle ' => $ this ->configProvider ->getBundleProductsBehaviour ($ storeId ),
202
+ 'grouped ' => $ this ->configProvider ->getGroupedProductsBehaviour ($ storeId )
238
203
]
239
204
];
240
205
241
206
return $ this ->type ->execute (
242
- $ this -> entityIds ,
207
+ $ batchIds ,
243
208
$ this ->attributeMap ,
244
209
$ extraParameters ,
245
210
$ storeId
@@ -288,7 +253,7 @@ private function getImageWithFallback(?array $imageData, array $storeIds): ?stri
288
253
return null ;
289
254
}
290
255
291
- $ imageSource = $ this ->dataConfigRepository ->getImageAttribute ($ storeIds [0 ]);
256
+ $ imageSource = $ this ->configProvider ->getImageAttribute ($ storeIds [0 ]);
292
257
foreach ($ storeIds as $ storeId ) {
293
258
if (!isset ($ imageData [$ storeId ])) {
294
259
continue ;
@@ -493,7 +458,7 @@ private function categoryData(array $productData): array
493
458
*/
494
459
private function postProcess (array $ result , int $ storeId = 0 ): array
495
460
{
496
- if (!$ this ->dataConfigRepository ->getFilters ($ storeId )['exclude_out_of_stock ' ] || empty ($ result )) {
461
+ if (!$ this ->configProvider ->getFilters ($ storeId )['exclude_out_of_stock ' ] || empty ($ result )) {
497
462
return $ result ;
498
463
}
499
464
0 commit comments