|
3 | 3 | namespace Algolia\AlgoliaSearch\Console\Command;
|
4 | 4 |
|
5 | 5 | use Algolia\AlgoliaSearch\Api\Console\ReplicaSyncCommandInterface;
|
| 6 | +use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface; |
6 | 7 | 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; |
7 | 16 | use Magento\Framework\Console\Cli;
|
| 17 | +use Magento\Framework\Serialize\SerializerInterface; |
| 18 | +use Magento\Store\Model\ScopeInterface; |
| 19 | +use Magento\Store\Model\StoreManagerInterface; |
8 | 20 | use Symfony\Component\Console\Input\InputInterface;
|
9 | 21 | use Symfony\Component\Console\Output\OutputInterface;
|
10 |
| -use Symfony\Component\Console\Question\ConfirmationQuestion; |
11 | 22 |
|
12 |
| -class ReplicaDisableVirtualCommand extends AbstractReplicaCommand implements ReplicaSyncCommandInterface |
13 |
| -{ |
| 23 | +class ReplicaDisableVirtualCommand extends AbstractReplicaCommand implements ReplicaSyncCommandInterface { |
14 | 24 | use ReplicaSyncCommandTrait;
|
15 | 25 |
|
| 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 | + |
16 | 44 | protected function getReplicaCommandName(): string
|
17 | 45 | {
|
18 | 46 | return 'disable-virtual-replicas';
|
@@ -48,7 +76,69 @@ protected function execute(InputInterface $input, OutputInterface $output): int
|
48 | 76 | return CLI::RETURN_SUCCESS;
|
49 | 77 | }
|
50 | 78 |
|
| 79 | + $this->disableVirtualReplicas($storeIds); |
| 80 | + |
51 | 81 | return Cli::RETURN_SUCCESS;
|
52 | 82 | }
|
53 | 83 |
|
| 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 | + |
54 | 144 | }
|
0 commit comments