You are here

protected function ExchangerProviderRemoteBase::mapExchangeRates in Commerce Exchanger 8

Helper function to create array for exchange rates.

Parameters

\Drupal\commerce_exchanger\ExchangerProviderRates $exchange_rates: The ExchangeRates.

Return value

array Return array prepared for saving in Drupal config.

2 calls to ExchangerProviderRemoteBase::mapExchangeRates()
ExchangerProviderRemoteBase::crossSyncCalculate in src/Plugin/Commerce/ExchangerProvider/ExchangerProviderRemoteBase.php
Rates calculation for currencies when we use cross sync conversion.
ExchangerProviderRemoteBase::importEnterprise in src/Plugin/Commerce/ExchangerProvider/ExchangerProviderRemoteBase.php
Fetch remote provider by each currency and create dataset.

File

src/Plugin/Commerce/ExchangerProvider/ExchangerProviderRemoteBase.php, line 188

Class

ExchangerProviderRemoteBase
Base class for Commerce exchanger provider plugins.

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider

Code

protected function mapExchangeRates(ExchangerProviderRates $exchange_rates) {

  // Get current exchange rates.
  $mapping = $this->configFactory
    ->get($this
    ->getConfigName())
    ->getRawData();
  $rates = $exchange_rates
    ->getRates();
  $base_currency = $exchange_rates
    ->getBaseCurrency();

  // Set defaults.
  $calculated_rates = [];
  $calculated_rates[$base_currency] = [];

  // Loop trough data, set new values or leave manually defined.
  foreach ($rates as $currency => $rate) {

    // Skip base currency to map to itself.
    if ($currency !== $base_currency) {
      if (empty($mapping[$base_currency][$currency]['sync'])) {
        $calculated_rates[$base_currency][$currency]['value'] = $rate;
        $calculated_rates[$base_currency][$currency]['sync'] = $mapping[$base_currency][$currency]['sync'] ?? 0;
      }
      else {
        $calculated_rates[$base_currency][$currency] = $mapping[$base_currency][$currency];
      }
    }
  }
  return $calculated_rates;
}