self::CONFIG_PATH__REMOTE_STORAGE_PREFIX, self::OPTION_REMOTE_STORAGE_ENDPOINT => self::CONFIG_PATH__REMOTE_STORAGE_ENDPOINT, self::OPTION_REMOTE_STORAGE_BUCKET => self::CONFIG_PATH__REMOTE_STORAGE_BUCKET, self::OPTION_REMOTE_STORAGE_REGION => self::CONFIG_PATH__REMOTE_STORAGE_REGION, self::OPTION_REMOTE_STORAGE_ACCESS_KEY => self::CONFIG_PATH__REMOTE_STORAGE_ACCESS_KEY, self::OPTION_REMOTE_STORAGE_SECRET_KEY => self::CONFIG_PATH__REMOTE_STORAGE_SECRET_KEY, self::OPTION_REMOTE_STORAGE_PATH_STYLE => self::CONFIG_PATH__REMOTE_STORAGE_PATH_STYLE ]; /** * @var DriverFactoryPool */ private $driverFactoryPool; /** * @var LoggerInterface */ private $logger; /** * @param DriverFactoryPool $driverFactoryPool * @param LoggerInterface $logger */ public function __construct(DriverFactoryPool $driverFactoryPool, LoggerInterface $logger) { $this->driverFactoryPool = $driverFactoryPool; $this->logger = $logger; } /** * @inheritDoc */ public function getOptions(): array { return [ new TextConfigOption( self::OPTION_REMOTE_STORAGE_DRIVER, TextConfigOption::FRONTEND_WIZARD_TEXT, self::CONFIG_PATH__REMOTE_STORAGE_DRIVER, 'Remote storage driver', DriverPool::FILE ), new TextConfigOption( self::OPTION_REMOTE_STORAGE_PREFIX, TextConfigOption::FRONTEND_WIZARD_TEXT, self::CONFIG_PATH__REMOTE_STORAGE_PREFIX, 'Remote storage prefix', '' ), new TextConfigOption( self::OPTION_REMOTE_STORAGE_ENDPOINT, TextConfigOption::FRONTEND_WIZARD_TEXT, self::CONFIG_PATH__REMOTE_STORAGE_ENDPOINT, 'Remote storage endpoint' ), new TextConfigOption( self::OPTION_REMOTE_STORAGE_BUCKET, TextConfigOption::FRONTEND_WIZARD_TEXT, self::CONFIG_PATH__REMOTE_STORAGE_BUCKET, 'Remote storage bucket' ), new TextConfigOption( self::OPTION_REMOTE_STORAGE_REGION, TextConfigOption::FRONTEND_WIZARD_TEXT, self::CONFIG_PATH__REMOTE_STORAGE_REGION, 'Remote storage region' ), new TextConfigOption( self::OPTION_REMOTE_STORAGE_ACCESS_KEY, TextConfigOption::FRONTEND_WIZARD_PASSWORD, self::CONFIG_PATH__REMOTE_STORAGE_ACCESS_KEY, 'Remote storage access key', '' ), new TextConfigOption( self::OPTION_REMOTE_STORAGE_SECRET_KEY, TextConfigOption::FRONTEND_WIZARD_PASSWORD, self::CONFIG_PATH__REMOTE_STORAGE_SECRET_KEY, 'Remote storage secret key', '' ), new TextConfigOption( self::OPTION_REMOTE_STORAGE_PATH_STYLE, TextConfigOption::FRONTEND_WIZARD_PASSWORD, self::CONFIG_PATH__REMOTE_STORAGE_PATH_STYLE, 'Remote storage path style', '0' ) ]; } /** * @inheritDoc */ public function createConfig(array $options, DeploymentConfig $deploymentConfig): array { $isRemoteStorageDeploymentConfigExists = isset($deploymentConfig->getConfigData()['remote_storage']); // if remote storage config is already present and driver is not in $options, return early to prevent overwrite if ($isRemoteStorageDeploymentConfigExists && !isset($options[self::OPTION_REMOTE_STORAGE_DRIVER])) { return []; } $driver = $options[self::OPTION_REMOTE_STORAGE_DRIVER] ?? DriverPool::FILE; if ($driver === DriverPool::FILE) { $configData = new ConfigData(ConfigFilePool::APP_ENV); $configData->setOverrideWhenSave(true); $configData->set(self::CONFIG_PATH__REMOTE_STORAGE_DRIVER, $driver); } else { $configData = $this->createConfigData($driver, $options); } return [$configData]; } /** * @inheritDoc */ public function validate(array $options, DeploymentConfig $deploymentConfig): array { // deployment configuration existence determines readiness of object manager to resolve remote storage drivers $isDeploymentConfigExists = (bool) $deploymentConfig->getConfigData(); if (!$isDeploymentConfigExists) { return []; } $driver = $options[self::OPTION_REMOTE_STORAGE_DRIVER] ?? DriverPool::FILE; if ($driver === DriverPool::FILE) { return []; } $errors = []; if (empty($options[self::OPTION_REMOTE_STORAGE_REGION])) { $errors[] = 'Region is required'; } if (empty($options[self::OPTION_REMOTE_STORAGE_BUCKET])) { $errors[] = 'Bucket is required'; } if (!$errors) { $configData = $this->createConfigData($driver, $options); try { $this->driverFactoryPool->get($driver)->createConfigured( (array)$configData->getData()['remote_storage']['config'], (string)$options[self::OPTION_REMOTE_STORAGE_PREFIX] )->test(); } catch (LocalizedException $exception) { $message = $exception->getMessage(); $this->logger->critical($message); $errors[] = 'Adapter error: ' . $message; } } return $errors; } /** * Creates pre-configured config data object. * * @param string $driver * @param array $options * @return ConfigData */ private function createConfigData(string $driver, array $options): ConfigData { $configData = new ConfigData(ConfigFilePool::APP_ENV); $configData->setOverrideWhenSave(true); $configData->set(self::CONFIG_PATH__REMOTE_STORAGE_DRIVER, $driver); foreach (self::$map as $option => $configPath) { if (!empty($options[$option])) { $configData->set($configPath, $options[$option]); } } return $configData; } }