Skip to content

Commit 31dfb42

Browse files
committed
MAGE-1281 Implement hanlder for conditional replica forwarding
1 parent 49c8c27 commit 31dfb42

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

Helper/Entity/ProductHelper.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Algolia\AlgoliaSearch\Service\IndexOptionsBuilder;
1313
use Algolia\AlgoliaSearch\Service\Product\FacetBuilder;
1414
use Algolia\AlgoliaSearch\Service\Product\RecordBuilder as ProductRecordBuilder;
15+
use Algolia\AlgoliaSearch\Service\ReplicaSettingsHandler;
1516
use Magento\Catalog\Api\Data\ProductInterfaceFactory;
1617
use Magento\Catalog\Model\Product;
1718
use Magento\Catalog\Model\Product\Attribute\Source\Status;
@@ -87,6 +88,7 @@ public function __construct(
8788
protected ProductInterfaceFactory $productFactory,
8889
protected ProductRecordBuilder $productRecordBuilder,
8990
protected FacetBuilder $facetBuilder,
91+
protected ReplicaSettingsHandler $replicaSettingsHandler,
9092
)
9193
{
9294
parent::__construct($indexNameFetcher);
@@ -316,21 +318,13 @@ public function setSettings(
316318
): void {
317319
$indexSettings = $this->getIndexSettings($storeId);
318320

319-
$this->algoliaConnector->setSettings(
320-
$indexOptions,
321-
$indexSettings,
322-
false,
323-
true
324-
);
321+
$this->replicaSettingsHandler->setSettings($indexOptions, $indexSettings);
325322

326323
$this->logger->log('Settings: ' . json_encode($indexSettings));
327324
if ($saveToTmpIndicesToo) {
328-
329-
$this->algoliaConnector->setSettings(
325+
$this->replicaSettingsHandler->setSettings(
330326
$indexTmpOptions,
331327
$indexSettings,
332-
false,
333-
true,
334328
$indexOptions->getIndexName()
335329
);
336330

Service/ReplicaSettingsHandler.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Service;
4+
5+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
6+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
7+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
10+
/**
11+
* This class will proxy the settings save operations and forward to replicas based on user configuration
12+
* excluding attributes which should never be forwarded
13+
*/
14+
class ReplicaSettingsHandler
15+
{
16+
/**
17+
* As replicas are used for sorting we do not want to override these replicas specific configurations
18+
*/
19+
protected const FORWARDING_EXCLUDED_ATTRIBUTES = [
20+
'customRanking',
21+
'ranking'
22+
];
23+
24+
public function __construct(
25+
protected AlgoliaConnector $connector,
26+
protected ConfigHelper $config,
27+
) {}
28+
29+
/**
30+
* @throws NoSuchEntityException
31+
* @throws AlgoliaException
32+
*/
33+
public function setSettings(
34+
IndexOptionsInterface $indexOptions,
35+
array $indexSettings,
36+
string $mergeSettingsFrom = ''
37+
): void
38+
{
39+
if ($this->config->shouldForwardPrimaryIndexSettingsToReplicas($indexOptions->getStoreId())) {
40+
[$forward, $noForward] = $this->splitSettings($indexSettings);
41+
$this->connector->setSettings(
42+
$indexOptions,
43+
$forward,
44+
true,
45+
false
46+
);
47+
$this->connector->setSettings(
48+
$indexOptions,
49+
$noForward,
50+
false,
51+
true,
52+
$mergeSettingsFrom
53+
);
54+
} else {
55+
$this->connector->setSettings(
56+
$indexOptions,
57+
$indexSettings,
58+
false,
59+
true,
60+
$mergeSettingsFrom
61+
);
62+
}
63+
}
64+
65+
/**
66+
* Split settings based on whether they should be forwarded to the replicas
67+
* @return array Tuple of settings (to forward and not to forward)
68+
*/
69+
protected function splitSettings(array $settings): array
70+
{
71+
$excludedKeys = array_flip(self::FORWARDING_EXCLUDED_ATTRIBUTES);
72+
return [
73+
array_diff_key($settings, $excludedKeys),
74+
array_intersect_key($settings, $excludedKeys)
75+
];
76+
}
77+
78+
}

0 commit comments

Comments
 (0)