CurrencyImporter.php in Commerce Core 8.2
File
modules/price/src/CurrencyImporter.php
View source
<?php
namespace Drupal\commerce_price;
use CommerceGuys\Addressing\Country\CountryRepository;
use CommerceGuys\Intl\Currency\CurrencyRepository;
use CommerceGuys\Intl\Exception\UnknownCurrencyException;
use CommerceGuys\Intl\Exception\UnknownLocaleException;
use Drupal\commerce_price\Entity\CurrencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
class CurrencyImporter implements CurrencyImporterInterface {
protected $storage;
protected $languageManager;
protected $externalRepository;
public function __construct(EntityTypeManagerInterface $entity_type_manager, LanguageManagerInterface $language_manager) {
$this->storage = $entity_type_manager
->getStorage('commerce_currency');
$this->languageManager = $language_manager;
$this->externalRepository = new CurrencyRepository();
}
public function getImportable() {
$imported_currencies = $this->storage
->loadMultiple();
$language = $this->languageManager
->getConfigOverrideLanguage() ?: $this->languageManager
->getCurrentLanguage();
$langcode = $language
->getId();
$all_currencies = $this->externalRepository
->getAll($langcode, 'en');
$importable_currencies = array_diff_key($all_currencies, $imported_currencies);
$importable_currencies = array_map(function ($currency) {
return $currency
->getName();
}, $importable_currencies);
return $importable_currencies;
}
public function import($currency_code) {
if ($existing_entity = $this->storage
->load($currency_code)) {
return $existing_entity;
}
$default_langcode = $this->languageManager
->getDefaultLanguage()
->getId();
$currency = $this->externalRepository
->get($currency_code, $default_langcode, 'en');
$values = [
'langcode' => $default_langcode,
'currencyCode' => $currency
->getCurrencyCode(),
'name' => $currency
->getName(),
'numericCode' => $currency
->getNumericCode(),
'symbol' => $currency
->getSymbol(),
'fractionDigits' => $currency
->getFractionDigits(),
];
$entity = $this->storage
->create($values);
$entity
->trustData()
->save();
if ($this->languageManager
->isMultilingual()) {
$languages = $this->languageManager
->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
$languages = array_diff_key($languages, [
$default_langcode => $default_langcode,
]);
$langcodes = array_map(function ($language) {
return $language
->getId();
}, $languages);
$this
->importEntityTranslations($entity, $langcodes);
}
return $entity;
}
public function importByCountry($country_code) {
$country_repository = new CountryRepository();
$country = $country_repository
->get($country_code);
$currency_code = $country
->getCurrencyCode();
$entity = NULL;
if ($currency_code) {
$entity = $this
->import($currency_code);
}
return $entity;
}
public function importTranslations(array $langcodes) {
foreach ($this->storage
->loadMultiple() as $currency) {
$this
->importEntityTranslations($currency, $langcodes);
}
}
protected function importEntityTranslations(CurrencyInterface $currency, array $langcodes) {
$currency_code = $currency
->getCurrencyCode();
$config_name = $currency
->getConfigDependencyName();
foreach ($langcodes as $langcode) {
try {
$translated_currency = $this->externalRepository
->get($currency_code, $langcode);
} catch (UnknownCurrencyException $e) {
return;
} catch (UnknownLocaleException $e) {
continue;
}
$config_translation = $this->languageManager
->getLanguageConfigOverride($langcode, $config_name);
if ($config_translation
->isNew()) {
$config_translation
->set('name', $translated_currency
->getName());
$config_translation
->set('symbol', $translated_currency
->getSymbol());
$config_translation
->save();
}
}
}
}