|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Console\Command; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Api\Console\ReplicaSyncCommandInterface; |
| 6 | +use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface; |
| 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\ReinitableConfigInterface; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Framework\App\Config\Storage\WriterInterface; |
| 16 | +use Magento\Framework\App\State; |
| 17 | +use Magento\Framework\Console\Cli; |
| 18 | +use Magento\Framework\Serialize\SerializerInterface; |
| 19 | +use Magento\Store\Model\ScopeInterface; |
| 20 | +use Magento\Store\Model\StoreManagerInterface; |
| 21 | +use Symfony\Component\Console\Input\InputInterface; |
| 22 | +use Symfony\Component\Console\Output\OutputInterface; |
| 23 | + |
| 24 | +class ReplicaDisableVirtualCommand extends AbstractReplicaCommand implements ReplicaSyncCommandInterface { |
| 25 | + use ReplicaSyncCommandTrait; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + protected WriterInterface $configWriter, |
| 29 | + protected ConfigChecker $configChecker, |
| 30 | + protected ReinitableConfigInterface $scopeConfig, |
| 31 | + protected SerializerInterface $serializer, |
| 32 | + protected ConfigHelper $configHelper, |
| 33 | + protected CacheManager $cacheManager, |
| 34 | + protected ReplicaManagerInterface $replicaManager, |
| 35 | + protected StoreManagerInterface $storeManager, |
| 36 | + protected ProductHelper $productHelper, |
| 37 | + State $state, |
| 38 | + StoreNameFetcher $storeNameFetcher, |
| 39 | + ?string $name = null |
| 40 | + ) |
| 41 | + { |
| 42 | + parent::__construct($state, $storeNameFetcher, $name); |
| 43 | + } |
| 44 | + |
| 45 | + protected function getReplicaCommandName(): string |
| 46 | + { |
| 47 | + return 'disable-virtual-replicas'; |
| 48 | + } |
| 49 | + |
| 50 | + protected function getCommandDescription(): string |
| 51 | + { |
| 52 | + return 'Disable virtual replicas for all product sorting attributes and revert to standard replicas'; |
| 53 | + } |
| 54 | + |
| 55 | + protected function getStoreArgumentDescription(): string |
| 56 | + { |
| 57 | + return 'ID(s) for store(s) to disable virtual replicas (optional), if not specified disable virtual replicas for all stores'; |
| 58 | + } |
| 59 | + |
| 60 | + protected function getAdditionalDefinition(): array |
| 61 | + { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 66 | + { |
| 67 | + $this->output = $output; |
| 68 | + $this->input = $input; |
| 69 | + $this->setAreaCode(); |
| 70 | + |
| 71 | + $storeIds = $this->getStoreIds($input); |
| 72 | + |
| 73 | + $output->writeln($this->decorateOperationAnnouncementMessage('Disabling virtual replicas for {{target}}', $storeIds)); |
| 74 | + |
| 75 | + $okMsg = 'Configure virtual replicas by attribute under: Stores > Configuration > Algolia Search > InstantSearch Results Page > Sorting'; |
| 76 | + if (!$this->confirmOperation($okMsg)) { |
| 77 | + return CLI::RETURN_SUCCESS; |
| 78 | + } |
| 79 | + |
| 80 | + $this->disableVirtualReplicas($storeIds); |
| 81 | + |
| 82 | + return Cli::RETURN_SUCCESS; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @param int[] $storeIds |
| 87 | + * @return void |
| 88 | + */ |
| 89 | + protected function disableVirtualReplicas(array $storeIds = []): void |
| 90 | + { |
| 91 | + $updates = []; |
| 92 | + if (count($storeIds)) { |
| 93 | + foreach ($storeIds as $storeId) { |
| 94 | + if ($this->disableVirtualReplicasForStore($storeId)) { |
| 95 | + $updates[] = $storeId; |
| 96 | + } |
| 97 | + } |
| 98 | + if ($updates) { |
| 99 | + $this->scopeConfig->reinit(); |
| 100 | + foreach ($updates as $storeId) { |
| 101 | + $this->syncReplicasForStore($storeId); |
| 102 | + } |
| 103 | + } |
| 104 | + } else { |
| 105 | + $this->disableVirtualReplicasForAllStores(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + protected function disableVirtualReplicasForStore(int $storeId): bool |
| 110 | + { |
| 111 | + $storeName = $this->storeNameFetcher->getStoreName($storeId); |
| 112 | + $isStoreScoped = false; |
| 113 | + |
| 114 | + if ($this->configChecker->isSettingAppliedForScopeAndCode( |
| 115 | + ConfigHelper::LEGACY_USE_VIRTUAL_REPLICA_ENABLED, |
| 116 | + ScopeInterface::SCOPE_STORES, |
| 117 | + $storeId) |
| 118 | + ) { |
| 119 | + $isStoreScoped = true; |
| 120 | + $this->removeLegacyVirtualReplicaConfig(ScopeInterface::SCOPE_STORES, $storeId); |
| 121 | + } |
| 122 | + |
| 123 | + if ($this->configChecker->isSettingAppliedForScopeAndCode( |
| 124 | + ConfigHelper::SORTING_INDICES, |
| 125 | + ScopeInterface::SCOPE_STORES, |
| 126 | + $storeId) |
| 127 | + ) { |
| 128 | + $isStoreScoped = true; |
| 129 | + $this->disableVirtualReplicaSortConfig(ScopeInterface::SCOPE_STORES, $storeId); |
| 130 | + } |
| 131 | + |
| 132 | + if (!$isStoreScoped) { |
| 133 | + $this->output->writeln("<info>Virtual replicas are not configured at the store level for $storeName. You will need to re-run this command for all stores.</info>"); |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + protected function disableVirtualReplicasForAllStores(): void |
| 141 | + { |
| 142 | + $this->configChecker->checkAndApplyAllScopes(ConfigHelper::LEGACY_USE_VIRTUAL_REPLICA_ENABLED, [$this, 'removeLegacyVirtualReplicaConfig']); |
| 143 | + |
| 144 | + $this->configChecker->checkAndApplyAllScopes(ConfigHelper::SORTING_INDICES, [$this, 'disableVirtualReplicaSortConfig']); |
| 145 | + |
| 146 | + $this->scopeConfig->reinit(); |
| 147 | + |
| 148 | + $this->syncReplicasForAllStores(); |
| 149 | + } |
| 150 | + |
| 151 | + public function removeLegacyVirtualReplicaConfig(string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, int $scopeId = 0): void |
| 152 | + { |
| 153 | + $value = $this->scopeConfig->getValue(ConfigHelper::LEGACY_USE_VIRTUAL_REPLICA_ENABLED, $scope, $scopeId); |
| 154 | + if (is_null($value)) { |
| 155 | + return; |
| 156 | + } |
| 157 | + $this->output->writeln("<info>Removing legacy configuration " . ConfigHelper::LEGACY_USE_VIRTUAL_REPLICA_ENABLED . " for $scope scope" . ($scope != ScopeConfigInterface::SCOPE_TYPE_DEFAULT ? " (ID=$scopeId)" : "") . "</info>"); |
| 158 | + $this->configWriter->delete(ConfigHelper::LEGACY_USE_VIRTUAL_REPLICA_ENABLED, $scope, $scopeId); |
| 159 | + } |
| 160 | + |
| 161 | + public function disableVirtualReplicaSortConfig(string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, int $scopeId = 0): void |
| 162 | + { |
| 163 | + $raw = $this->scopeConfig->getValue(ConfigHelper::SORTING_INDICES, $scope, $scopeId); |
| 164 | + if (!$raw) { |
| 165 | + return; |
| 166 | + } |
| 167 | + $sorting = array_map( |
| 168 | + function($sort) { |
| 169 | + $sort[ReplicaManagerInterface::SORT_KEY_VIRTUAL_REPLICA] = 0; |
| 170 | + return $sort; |
| 171 | + }, |
| 172 | + $this->serializer->unserialize($raw) |
| 173 | + ); |
| 174 | + $this->output->writeln("<info>Disabling all virtual replicas in " . ConfigHelper::SORTING_INDICES . " for $scope scope" . ($scope != ScopeConfigInterface::SCOPE_TYPE_DEFAULT ? " (ID=$scopeId)" : "") . "</info>"); |
| 175 | + $this->configHelper->setSorting($sorting, $scope, $scopeId); |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments