|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Algolia\AlgoliaSearch\Service; |
| 4 | + |
| 5 | +use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface; |
| 6 | +use Algolia\AlgoliaSearch\Exceptions\AlgoliaException; |
| 7 | +use Algolia\AlgoliaSearch\Helper\ConfigHelper; |
| 8 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 9 | + |
| 10 | +/** |
| 11 | + * This class will proxy the settings save operations and forward to replicas based on user configuration |
| 12 | + * excluding attributes which should never be forwarded |
| 13 | + */ |
| 14 | +class ReplicaSettingsHandler |
| 15 | +{ |
| 16 | + /** |
| 17 | + * As replicas are used for sorting we do not want to override these replicas specific configurations |
| 18 | + */ |
| 19 | + protected const FORWARDING_EXCLUDED_ATTRIBUTES = [ |
| 20 | + 'customRanking', |
| 21 | + 'ranking' |
| 22 | + ]; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + protected AlgoliaConnector $connector, |
| 26 | + protected ConfigHelper $config, |
| 27 | + ) {} |
| 28 | + |
| 29 | + /** |
| 30 | + * @throws NoSuchEntityException |
| 31 | + * @throws AlgoliaException |
| 32 | + */ |
| 33 | + public function setSettings( |
| 34 | + IndexOptionsInterface $indexOptions, |
| 35 | + array $indexSettings, |
| 36 | + string $mergeSettingsFrom = '' |
| 37 | + ): void |
| 38 | + { |
| 39 | + if ($this->config->shouldForwardPrimaryIndexSettingsToReplicas($indexOptions->getStoreId())) { |
| 40 | + [$forward, $noForward] = $this->splitSettings($indexSettings); |
| 41 | + $this->connector->setSettings( |
| 42 | + $indexOptions, |
| 43 | + $forward, |
| 44 | + true, |
| 45 | + false |
| 46 | + ); |
| 47 | + $this->connector->setSettings( |
| 48 | + $indexOptions, |
| 49 | + $noForward, |
| 50 | + false, |
| 51 | + true, |
| 52 | + $mergeSettingsFrom |
| 53 | + ); |
| 54 | + } else { |
| 55 | + $this->connector->setSettings( |
| 56 | + $indexOptions, |
| 57 | + $indexSettings, |
| 58 | + false, |
| 59 | + true, |
| 60 | + $mergeSettingsFrom |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Split settings based on whether they should be forwarded to the replicas |
| 67 | + * @return array Tuple of settings (to forward and not to forward) |
| 68 | + */ |
| 69 | + protected function splitSettings(array $settings): array |
| 70 | + { |
| 71 | + $excludedKeys = array_flip(self::FORWARDING_EXCLUDED_ATTRIBUTES); |
| 72 | + return [ |
| 73 | + array_diff_key($settings, $excludedKeys), |
| 74 | + array_intersect_key($settings, $excludedKeys) |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments