AlgoliaSearch\Laravel\AlgoliaEloquentTrait::_setSettings PHP Method

_setSettings() public method

public _setSettings ( $setToTmpIndices = false, $mergeOldSettings = false )
    public function _setSettings($setToTmpIndices = false, $mergeOldSettings = false)
    {
        /** @var \AlgoliaSearch\Laravel\ModelHelper $modelHelper */
        $modelHelper = App::make('\\AlgoliaSearch\\Laravel\\ModelHelper');
        $settings = $modelHelper->getSettings($this);
        if ($setToTmpIndices === false) {
            $indices = $modelHelper->getIndices($this);
        } else {
            $indices = $modelHelper->getIndicesTmp($this);
        }
        $replicas_settings = $modelHelper->getReplicasSettings($this);
        $replicas = isset($settings['replicas']) ? $settings['replicas'] : [];
        // Backward compatibility
        if ($replicas === [] && isset($settings['slaves'])) {
            $replicas = $settings['slaves'];
        }
        $b = true;
        /** @var \AlgoliaSearch\Index $index */
        foreach ($indices as $key => $index) {
            if ($mergeOldSettings) {
                $old_indices = $modelHelper->getIndices($this);
                $old_index = $old_indices[$key];
                try {
                    $oldSettings = $old_index->getSettings();
                } catch (\Exception $e) {
                    $oldSettings = [];
                }
                unset($oldSettings['replicas']);
                unset($oldSettings['slaves']);
                $newSettings = $oldSettings;
                foreach ($settings as $settingName => $settingValue) {
                    $newSettings[$settingName] = $settingValue;
                }
                $settings = $newSettings;
            }
            if ($b && isset($settings['replicas'])) {
                $settings['replicas'] = array_map(function ($indexName) use($modelHelper) {
                    return $modelHelper->getFinalIndexName($this, $indexName);
                }, $settings['replicas']);
            } elseif ($b && isset($settings['slaves'])) {
                // Backward compatibility
                $settings['slaves'] = array_map(function ($indexName) use($modelHelper) {
                    return $modelHelper->getFinalIndexName($this, $indexName);
                }, $settings['slaves']);
            }
            if (isset($settings['synonyms'])) {
                $index->batchSynonyms($settings['synonyms'], true, true);
            } else {
                // If no synonyms are passed, clear all synonyms from index
                $index->clearSynonyms(true);
            }
            // If we move the index the setSettings should not contains slave or replica.
            if ($setToTmpIndices && $b) {
                $b = false;
                unset($settings['replicas']);
                unset($settings['slaves']);
                // backward compatibility
            }
            if (count(array_keys($settings)) > 0) {
                // Synonyms cannot be pushed into "setSettings", it's got rejected from API and throwing exception
                // Synonyms cannot be removed directly from $settings var, because then synonym would not be set to other indices
                $settingsWithoutSynonyms = $settings;
                unset($settingsWithoutSynonyms['synonyms']);
                $index->setSettings($settingsWithoutSynonyms);
            }
            if ($b) {
                $b = false;
                unset($settings['replicas']);
                unset($settings['slaves']);
                // backward compatibility
            }
        }
        foreach ($replicas as $replica) {
            if (isset($replicas_settings[$replica])) {
                $index = $modelHelper->getIndices($this, $replica)[0];
                $s = array_merge($settings, $replicas_settings[$replica]);
                unset($s['synonyms']);
                if (count(array_keys($s)) > 0) {
                    $index->setSettings($s);
                }
            }
        }
    }