You are here

abstract class ExchangerProviderRemoteBase in Commerce Exchanger 8

Base class for Commerce exchanger provider plugins.

Hierarchy

Expanded class hierarchy of ExchangerProviderRemoteBase

2 files declare their use of ExchangerProviderRemoteBase
TestEnterpriseExchanger.php in tests/modules/commerce_exchanger_test/src/Plugin/Commerce/ExchangerProvider/TestEnterpriseExchanger.php
TestExchanger.php in tests/modules/commerce_exchanger_test/src/Plugin/Commerce/ExchangerProvider/TestExchanger.php

File

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

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider
View source
abstract class ExchangerProviderRemoteBase extends ExchangerProviderBase implements ExchangerProviderRemoteInterface {

  /**
   * {@inheritdoc}
   */
  public function getMethod() {
    return $this->pluginDefinition['method'] ?? 'GET';
  }

  /**
   * {@inheritdoc}
   */
  public function isEnterprise() {
    return $this->configuration['enterprise'] ?? FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getApiKey() {
    return $this->configuration['api_key'];
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthData() {
    return $this->configuration['auth'];
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseCurrency() {
    return $this->configuration['base_currency'];
  }

  /**
   * {@inheritdoc}
   */
  public function useCrossSync() {
    return $this->configuration['use_cross_sync'];
  }

  /**
   * {@inheritdoc}
   */
  public function transformRates() {
    return $this->pluginDefinition['transform_rates'] ?? FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function apiClient(array $options) {
    $data = [];

    // Prepare for client.
    $client = $this->httpClientFactory
      ->fromOptions();
    try {
      $response = $client
        ->request($this
        ->getMethod(), $this
        ->apiUrl(), $options);

      // Expected result.
      $data = $response
        ->getBody()
        ->getContents();
    } catch (GuzzleException $e) {
      $this->logger
        ->error($e
        ->getMessage());
    }
    return $data;
  }

  /**
   * Process all currencies for rates for other currencies.
   *
   * @return array
   *   Return prepared data for saving.
   */
  protected function buildExchangeRates() {

    // If we use enterprise and we don't want cross sync feature.
    if ($this
      ->isEnterprise() && !$this
      ->useCrossSync()) {
      return $this
        ->importEnterprise();
    }

    // Everything else.
    return $this
      ->importCrossSync();
  }

  /**
   * Preform cross conversion between currencies to build exchange data rates.
   *
   * @return array
   *   Return array of exchange rates.
   */
  protected function importCrossSync() {
    $exchange_rates_data = $this
      ->processRemoteData();

    // Based on cross sync settings fetch and process data.
    return $this
      ->crossSyncCalculate($exchange_rates_data);
  }

  /**
   * Fetch remote provider by each currency and create dataset.
   *
   * @return array
   *   Return array of exchange rates.
   */
  protected function importEnterprise() {
    $exchange_rates = [];
    foreach ($this->currencies as $code => $currency) {
      $exchange_rates_data = $this
        ->processRemoteData($code);
      $exchange_rates += $this
        ->mapExchangeRates($exchange_rates_data);
    }
    return $exchange_rates;
  }

  /**
   * Process data with checking structure and preparing data for importing.
   *
   * @param string|null $base_currency
   *   The base currency or null if none.
   *
   * @return \Drupal\commerce_exchanger\ExchangerProviderRates
   *   The ExchangeRates.
   */
  protected function processRemoteData(string $base_currency = NULL) {
    $remote_data = $this
      ->getRemoteData($base_currency);

    // Validate and build structure.
    if (!isset($remote_data['base'], $remote_data['rates'])) {
      $exchange_rates['rates'] = $remote_data ?? [];
      $exchange_rates['base'] = $base_currency ?? $this
        ->getBaseCurrency();
    }
    else {
      $exchange_rates = $remote_data;
    }

    // Pass enabled currencies to automatically filter data.
    $exchange_rates['currencies'] = $this
      ->getCurrencies();
    $exchange_rates['transform'] = $this
      ->transformRates();
    return new ExchangerProviderRates($exchange_rates);
  }

  /**
   * Rates calculation for currencies when we use cross sync conversion.
   *
   * @param \Drupal\commerce_exchanger\ExchangerProviderRates $exchange_rates
   *   The ExchangeRates.
   *
   * @return array
   *   Return data prepared for saving.
   */
  protected function crossSyncCalculate(ExchangerProviderRates $exchange_rates) {
    $calculated_rates = [];

    // Enabled currency.
    $currencies = $this->currencies;
    foreach ($currencies as $currency_code => $name) {
      $calculate_rates = $this
        ->recalculateRates($currency_code, $exchange_rates);
      $map_rates = $this
        ->mapExchangeRates($calculate_rates);
      $calculated_rates[$currency_code] = $map_rates[$currency_code];
    }
    return $calculated_rates;
  }

  /**
   * Helper function to create array for exchange rates.
   *
   * @param \Drupal\commerce_exchanger\ExchangerProviderRates $exchange_rates
   *   The ExchangeRates.
   *
   * @return array
   *   Return array prepared for saving in Drupal config.
   */
  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;
  }

  /**
   * Recalculate currencies from exchange rate between two other currencies.
   *
   * @param string $target_currency
   *   Currency to which should be exchange rate calculated.
   * @param \Drupal\commerce_exchanger\ExchangerProviderRates $data
   *   Currency and rate array.
   *
   * @return \Drupal\commerce_exchanger\ExchangerProviderRates
   *   Return recalculated data.
   */
  protected function recalculateRates(string $target_currency, ExchangerProviderRates $data) {
    $rates = $data
      ->getRates();
    $base_currency = $data
      ->getBaseCurrency();

    // If we accidentally sent same target and base currency.
    $rate_target_currency = $rates[$target_currency] ?? 1;

    // Get rate based from base currency.
    $currency_default = round(1 / $rate_target_currency, 6);
    $recalculated = [];
    $recalculated[$base_currency] = $currency_default;

    // Recalculate all data.
    foreach ($rates as $currency => $rate) {
      if ($currency !== $target_currency) {
        $recalculated[$currency] = round($rate * $currency_default, 6);
      }
    }
    return new ExchangerProviderRates([
      'base' => $target_currency,
      'rates' => $recalculated,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function import() {
    $exchange_rates = $this
      ->buildExchangeRates();

    // Write new data.
    if (!empty($exchange_rates)) {
      $file = $this->configFactory
        ->getEditable($this
        ->getConfigName());
      $file
        ->setData([
        'rates' => $exchange_rates,
      ]);
      $file
        ->save();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExchangerProviderBase::$configFactory protected property Configuration management.
ExchangerProviderBase::$currencies protected property Return formatted array of currencies ['HRK' => 'Croatian Kuna'].
ExchangerProviderBase::$currencyStorage protected property The currency storage.
ExchangerProviderBase::$entityId private property Parent entity if present.
ExchangerProviderBase::$httpClientFactory protected property The HTTP client to fetch the feed data with.
ExchangerProviderBase::$logger protected property The logger.
ExchangerProviderBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
ExchangerProviderBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ExchangerProviderBase::defaultConfiguration public function
ExchangerProviderBase::getConfigName public function Return config object name where exchange rates are saved. Overrides ExchangerProviderInterface::getConfigName
ExchangerProviderBase::getConfiguration public function
ExchangerProviderBase::getCurrencies protected function Simple key-value array for enabled currencies.
ExchangerProviderBase::getMode public function
ExchangerProviderBase::setConfiguration public function
ExchangerProviderBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
ExchangerProviderBase::supportingModes public function
ExchangerProviderBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
ExchangerProviderBase::__construct public function Constructs a new ExchangeProvider object. Overrides PluginBase::__construct
ExchangerProviderRemoteBase::apiClient public function Generic wrapper around Drupal http client. Overrides ExchangerProviderRemoteInterface::apiClient
ExchangerProviderRemoteBase::buildExchangeRates protected function Process all currencies for rates for other currencies.
ExchangerProviderRemoteBase::crossSyncCalculate protected function Rates calculation for currencies when we use cross sync conversion.
ExchangerProviderRemoteBase::getApiKey public function Remote providers api key. Overrides ExchangerProviderRemoteInterface::getApiKey
ExchangerProviderRemoteBase::getAuthData public function Remote authentication credentials. Overrides ExchangerProviderRemoteInterface::getAuthData
ExchangerProviderRemoteBase::getBaseCurrency public function Either remote provider defined base currency, or use entered. Overrides ExchangerProviderRemoteInterface::getBaseCurrency
ExchangerProviderRemoteBase::getMethod public function Method which supports remote provider. Overrides ExchangerProviderRemoteInterface::getMethod
ExchangerProviderRemoteBase::import public function
ExchangerProviderRemoteBase::importCrossSync protected function Preform cross conversion between currencies to build exchange data rates.
ExchangerProviderRemoteBase::importEnterprise protected function Fetch remote provider by each currency and create dataset.
ExchangerProviderRemoteBase::isEnterprise public function Determine if remote provider supports querying by different base currency. Overrides ExchangerProviderRemoteInterface::isEnterprise
ExchangerProviderRemoteBase::mapExchangeRates protected function Helper function to create array for exchange rates.
ExchangerProviderRemoteBase::processRemoteData protected function Process data with checking structure and preparing data for importing.
ExchangerProviderRemoteBase::recalculateRates protected function Recalculate currencies from exchange rate between two other currencies.
ExchangerProviderRemoteBase::transformRates public function Determine if rates provided by provider needs to be transformed in a required rate ratio based on base currency. Overrides ExchangerProviderRemoteInterface::transformRates
ExchangerProviderRemoteBase::useCrossSync public function Either remote provider defined or use defined choice. Overrides ExchangerProviderRemoteInterface::useCrossSync
ExchangerProviderRemoteInterface::apiUrl public function URL from remote provider upon API call should be made. 6
ExchangerProviderRemoteInterface::getRemoteData public function Fetch external data. 6
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.