Skip to content

Commit ce3b23f

Browse files
committed
MAGE-1244: removed affectedProductIds logic from indexer
1 parent 2d3cf62 commit ce3b23f

File tree

3 files changed

+23
-46
lines changed

3 files changed

+23
-46
lines changed

Model/Indexer/Category.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
*/
1313
class Category implements \Magento\Framework\Indexer\ActionInterface, \Magento\Framework\Mview\ActionInterface
1414
{
15-
public static $affectedProductIds = [];
16-
1715
public function __construct(
1816
protected StoreManagerInterface $storeManager,
1917
protected ConfigHelper $configHelper,
@@ -43,9 +41,6 @@ public function executeList(array $ids)
4341

4442
public function executeRow($id)
4543
{
46-
if (count(self::$affectedProductIds)) {
47-
$this->categoryBatchQueueProcessor->setAffectedProductIds(self::$affectedProductIds);
48-
}
4944
$this->execute([$id]);
5045
}
5146
}

Model/Indexer/CategoryObserver.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Algolia\AlgoliaSearch\Model\Indexer;
44

5+
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
56
use Algolia\AlgoliaSearch\Model\Indexer\Category as CategoryIndexer;
67
use Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager;
8+
use Algolia\AlgoliaSearch\Service\Product\BatchQueueProcessor as ProductBatchQueueProcessor;
79
use Magento\Catalog\Model\Category as CategoryModel;
810
use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel;
911
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
1012
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1114
use Magento\Framework\Indexer\IndexerRegistry;
15+
use Magento\Store\Model\StoreManagerInterface;
1216

1317
class CategoryObserver
1418
{
@@ -17,7 +21,9 @@ class CategoryObserver
1721

1822
public function __construct(
1923
IndexerRegistry $indexerRegistry,
24+
protected StoreManagerInterface $storeManager,
2025
protected ResourceConnection $resource,
26+
protected ProductBatchQueueProcessor $productBatchQueueProcessor,
2127
protected AlgoliaCredentialsManager $algoliaCredentialsManager
2228
) {
2329
$this->indexer = $indexerRegistry->get('algolia_categories');
@@ -52,8 +58,8 @@ public function afterSave(
5258
$changedProductIds = ($category->getChangedProductIds() !== null ? (array) $category->getChangedProductIds() : []);
5359

5460
if (!$this->indexer->isScheduled()) {
55-
CategoryIndexer::$affectedProductIds = array_unique(array_merge($changedProductIds, $collectionIds));
5661
$this->indexer->reindexRow($category->getId());
62+
$this->reindexAffectedProducts(array_unique(array_merge($changedProductIds, $collectionIds)));
5763
} else {
5864
// missing logic, if scheduled, when category is saved w/out product, products need to be added to _cl
5965
if (count($changedProductIds) === 0 && count($collectionIds) > 0) {
@@ -82,15 +88,29 @@ public function afterDelete(
8288
if (!$this->indexer->isScheduled()) {
8389
/* we are using products position because getProductCollection() doesn't use correct store */
8490
$productCollection = $category->getProductsPosition();
85-
CategoryIndexer::$affectedProductIds = array_keys($productCollection);
86-
8791
$this->indexer->reindexRow($category->getId());
92+
$this->reindexAffectedProducts(array_keys($productCollection));
8893
}
8994
});
9095

9196
return $result;
9297
}
9398

99+
/**
100+
* @param array $affectedProductIds
101+
* @return void
102+
* @throws DiagnosticsException
103+
* @throws NoSuchEntityException
104+
*/
105+
protected function reindexAffectedProducts(array $affectedProductIds): void
106+
{
107+
if (count($affectedProductIds) > 0) {
108+
foreach (array_keys($this->storeManager->getStores()) as $storeId) {
109+
$this->productBatchQueueProcessor->processBatch($storeId, $affectedProductIds);
110+
}
111+
}
112+
}
113+
94114
/**
95115
* @param array $productIds
96116
*/

Service/Category/BatchQueueProcessor.php

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@
1010
use Algolia\AlgoliaSearch\Model\Queue;
1111
use Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager;
1212
use Algolia\AlgoliaSearch\Service\Category\IndexBuilder as CategoryIndexBuilder;
13-
use Algolia\AlgoliaSearch\Service\Product\IndexBuilder as ProductIndexBuilder;
1413
use Magento\Framework\Exception\LocalizedException;
1514
use Magento\Framework\Exception\NoSuchEntityException;
1615

1716
class BatchQueueProcessor implements BatchQueueProcessorInterface
1817
{
19-
public $affectedProductIds = [];
20-
2118
public function __construct(
2219
protected Data $dataHelper,
2320
protected ConfigHelper $configHelper,
@@ -44,10 +41,6 @@ public function processBatch(int $storeId, ?array $entityIds = null): void
4441
return;
4542
}
4643

47-
if (count($this->affectedProductIds) > 0) {
48-
$this->rebuildAffectedProducts($storeId);
49-
}
50-
5144
$categoriesPerPage = $this->configHelper->getNumberOfElementByPage();
5245

5346
if (is_array($entityIds) && count($entityIds) > 0) {
@@ -59,28 +52,6 @@ public function processBatch(int $storeId, ?array $entityIds = null): void
5952
$this->processFullReindex($storeId, $categoriesPerPage);
6053
}
6154

62-
/**
63-
* @param int $storeId
64-
*/
65-
protected function rebuildAffectedProducts(int $storeId): void
66-
{
67-
if ($this->configHelper->indexProductOnCategoryProductsUpdate($storeId)) {
68-
$productsPerPage = $this->configHelper->getNumberOfElementByPage();
69-
foreach (array_chunk($this->affectedProductIds, $productsPerPage) as $chunk) {
70-
/** @uses ProductIndexBuilder::buildIndexList() */
71-
$this->queue->addToQueue(
72-
ProductIndexBuilder::class,
73-
'buildIndexList',
74-
[
75-
'storeId' => $storeId,
76-
'entityIds' => $chunk,
77-
],
78-
count($chunk)
79-
);
80-
}
81-
}
82-
}
83-
8455
/**
8556
* @param array $categoryIds
8657
* @param int $categoriesPerPage
@@ -138,13 +109,4 @@ protected function processFullReindex(int $storeId, int $categoriesPerPage): voi
138109
);
139110
}
140111
}
141-
142-
/**
143-
* @param array $affectedProductIds
144-
* @return void
145-
*/
146-
public function setAffectedProductIds(array $affectedProductIds): void
147-
{
148-
$this->affectedProductIds = $affectedProductIds;
149-
}
150112
}

0 commit comments

Comments
 (0)