Skip to content

Commit e42ed35

Browse files
committed
MAGE-838 Add reversion for bad sorting config
1 parent 32642e3 commit e42ed35

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

Helper/ConfigHelper.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,10 @@ public function getAutocompleteSections($storeId = null)
443443
return [];
444444
}
445445

446+
protected function serialize(array $value): string {
447+
return $this->serializer->serialize($value);
448+
}
449+
446450
/**
447451
* @param $value
448452
* @return array|bool|float|int|mixed|string|null
@@ -1119,6 +1123,22 @@ public function getRawSortingValue($storeId = null)
11191123
);
11201124
}
11211125

1126+
/**
1127+
* @param array $sorting
1128+
* @param string|null $scope
1129+
* @param int|null $scopeId
1130+
* @return void
1131+
*/
1132+
public function setSorting(array $sorting, ?string $scope = null, ?int $scopeId = null): void
1133+
{
1134+
$this->configWriter->save(
1135+
self::SORTING_INDICES,
1136+
$this->serialize($sorting),
1137+
$scope,
1138+
$scopeId
1139+
);
1140+
}
1141+
11221142
/**
11231143
* @param $storeId
11241144
* @return string

Model/Backend/Sorts.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function __construct(
4848
*/
4949
public function afterSave(): \Magento\Framework\App\Config\Value
5050
{
51+
// Save context of the admin operation for possible state reversion
52+
$this->replicaState->setParentScope($this->getScope());
53+
$this->replicaState->setParentScopeId($this->getScopeId());
54+
5155
$storeIds = $this->configChecker->getAffectedStoreIds(
5256
$this->getPath(),
5357
$this->getScope(),

Model/Product/ReplicaManager.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,10 @@ protected function isReplicaConfigurationValid(string $primaryIndexName, int $st
275275
$sortingIndices = $this->configHelper->getSortingIndices($primaryIndexName, $storeId);
276276
$validator = $this->validatorFactory->create();
277277
if (!$validator->isReplicaConfigurationValid($sortingIndices)) {
278-
$postfix = "Please note that there can be no more than " . $this->getMaxVirtualReplicasPerIndex() . " virtual replicas per index. Reverting to previous configuration.";
279-
// TODO: Implement revert settings via ReplicaState
278+
$postfix = "Please note that there can be no more than " . $this->getMaxVirtualReplicasPerIndex() . " virtual replicas per index.";
279+
if ($this->revertReplicaConfig($storeId)) {
280+
$postfix .= ' Reverting to previous configuration.';
281+
}
280282
if ($validator->isTooManyCustomerGroups()) {
281283
throw (new TooManyCustomerGroupsAsReplicasException(__("You have too many customer groups to enable virtual replicas on the pricing sort. $postfix")))
282284
->withReplicaCount($validator->getReplicaCount())
@@ -290,6 +292,20 @@ protected function isReplicaConfigurationValid(string $primaryIndexName, int $st
290292
return true;
291293
}
292294

295+
protected function revertReplicaConfig(int $storeId): bool
296+
{
297+
if ($ogConfig = $this->replicaState->getOriginalSortConfiguration($storeId)) {
298+
$this->configHelper->setSorting(
299+
$ogConfig,
300+
$this->replicaState->getParentScope(),
301+
$this->replicaState->getParentScopeId()
302+
);
303+
return true;
304+
}
305+
306+
return false;
307+
}
308+
293309
/**
294310
* @param string[] $replicas
295311
* @return string[]

Registry/ReplicaState.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,28 @@ class ReplicaState
1313
private array $_scopeConfigurationOld = [];
1414

1515
private array $_scopeConfigurationNew = [];
16-
16+
17+
private ?string $_parentScope = null;
18+
private ?int $_parentScopeId = null;
19+
20+
public function getParentScope(): ?string
21+
{
22+
return $this->_parentScope;
23+
}
24+
25+
public function setParentScope(string $scope) {
26+
$this->_parentScope = $scope;
27+
}
28+
29+
public function getParentScopeId(): ?int
30+
{
31+
return $this->_parentScopeId;
32+
}
33+
34+
public function setParentScopeId(int $scopeId) {
35+
$this->_parentScopeId = $scopeId;
36+
}
37+
1738
public function getOriginalSortConfiguration(int $storeId): array
1839
{
1940
return $this->_scopeConfigurationOld[$storeId] ?? [];

0 commit comments

Comments
 (0)