Skip to content

Commit 41bcdec

Browse files
committed
MAGE-838 Add max replicas per index method
1 parent 91b3f88 commit 41bcdec

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Api/Product/ReplicaManagerInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ interface ReplicaManagerInterface
1313

1414
public const SORT_KEY_ATTRIBUTE_NAME = 'attribute';
1515
public const SORT_KEY_VIRTUAL_REPLICA = 'virtualReplica';
16-
// https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/#what-are-virtual-replicas
1716
public const MAX_VIRTUAL_REPLICA_LIMIT = 20;
1817

1918

@@ -40,4 +39,12 @@ public function handleReplicas(string $primaryIndexName, int $storeId, array $pr
4039
* @return bool
4140
*/
4241
public function isReplicaSyncEnabled(int $storeId): bool;
42+
43+
/**
44+
* Return the number of virtual replicas permitted per index
45+
* @link https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/#differences
46+
*
47+
* @return int
48+
*/
49+
public function getMaxVirtualReplicasPerIndex() : int;
4350
}

Model/Product/ReplicaManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,12 @@ public function isReplicaSyncEnabled(int $storeId): bool
370370
{
371371
return $this->configHelper->isInstantEnabled($storeId);
372372
}
373+
374+
/**
375+
* @inheritDoc
376+
*/
377+
public function getMaxVirtualReplicasPerIndex() : int
378+
{
379+
return self::MAX_VIRTUAL_REPLICA_LIMIT;
380+
}
373381
}

Validator/VirtualReplicaValidator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@ class VirtualReplicaValidator
1111
protected bool $replicaLimitExceeded = false;
1212
protected bool $tooManyCustomerGroups = false;
1313

14+
public function __construct(
15+
protected ReplicaManagerInterface $replicaManager
16+
)
17+
{}
18+
1419
public function isReplicaConfigurationValid(array $replicas): bool
1520
{
1621
foreach ($replicas as $replica) {
17-
// TODO: Implement replica limit override
22+
$maxReplicas = $this->replicaManager->getMaxVirtualReplicasPerIndex();
1823
if (!empty($replica[ReplicaManagerInterface::SORT_KEY_VIRTUAL_REPLICA])) {
1924
$this->replicaCount++;
2025
if ($replica[ReplicaManagerInterface::SORT_KEY_ATTRIBUTE_NAME] == ReplicaManagerInterface::SORT_ATTRIBUTE_PRICE) {
2126
$this->priceSortReplicaCount++;
2227
}
2328
}
2429

25-
if ($this->replicaCount > ReplicaManagerInterface::MAX_VIRTUAL_REPLICA_LIMIT) {
30+
if ($this->replicaCount > $maxReplicas) {
2631
$this->replicaLimitExceeded = true;
27-
$this->tooManyCustomerGroups = $this->priceSortReplicaCount > ReplicaManagerInterface::MAX_VIRTUAL_REPLICA_LIMIT;
32+
$this->tooManyCustomerGroups = $this->priceSortReplicaCount > $maxReplicas;
2833
}
2934
}
3035
return !$this->replicaLimitExceeded;

0 commit comments

Comments
 (0)