scopeConfig = $scopeConfig; $this->resourceConfig = $resourceConfig; $this->encryptor = $encryptor; $this->moduleDataSetup = $moduleDataSetup; $this->storeManager = $storeManager; } /** * @inheritdoc */ public function apply(): void { $this->moduleDataSetup->startSetup(); $this->migrateDefaultValues(); $this->migrateWebsiteValues(); $this->moduleDataSetup->endSetup(); } /** * Migrate configuration values from DirectPost to Accept.js on default scope * * @return void */ private function migrateDefaultValues(): void { foreach ($this->configFieldsToMigrate as $field) { $configValue = $this->getOldConfigValue($field); if (!empty($configValue)) { $this->saveNewConfigValue($field, $configValue); } } foreach ($this->encryptedConfigFieldsToMigrate as $field) { $configValue = $this->getOldConfigValue($field); if (!empty($configValue)) { $this->saveNewConfigValue( $field, $configValue, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0, true ); } } } /** * Migrate configuration values from DirectPost to Accept.js on all website scopes * * @return void */ private function migrateWebsiteValues(): void { foreach ($this->storeManager->getWebsites() as $website) { $websiteID = (int) $website->getId(); foreach ($this->configFieldsToMigrate as $field) { $configValue = $this->getOldConfigValue($field, ScopeInterface::SCOPE_WEBSITES, $websiteID); if (!empty($configValue)) { $this->saveNewConfigValue($field, $configValue, ScopeInterface::SCOPE_WEBSITES, $websiteID); } } foreach ($this->encryptedConfigFieldsToMigrate as $field) { $configValue = $this->getOldConfigValue($field, ScopeInterface::SCOPE_WEBSITES, $websiteID); if (!empty($configValue)) { $this->saveNewConfigValue($field, $configValue, ScopeInterface::SCOPE_WEBSITES, $websiteID, true); } } } } /** * Get old configuration value from the DirectPost module's configuration on the store scope * * @param string $field * @param string $scope * @param int $scopeID * @return mixed */ private function getOldConfigValue( string $field, string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, int $scopeID = null ) { return $this->scopeConfig->getValue( sprintf(self::PAYMENT_PATH_FORMAT, self::DIRECTPOST_PATH, $field), $scope, $scopeID ); } /** * Save configuration value for AcceptJS * * @param string $field * @param mixed $value * @param string $scope * @param int $scopeID * @param bool $isEncrypted * @return void */ private function saveNewConfigValue( string $field, $value, string $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, int $scopeID = 0, bool $isEncrypted = false ): void { $value = $isEncrypted ? $this->encryptor->encrypt($value) : $value; $this->resourceConfig->saveConfig( sprintf(self::PAYMENT_PATH_FORMAT, self::ACCEPTJS_PATH, $field), $value, $scope, $scopeID ); } /** * @inheritdoc */ public static function getDependencies() { return []; } /** * @inheritdoc */ public function getAliases() { return []; } }