1010use Magento \Framework \Exception \NoSuchEntityException ;
1111use Magento \Framework \Filter \FilterManager ;
1212use Magento \Framework \Serialize \Serializer \Json ;
13- use Magmodules \Sooqr \Api \Config \RepositoryInterface as DataConfigRepository ;
13+ use Magmodules \Sooqr \Api \Config \RepositoryInterface as ConfigProvider ;
1414use Magmodules \Sooqr \Api \ProductData \RepositoryInterface as ProductData ;
1515use Magmodules \Sooqr \Model \Config \Source \FeedType ;
1616use Magmodules \Sooqr \Service \ProductData \AttributeCollector \Data \Image ;
@@ -30,24 +30,14 @@ class Repository implements ProductData
3030 'brand '
3131 ];
3232
33- /**
34- * Base attributes map to pull from product
35- *
36- * @var array
37- */
38- private $ attributeMap = [
33+ private array $ attributeMap = [
3934 'product_id ' => 'entity_id ' ,
4035 'visibility ' => 'visibility ' ,
4136 'type_id ' => 'type_id ' ,
4237 'status ' => 'status '
4338 ];
4439
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 = [
5141 'sqr:content_type ' => 'content_type ' ,
5242 'sqr:id ' => 'product_id ' ,
5343 'sqr:title ' => 'name ' ,
@@ -73,64 +63,27 @@ class Repository implements ProductData
7363 'sqr:rating ' => 'rating_summary '
7464 ];
7565
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 ;
11677
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- */
12578 public function __construct (
126- DataConfigRepository $ dataConfigRepository ,
79+ ConfigProvider $ configProvider ,
12780 Json $ json ,
12881 FilterManager $ filterManager ,
12982 Filter $ filter ,
13083 Type $ type ,
13184 Image $ image
13285 ) {
133- $ this ->dataConfigRepository = $ dataConfigRepository ;
86+ $ this ->configProvider = $ configProvider ;
13487 $ this ->json = $ json ;
13588 $ this ->filterManager = $ filterManager ;
13689 $ this ->filter = $ filter ;
@@ -146,23 +99,35 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $
14699 $ this ->collectIds ($ storeId , $ entityIds );
147100 $ this ->collectAttributes ($ storeId );
148101 $ 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 );
151103
152104 $ 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 )) {
155113 continue ;
156114 }
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 );
163115
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+ }
166131 }
167132 }
168133
@@ -178,7 +143,7 @@ public function getProductData(int $storeId = 0, ?array $entityIds = null, int $
178143 private function collectIds (int $ storeId , ?array $ entityIds = null ): void
179144 {
180145 $ this ->entityIds = $ this ->filter ->execute (
181- $ this ->dataConfigRepository ->getFilters ($ storeId ),
146+ $ this ->configProvider ->getFilters ($ storeId ),
182147 $ storeId
183148 );
184149 if ($ entityIds !== null ) {
@@ -193,7 +158,7 @@ private function collectIds(int $storeId, ?array $entityIds = null): void
193158 */
194159 private function collectAttributes (int $ storeId = 0 ): void
195160 {
196- $ attributes = $ this ->dataConfigRepository ->getAttributes ($ storeId );
161+ $ attributes = $ this ->configProvider ->getAttributes ($ storeId );
197162 $ this ->attributeMap += $ attributes ;
198163
199164 $ extraAttributes = array_diff_key ($ attributes , array_flip (self ::ATTRIBUTES ));
@@ -208,38 +173,38 @@ private function collectAttributes(int $storeId = 0): void
208173 * Collect all product data
209174 *
210175 * @param int $storeId
211- * @param int $type
176+ * @param array $batchIds
212177 * @return array
213178 * @throws NoSuchEntityException
214179 */
215- private function collectProductData (int $ storeId , int $ type = 3 ): array
180+ private function collectProductData (int $ storeId , array $ batchIds ): array
216181 {
217182 $ extraParameters = [
218183 'filters ' => [
219184 'exclude_attribute ' => null ,
220- 'exclude_disabled ' => !$ this ->dataConfigRepository ->getFilters ($ storeId )['add_disabled_products ' ],
185+ 'exclude_disabled ' => !$ this ->configProvider ->getFilters ($ storeId )['add_disabled_products ' ],
221186 'custom ' => []
222187 ],
223188 'stock ' => [
224189 'inventory ' => true ,
225190 'inventory_fields ' => ['qty ' , 'is_in_stock ' , 'salable_qty ' ]
226191 ],
227192 'rating_summary ' => [
228- 'enabled ' => $ this ->dataConfigRepository ->addRatingSummary ($ storeId ),
193+ 'enabled ' => $ this ->configProvider ->addRatingSummary ($ storeId ),
229194 ],
230195 'category ' => [
231196 'exclude_attribute ' => ['code ' => 'sooqr_cat_disable_export ' , 'value ' => 1 ],
232197 'include_anchor ' => true
233198 ],
234199 '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 )
238203 ]
239204 ];
240205
241206 return $ this ->type ->execute (
242- $ this -> entityIds ,
207+ $ batchIds ,
243208 $ this ->attributeMap ,
244209 $ extraParameters ,
245210 $ storeId
@@ -288,7 +253,7 @@ private function getImageWithFallback(?array $imageData, array $storeIds): ?stri
288253 return null ;
289254 }
290255
291- $ imageSource = $ this ->dataConfigRepository ->getImageAttribute ($ storeIds [0 ]);
256+ $ imageSource = $ this ->configProvider ->getImageAttribute ($ storeIds [0 ]);
292257 foreach ($ storeIds as $ storeId ) {
293258 if (!isset ($ imageData [$ storeId ])) {
294259 continue ;
@@ -493,7 +458,7 @@ private function categoryData(array $productData): array
493458 */
494459 private function postProcess (array $ result , int $ storeId = 0 ): array
495460 {
496- if (!$ this ->dataConfigRepository ->getFilters ($ storeId )['exclude_out_of_stock ' ] || empty ($ result )) {
461+ if (!$ this ->configProvider ->getFilters ($ storeId )['exclude_out_of_stock ' ] || empty ($ result )) {
497462 return $ result ;
498463 }
499464
0 commit comments