98 lines
3.4 KiB
PHP
98 lines
3.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Video\Model\RemoteStorage;
|
|
|
|
use Magento\Framework\App\Cache\Type\Config as ConfigCache;
|
|
use Magento\Framework\App\Cache\TypeListInterface;
|
|
use Magento\Framework\App\Config\Value;
|
|
use Magento\Framework\App\DeploymentConfig;
|
|
use Magento\Framework\App\DeploymentConfig\Writer;
|
|
use Magento\Framework\Config\File\ConfigFilePool;
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
|
|
class ConfigSynchronizer
|
|
{
|
|
private bool $synchronized = false;
|
|
|
|
public function __construct(
|
|
private readonly DeploymentConfig $deploymentConfig,
|
|
private readonly Writer $deploymentConfigWriter,
|
|
private readonly TypeListInterface $cacheTypeList
|
|
) {
|
|
}
|
|
|
|
public function synchronize(Value $backend): void
|
|
{
|
|
if ($this->synchronized) {
|
|
return;
|
|
}
|
|
|
|
$driver = (string)$backend->getFieldsetDataValue('driver');
|
|
if (!in_array($driver, ['file', 'aws-s3'], true)) {
|
|
throw new LocalizedException(__('Unsupported media storage driver.'));
|
|
}
|
|
if (!$this->deploymentConfigWriter->checkIfWritable()) {
|
|
throw new LocalizedException(__('app/etc/env.php is not writable by the PHP process.'));
|
|
}
|
|
|
|
$remoteStorage = ['driver' => $driver];
|
|
if ($driver === 'aws-s3') {
|
|
$bucket = trim((string)$backend->getFieldsetDataValue('bucket'));
|
|
$region = trim((string)$backend->getFieldsetDataValue('region'));
|
|
if ($bucket === '' || $region === '') {
|
|
throw new LocalizedException(__('S3 Bucket and AWS Region are required.'));
|
|
}
|
|
|
|
$current = (array)$this->deploymentConfig->get('remote_storage', []);
|
|
$currentCredentials = (array)($current['config']['credentials'] ?? []);
|
|
$accessKey = $this->resolveSecret(
|
|
(string)$backend->getFieldsetDataValue('access_key'),
|
|
(string)($currentCredentials['key'] ?? '')
|
|
);
|
|
$secretKey = $this->resolveSecret(
|
|
(string)$backend->getFieldsetDataValue('secret_key'),
|
|
(string)($currentCredentials['secret'] ?? '')
|
|
);
|
|
|
|
$remoteStorage['prefix'] = trim((string)$backend->getFieldsetDataValue('prefix'), '/ ');
|
|
$remoteStorage['config'] = [
|
|
'bucket' => $bucket,
|
|
'region' => $region,
|
|
'path_style' => (int)$backend->getFieldsetDataValue('path_style'),
|
|
'credentials' => [
|
|
'key' => $accessKey,
|
|
'secret' => $secretKey,
|
|
],
|
|
];
|
|
$endpoint = trim((string)$backend->getFieldsetDataValue('endpoint'));
|
|
if ($endpoint !== '') {
|
|
$remoteStorage['config']['endpoint'] = $endpoint;
|
|
}
|
|
}
|
|
|
|
$this->deploymentConfigWriter->saveConfig(
|
|
[ConfigFilePool::APP_ENV => ['remote_storage' => $remoteStorage]],
|
|
true,
|
|
null,
|
|
[],
|
|
true
|
|
);
|
|
$this->cacheTypeList->cleanType(ConfigCache::TYPE_IDENTIFIER);
|
|
$this->synchronized = true;
|
|
}
|
|
|
|
private function resolveSecret(string $postedValue, string $currentValue): string
|
|
{
|
|
if (preg_match('/^\*+$/', $postedValue)) {
|
|
return $currentValue;
|
|
}
|
|
|
|
if ($postedValue === '') {
|
|
return '';
|
|
}
|
|
|
|
return $postedValue;
|
|
}
|
|
}
|