53 lines
1.5 KiB
PHP
53 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\MultiLocaleTranslation\Model;
|
|
|
|
use Magento\Framework\App\Config\ScopeConfigInterface;
|
|
|
|
class ProviderConfig
|
|
{
|
|
private const XML_PATH_DEFAULT_PROVIDER = 'esun_multilocale_translation/general/default_provider';
|
|
|
|
private const PROVIDERS = [
|
|
'deepl' => ['label' => 'DeepL', 'enabled_path' => 'esun_multilocale_translation/deepl/enabled'],
|
|
'google' => [
|
|
'label' => 'Google Cloud Translation V3',
|
|
'enabled_path' => 'esun_multilocale_translation/google/enabled',
|
|
],
|
|
];
|
|
|
|
public function __construct(private readonly ScopeConfigInterface $scopeConfig)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array{code:string,label:string}>
|
|
*/
|
|
public function getEnabledOptions(): array
|
|
{
|
|
$result = [];
|
|
foreach (self::PROVIDERS as $code => $provider) {
|
|
if (!$this->scopeConfig->isSetFlag($provider['enabled_path'])) {
|
|
continue;
|
|
}
|
|
$result[] = ['code' => $code, 'label' => $provider['label']];
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function getDefaultProvider(): string
|
|
{
|
|
$configured = (string)$this->scopeConfig->getValue(self::XML_PATH_DEFAULT_PROVIDER);
|
|
foreach ($this->getEnabledOptions() as $provider) {
|
|
if ($provider['code'] === $configured) {
|
|
return $configured;
|
|
}
|
|
}
|
|
|
|
return (string)($this->getEnabledOptions()[0]['code'] ?? '');
|
|
}
|
|
}
|