You are here

class CurrencyImporter in Commerce Core 8.2

Default implementation of the currency importer.

Hierarchy

Expanded class hierarchy of CurrencyImporter

1 file declares its use of CurrencyImporter
CreateStoreCommand.php in modules/store/src/Command/CreateStoreCommand.php
1 string reference to 'CurrencyImporter'
commerce_price.services.yml in modules/price/commerce_price.services.yml
modules/price/commerce_price.services.yml
1 service uses CurrencyImporter
commerce_price.currency_importer in modules/price/commerce_price.services.yml
Drupal\commerce_price\CurrencyImporter

File

modules/price/src/CurrencyImporter.php, line 17

Namespace

Drupal\commerce_price
View source
class CurrencyImporter implements CurrencyImporterInterface {

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $storage;

  /**
   * The language manager.
   *
   * @var \Drupal\language\ConfigurableLanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The library's currency repository.
   *
   * @var \CommerceGuys\Intl\Currency\CurrencyRepositoryInterface
   */
  protected $externalRepository;

  /**
   * Creates a new CurrencyImporter object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public function getImportable() {
    $imported_currencies = $this->storage
      ->loadMultiple();

    // The getCurrentLanguage() fallback is a workaround for core bug #2684873.
    $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;
  }

  /**
   * {@inheritdoc}
   */
  public function import($currency_code) {
    if ($existing_entity = $this->storage
      ->load($currency_code)) {

      // Pretend the currency was just imported.
      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(),
    ];

    /** @var \Drupal\commerce_price\Entity\CurrencyInterface $entity */
    $entity = $this->storage
      ->create($values);
    $entity
      ->trustData()
      ->save();
    if ($this->languageManager
      ->isMultilingual()) {

      // Import translations for any additional languages the site has.
      $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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function importTranslations(array $langcodes) {
    foreach ($this->storage
      ->loadMultiple() as $currency) {
      $this
        ->importEntityTranslations($currency, $langcodes);
    }
  }

  /**
   * Imports translations for the given currency entity.
   *
   * @param \Drupal\commerce_price\Entity\CurrencyInterface $currency
   *   The currency entity.
   * @param array $langcodes
   *   The 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) {

        // The currency is custom and doesn't exist in the library.
        return;
      } catch (UnknownLocaleException $e) {

        // No translation found.
        continue;
      }

      /** @var \Drupal\language\Config\LanguageConfigOverride $config_translation */
      $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();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyImporter::$externalRepository protected property The library's currency repository.
CurrencyImporter::$languageManager protected property The language manager.
CurrencyImporter::$storage protected property The currency storage.
CurrencyImporter::getImportable public function Gets a list of importable currencies. Overrides CurrencyImporterInterface::getImportable
CurrencyImporter::import public function Imports currency data for the given currency code. Overrides CurrencyImporterInterface::import
CurrencyImporter::importByCountry public function Imports currency data for the given country code. Overrides CurrencyImporterInterface::importByCountry
CurrencyImporter::importEntityTranslations protected function Imports translations for the given currency entity.
CurrencyImporter::importTranslations public function Imports translations for the given language codes. Overrides CurrencyImporterInterface::importTranslations
CurrencyImporter::__construct public function Creates a new CurrencyImporter object.