Skip to content

Commit 6846698

Browse files
committed
MAGE-848 Implement virtual replica disable for all stores
1 parent b25bbee commit 6846698

File tree

1 file changed

+93
-3
lines changed

1 file changed

+93
-3
lines changed

Console/Command/ReplicaDisableVirtualCommand.php

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,44 @@
33
namespace Algolia\AlgoliaSearch\Console\Command;
44

55
use Algolia\AlgoliaSearch\Api\Console\ReplicaSyncCommandInterface;
6+
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
67
use Algolia\AlgoliaSearch\Console\Traits\ReplicaSyncCommandTrait;
8+
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
9+
use Algolia\AlgoliaSearch\Helper\Configuration\ConfigChecker;
10+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
11+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
12+
use Magento\Framework\App\Cache\Manager as CacheManager;
13+
use Magento\Framework\App\Config\ScopeConfigInterface;
14+
use Magento\Framework\App\Config\Storage\WriterInterface;
15+
use Magento\Framework\App\State;
716
use Magento\Framework\Console\Cli;
17+
use Magento\Framework\Serialize\SerializerInterface;
18+
use Magento\Store\Model\ScopeInterface;
19+
use Magento\Store\Model\StoreManagerInterface;
820
use Symfony\Component\Console\Input\InputInterface;
921
use Symfony\Component\Console\Output\OutputInterface;
10-
use Symfony\Component\Console\Question\ConfirmationQuestion;
1122

12-
class ReplicaDisableVirtualCommand extends AbstractReplicaCommand implements ReplicaSyncCommandInterface
13-
{
23+
class ReplicaDisableVirtualCommand extends AbstractReplicaCommand implements ReplicaSyncCommandInterface {
1424
use ReplicaSyncCommandTrait;
1525

26+
public function __construct(
27+
protected WriterInterface $configWriter,
28+
protected ConfigChecker $configChecker,
29+
protected ScopeConfigInterface $scopeConfig,
30+
protected SerializerInterface $serializer,
31+
protected ConfigHelper $configHelper,
32+
protected CacheManager $cacheManager,
33+
protected ReplicaManagerInterface $replicaManager,
34+
protected StoreManagerInterface $storeManager,
35+
protected ProductHelper $productHelper,
36+
State $state,
37+
StoreNameFetcher $storeNameFetcher,
38+
?string $name = null
39+
)
40+
{
41+
parent::__construct($state, $storeNameFetcher, $name);
42+
}
43+
1644
protected function getReplicaCommandName(): string
1745
{
1846
return 'disable-virtual-replicas';
@@ -48,7 +76,69 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4876
return CLI::RETURN_SUCCESS;
4977
}
5078

79+
$this->disableVirtualReplicas($storeIds);
80+
5181
return Cli::RETURN_SUCCESS;
5282
}
5383

84+
/**
85+
* @param int[] $storeIds
86+
* @return void
87+
*/
88+
protected function disableVirtualReplicas(array $storeIds = []): void
89+
{
90+
if (count($storeIds)) {
91+
foreach ($storeIds as $storeId) {
92+
$this->disableVirtualReplicasForStore($storeId);
93+
}
94+
} else {
95+
$this->disableVirtualReplicasForAllStores();
96+
}
97+
}
98+
99+
protected function disableVirtualReplicasForStore(int $storeId): void
100+
{
101+
$storeName = $this->storeNameFetcher->getStoreName($storeId);
102+
$isStoreScoped = false;
103+
104+
}
105+
106+
protected function disableVirtualReplicasForAllStores(): void
107+
{
108+
$this->configChecker->checkAndApplyAllScopes(ConfigHelper::USE_VIRTUAL_REPLICA_ENABLED, [$this, 'removeLegacyVirtualReplicaConfig']);
109+
110+
$this->configChecker->checkAndApplyAllScopes(ConfigHelper::SORTING_INDICES, [$this, 'disableVirtualReplicaSortConfig']);
111+
112+
$this->cacheManager->clean(['config']);
113+
114+
$this->syncReplicasForAllStores();
115+
}
116+
117+
public function removeLegacyVirtualReplicaConfig(string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, int $scopeId = 0): void
118+
{
119+
$value = $this->scopeConfig->getValue(ConfigHelper::USE_VIRTUAL_REPLICA_ENABLED, $scope, $scopeId);
120+
if (is_null($value)) {
121+
return;
122+
}
123+
$this->output->writeln("<info>Removing legacy configuration " . ConfigHelper::USE_VIRTUAL_REPLICA_ENABLED . " for $scope scope" . ($scope != ScopeConfigInterface::SCOPE_TYPE_DEFAULT ? " (ID=$scopeId)" : "") . "</info>");
124+
$this->configWriter->delete(ConfigHelper::USE_VIRTUAL_REPLICA_ENABLED, $scope, $scopeId);
125+
}
126+
127+
public function disableVirtualReplicaSortConfig(string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, int $scopeId = 0): void
128+
{
129+
$raw = $this->scopeConfig->getValue(ConfigHelper::SORTING_INDICES, $scope, $scopeId);
130+
if (!$raw) {
131+
return;
132+
}
133+
$sorting = array_map(
134+
function($sort) {
135+
$sort[ReplicaManagerInterface::SORT_KEY_VIRTUAL_REPLICA] = 0;
136+
return $sort;
137+
},
138+
$this->serializer->unserialize($raw)
139+
);
140+
$this->output->writeln("<info>Disabling all virtual replicas in " . ConfigHelper::SORTING_INDICES . " for $scope scope" . ($scope != ScopeConfigInterface::SCOPE_TYPE_DEFAULT ? " (ID=$scopeId)" : "") . "</info>");
141+
$this->configHelper->setSorting($sorting, $scope, $scopeId);
142+
}
143+
54144
}

0 commit comments

Comments
 (0)