34
34
35
35
class CategoryCollection
36
36
{
37
-
38
37
/**
39
38
* @var ProductCollectionFactory
40
39
*/
@@ -90,8 +89,7 @@ public function __construct(
90
89
SystemConfig $ systemConfig ,
91
90
ProductIdentifier $ productIdentifier ,
92
91
CategoryImageService $ imageService
93
- )
94
- {
92
+ ) {
95
93
$ this ->categoryCollection = $ categoryCollection ;
96
94
$ this ->categoryRepository = $ categoryRepository ;
97
95
$ this ->productCollectionFactory = $ productCollectionFactory ;
@@ -125,7 +123,7 @@ public function makeHttpRequestsAfterCategorySave(Category $category, bool $isNa
125
123
if ($ isNameChanged ) {
126
124
$ categories = $ this ->getAllActiveChildrenCategories ($ category , $ storeId );
127
125
} else {
128
- $ categories [] = $ this ->categoryRepository ->get ($ category ->getId (), $ storeId );;
126
+ $ categories [] = $ this ->categoryRepository ->get ($ category ->getId (), $ storeId );
129
127
}
130
128
131
129
if (!$ this ->systemConfig ->isCatalogSyncEnabled ($ storeId )) {
@@ -145,14 +143,14 @@ public function makeHttpRequestsAfterCategorySave(Category $category, bool $isNa
145
143
146
144
$ this ->pushCategoriesToFBCollections ($ categories , $ accessToken , $ storeId );
147
145
} catch (\Throwable $ e ) {
148
- $ extra_data = [
146
+ $ extraData = [
149
147
'category_id ' => $ category ->getId (),
150
148
'category_name ' => $ category ->getName (),
151
149
'num_of_stores_for_category ' => count ($ storeIds )
152
150
];
153
151
$ this ->fbeHelper ->logExceptionImmediatelyToMeta (
154
152
$ e ,
155
- $ this ->getCategoryLoggerContext ($ storeId , 'category_sync_real_time ' , $ extra_data )
153
+ $ this ->getCategoryLoggerContext ($ storeId , 'category_sync_real_time ' , $ extraData )
156
154
);
157
155
}
158
156
}
@@ -164,7 +162,7 @@ public function makeHttpRequestsAfterCategorySave(Category $category, bool $isNa
164
162
* If the category is Tops we might create "Default Category > Men > Tops"
165
163
*
166
164
* @param Category $category
167
- * @param $storeId
165
+ * @param int $storeId
168
166
* @return string
169
167
*/
170
168
private function getCategoryPathName (Category $ category , $ storeId )
@@ -218,6 +216,7 @@ private function getCategoryMetaData(Category $category): ?array
218
216
219
217
/**
220
218
* Get category landing page URL.
219
+ *
221
220
* It first tries request path and URL finder for category, if fails to fetch it return category canonical URL
222
221
*
223
222
* @param Category $category
@@ -230,9 +229,9 @@ private function getDirectCategoryURL(Category $category): string
230
229
$ url = $ category ->getUrl ();
231
230
232
231
// if url returned by getURL is category admin page url, replace it with category canonical URL
233
- if (str_contains ($ url , 'admin/catalog/category/view/s ' )) {
232
+ if (strpos ($ url , 'admin/catalog/category/view/s ' ) !== false ) {
234
233
// if nothing works return URL by category ID (canonical URL)
235
- $ urlKey = $ category ->getUrlKey () ? $ category -> getUrlKey () : $ category ->formatUrlKey ($ category ->getName ());
234
+ $ urlKey = $ category ->getUrlKey () ?? $ category ->formatUrlKey ($ category ->getName ());
236
235
237
236
$ url = $ category ->getUrlInstance ()->getDirectUrl (
238
237
sprintf ('catalog/category/view/s/%s/id/%s/ ' , $ urlKey , $ category ->getId ())
@@ -263,7 +262,7 @@ private function getDirectCategoryURL(Category $category): string
263
262
*
264
263
* @param Category $category
265
264
* @param string $setId
266
- * @param $storeId
265
+ * @param int $storeId
267
266
* @throws \Throwable
268
267
*/
269
268
private function saveFBProductSetID (Category $ category , string $ setId , $ storeId ): void
@@ -281,8 +280,10 @@ private function saveFBProductSetID(Category $category, string $setId, $storeId)
281
280
}
282
281
283
282
/**
283
+ * Save category for store
284
+ *
284
285
* @param Category $category
285
- * @param $storeId
286
+ * @param int $storeId
286
287
*/
287
288
private function saveCategoryForStore (Category $ category , $ storeId ): void
288
289
{
@@ -308,7 +309,7 @@ private function saveCategoryForStore(Category $category, $storeId): void
308
309
* Get all children node in category tree recursion is being used.
309
310
*
310
311
* @param Category $category
311
- * @param $storeId
312
+ * @param int $storeId
312
313
* @param bool $onlyActiveCategories
313
314
* @return Collection
314
315
* @throws \Throwable
@@ -326,16 +327,16 @@ private function getAllChildrenCategories(
326
327
->setStoreId ($ storeId )
327
328
->addAttributeToSelect ('* ' )
328
329
->addAttributeToFilter (
329
- array (
330
- array (
330
+ [
331
+ [
331
332
"attribute " => "path " ,
332
333
"like " => $ categoryPath . "/% "
333
- ) ,
334
- array (
334
+ ] ,
335
+ [
335
336
"attribute " => "path " ,
336
337
"like " => $ categoryPath
337
- )
338
- )
338
+ ]
339
+ ]
339
340
);
340
341
341
342
if ($ onlyActiveCategories ) {
@@ -348,7 +349,7 @@ private function getAllChildrenCategories(
348
349
* Get all children categories
349
350
*
350
351
* @param Category $category
351
- * @param $storeId
352
+ * @param int $storeId
352
353
* @return Collection
353
354
* @throws \Throwable
354
355
*/
@@ -361,10 +362,9 @@ private function getAllActiveChildrenCategories(Category $category, $storeId): C
361
362
/**
362
363
* Get all active categories
363
364
*
364
- * @param $storeId
365
+ * @param int $storeId
365
366
* @return Collection
366
367
* @throws \Throwable
367
- * @throws \Throwable
368
368
*/
369
369
private function getAllActiveCategories ($ storeId ): Collection
370
370
{
@@ -381,15 +381,14 @@ private function getAllActiveCategories($storeId): Collection
381
381
* Initial collection call after fbe installation, please not we only push leaf category to collection,
382
382
* this means if a category contains any category, we won't create a collection for it.
383
383
*
384
- * @param $storeId
384
+ * @param int $storeId
385
385
* @return string|null
386
386
* @throws \Throwable
387
387
*/
388
388
public function pushAllCategoriesToFbCollections ($ storeId ): ?string
389
389
{
390
-
391
390
$ accessToken = $ this ->systemConfig ->getAccessToken ($ storeId );
392
- if ($ accessToken == null ) {
391
+ if (! $ accessToken ) {
393
392
$ this ->fbeHelper ->log (
394
393
"Category force update: can't find access token, abort pushAllCategoriesToFbCollections "
395
394
);
@@ -407,9 +406,9 @@ public function pushAllCategoriesToFbCollections($storeId): ?string
407
406
/**
408
407
* Push categories to FB collections
409
408
*
410
- * @param $categories
411
- * @param $accessToken
412
- * @param $storeId
409
+ * @param Collection $categories
410
+ * @param string $accessToken
411
+ * @param int $storeId
413
412
* @return string|null
414
413
*/
415
414
private function pushCategoriesToFBCollections ($ categories , $ accessToken , $ storeId ): ?string
@@ -422,7 +421,7 @@ private function pushCategoriesToFBCollections($categories, $accessToken, $store
422
421
foreach ($ categories as $ category ) {
423
422
try {
424
423
$ syncEnabled = $ category ->getData (MetaCatalogAttributes::CATEGORY_SYNC_TO_FACEBOOK );
425
- if ($ syncEnabled === " 0 " ) {
424
+ if ($ syncEnabled === ' 0 ' ) {
426
425
$ this ->fbeHelper ->log (
427
426
sprintf (
428
427
"Category update: user disabled category sync, category name: %s for store id: %s " ,
@@ -478,14 +477,14 @@ private function pushCategoriesToFBCollections($categories, $accessToken, $store
478
477
"please check the error log for more details " ,
479
478
$ category ->getName ()
480
479
);
481
- $ extra_data = [
480
+ $ extraData = [
482
481
'category_id ' => $ category ->getId (),
483
482
'category_name ' => $ category ->getName (),
484
483
'num_categories_for_update ' => count ($ categories )
485
484
];
486
485
$ this ->fbeHelper ->logExceptionImmediatelyToMeta (
487
486
$ e ,
488
- $ this ->getCategoryLoggerContext ($ storeId , 'categories_push_to_meta ' , $ extra_data )
487
+ $ this ->getCategoryLoggerContext ($ storeId , 'categories_push_to_meta ' , $ extraData )
489
488
);
490
489
}
491
490
}
@@ -495,15 +494,15 @@ private function pushCategoriesToFBCollections($categories, $accessToken, $store
495
494
$ this ->flushCategoryBatchRequest ($ requests , $ updatedCategories ,
496
495
$ currentBatch , $ accessToken , $ storeId ));
497
496
} catch (\Throwable $ e ) {
498
- $ extra_data = [
497
+ $ extraData = [
499
498
'num_categories_for_update ' => count ($ categories )
500
499
];
501
500
$ this ->fbeHelper ->logExceptionImmediatelyToMeta (
502
501
$ e ,
503
502
$ this ->getCategoryLoggerContext (
504
503
$ storeId ,
505
504
'categories_push_to_meta_last_page ' ,
506
- $ extra_data
505
+ $ extraData
507
506
)
508
507
);
509
508
}
@@ -541,7 +540,7 @@ private function getCategoryProductFilter(ProductCollection $productCollection):
541
540
* e.g. {'retailer_id': {'is_any': ['10', '100']}}
542
541
*
543
542
* @param Category $category
544
- * @param $storeId
543
+ * @param int $storeId
545
544
* @return ProductCollection
546
545
*/
547
546
private function getCategoryProducts (Category $ category , $ storeId ): ProductCollection
@@ -563,8 +562,9 @@ private function getCategoryProducts(Category $category, $storeId): ProductColle
563
562
* Api link: https://developers.facebook.com/docs/marketing-api/reference/product-set/
564
563
*
565
564
* @param Category $category
565
+ * @param ProductCollection $products
566
566
* @param string $setId
567
- * @param $storeId
567
+ * @param int $storeId
568
568
* @return array
569
569
*/
570
570
private function updateCategoryWithFBRequestJson (
@@ -593,8 +593,9 @@ private function updateCategoryWithFBRequestJson(
593
593
* Api link: https://developers.facebook.com/docs/marketing-api/reference/product-set/
594
594
*
595
595
* @param Category $category
596
+ * @param ProductCollection $products
596
597
* @param string $catalogId
597
- * @param $storeId
598
+ * @param int $storeId
598
599
* @return array
599
600
*/
600
601
private function pushCategoryWithFBRequestJson (
@@ -674,7 +675,6 @@ private function processCategoryBatchResponse(
674
675
$ setId = $ responseData ['id ' ];
675
676
$ this ->saveFBProductSetID ($ category , $ setId , $ storeId );
676
677
}
677
-
678
678
} else {
679
679
$ this ->fbeHelper ->log (sprintf (
680
680
"Error occurred while syncing category %s, response body %s " ,
@@ -699,7 +699,7 @@ private function processCategoryBatchResponse(
699
699
/**
700
700
* Returns request JSON for product set delete batch API
701
701
*
702
- * Api link: https://developers.facebook.com/docs/marketing-api/reference/product-set/
702
+ * API link: https://developers.facebook.com/docs/marketing-api/reference/product-set/
703
703
*
704
704
* @param string $setId
705
705
* @return array
@@ -712,9 +712,8 @@ private function deleteCategoryWithFBRequestJson(string $setId): array
712
712
);
713
713
}
714
714
715
-
716
715
/**
717
- * Call the api delete existing product set under category
716
+ * Call the API delete existing product set under category
718
717
*
719
718
* When user deletes a category on magento, we first get all sub categories(including itself), and check if we
720
719
* have created a collection set on fb side, if yes then we make delete api call.
@@ -766,14 +765,14 @@ public function deleteCategoryAndSubCategoryFromFB(Category $category): void
766
765
$ currentBatch ++;
767
766
}
768
767
} catch (\Throwable $ e ) {
769
- $ extra_data = [
768
+ $ extraData = [
770
769
'category_id ' => $ category ->getId (),
771
770
'category_name ' => $ category ->getName (),
772
771
'num_categories_for_delete ' => count ($ childrenCategories )
773
772
];
774
773
$ this ->fbeHelper ->logExceptionImmediatelyToMeta (
775
774
$ e ,
776
- $ this ->getCategoryLoggerContext ($ storeId , 'delete_categories ' , $ extra_data )
775
+ $ this ->getCategoryLoggerContext ($ storeId , 'delete_categories ' , $ extraData )
777
776
);
778
777
}
779
778
}
@@ -782,14 +781,14 @@ public function deleteCategoryAndSubCategoryFromFB(Category $category): void
782
781
try {
783
782
$ this ->flushCategoryDeleteBatchRequest ($ requests , $ currentBatch , $ accessToken , $ storeId );
784
783
} catch (\Throwable $ e ) {
785
- $ extra_data = [
784
+ $ extraData = [
786
785
'category_id ' => $ category ->getId (),
787
786
'category_name ' => $ category ->getName (),
788
787
'num_categories_for_delete ' => count ($ childrenCategories )
789
788
];
790
789
$ this ->fbeHelper ->logExceptionImmediatelyToMeta (
791
790
$ e ,
792
- $ this ->getCategoryLoggerContext ($ storeId , 'delete_categories_last_page ' , $ extra_data )
791
+ $ this ->getCategoryLoggerContext ($ storeId , 'delete_categories_last_page ' , $ extraData )
793
792
);
794
793
}
795
794
}
@@ -801,8 +800,8 @@ public function deleteCategoryAndSubCategoryFromFB(Category $category): void
801
800
*
802
801
* @param array $requests
803
802
* @param int $currentBatch
804
- * @param $accessToken
805
- * @param $storeId
803
+ * @param string $accessToken
804
+ * @param int $storeId
806
805
* @return void
807
806
* @throws \Throwable
808
807
*/
@@ -843,19 +842,19 @@ private function flushCategoryDeleteBatchRequest(
843
842
/**
844
843
* Return Category logger context for be logged
845
844
*
846
- * @param $storeId
847
- * @param $eventType
848
- * @param $extra_data
845
+ * @param int $storeId
846
+ * @param string $eventType
847
+ * @param array $extraData
849
848
* @return array
850
849
*/
851
- private function getCategoryLoggerContext ($ storeId , $ eventType , $ extra_data ): array
850
+ private function getCategoryLoggerContext ($ storeId , $ eventType , $ extraData ): array
852
851
{
853
852
return [
854
853
'store_id ' => $ storeId ,
855
854
'event ' => 'category_sync ' ,
856
855
'event_type ' => $ eventType ,
857
856
'catalog_id ' => $ this ->systemConfig ->getCatalogId ($ storeId ),
858
- 'extra_data ' => $ extra_data
857
+ 'extra_data ' => $ extraData
859
858
];
860
859
}
861
860
}
0 commit comments