Files
2026-09-15 10:35:56 +08:00

88 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Translation\Model;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Encryption\EncryptorInterface;
use Magento\Store\Model\ScopeInterface;
class Config
{
private const XML_PATH_ENABLED = 'esun_translation/general/enabled';
private const XML_PATH_SOURCE_LOCALE = 'esun_translation/general/source_locale';
private const XML_PATH_SOURCE_STORE_ID = 'esun_translation/general/source_store_id';
private const XML_PATH_ATTRIBUTES = 'esun_translation/general/translatable_attributes';
private const XML_PATH_LANGUAGE_GROUPS = 'esun_translation/general/language_groups';
private const XML_PATH_API_BASE_URL = 'esun_translation/ai/api_base_url';
private const XML_PATH_API_KEY = 'esun_translation/ai/api_key';
private const XML_PATH_MODEL = 'esun_translation/ai/model';
private const XML_PATH_TEMPERATURE = 'esun_translation/ai/temperature';
private const XML_PATH_TIMEOUT = 'esun_translation/ai/timeout';
public function __construct(
private readonly ScopeConfigInterface $scopeConfig,
private readonly EncryptorInterface $encryptor
) {
}
public function isEnabled(): bool
{
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED, ScopeInterface::SCOPE_STORE);
}
public function getSourceLocale(): string
{
return trim((string)$this->scopeConfig->getValue(self::XML_PATH_SOURCE_LOCALE)) ?: 'en';
}
public function getSourceStoreId(): int
{
return max(0, (int)$this->scopeConfig->getValue(self::XML_PATH_SOURCE_STORE_ID));
}
/**
* @return string[]
*/
public function getTranslatableAttributeCodes(): array
{
$raw = (string)$this->scopeConfig->getValue(self::XML_PATH_ATTRIBUTES);
$codes = preg_split('/[\s,]+/', $raw) ?: [];
return array_values(array_unique(array_filter(array_map('trim', $codes))));
}
public function getLanguageGroupsConfig(): string
{
return (string)$this->scopeConfig->getValue(self::XML_PATH_LANGUAGE_GROUPS);
}
public function getApiBaseUrl(): string
{
return rtrim((string)$this->scopeConfig->getValue(self::XML_PATH_API_BASE_URL), '/');
}
public function getApiKey(): string
{
$value = (string)$this->scopeConfig->getValue(self::XML_PATH_API_KEY);
return $value !== '' ? $this->encryptor->decrypt($value) : '';
}
public function getModel(): string
{
return trim((string)$this->scopeConfig->getValue(self::XML_PATH_MODEL));
}
public function getTemperature(): float
{
return (float)$this->scopeConfig->getValue(self::XML_PATH_TEMPERATURE);
}
public function getTimeout(): int
{
return max(1, (int)$this->scopeConfig->getValue(self::XML_PATH_TIMEOUT));
}
}