You are here

class CurrencylayerExchanger in Commerce Exchanger 8

Provides the Currencylayer.com exchange rates.

Plugin annotation


@CommerceExchangerProvider(
  id = "currencylayer",
  label = "Currencylayer",
  display_label = "currencylayer.com",
  historical_rates = TRUE,
  enterprise = TRUE,
  api_key= TRUE,
)

Hierarchy

Expanded class hierarchy of CurrencylayerExchanger

File

src/Plugin/Commerce/ExchangerProvider/CurrencylayerExchanger.php, line 19

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider
View source
class CurrencylayerExchanger extends ExchangerProviderRemoteBase {

  /**
   * {@inheritdoc}
   */
  public function apiUrl() {
    if ($this
      ->isEnterprise()) {
      return 'https://api.currencylayer.com/live';
    }
    return 'http://api.currencylayer.com/live';
  }

  /**
   * {@inheritdoc}
   */
  public function getRemoteData($base_currency = NULL) {
    $data = NULL;
    $options = [
      'query' => [
        'access_key' => $this
          ->getApiKey(),
      ],
    ];

    // Add base currency if we use enterprise model.
    if ($this
      ->isEnterprise()) {
      $options['query']['base'] = $base_currency;
    }
    $request = $this
      ->apiClient($options);
    if ($request) {
      $json = Json::decode($request);
      if (!empty($json['success'])) {

        // Leave base currency. In some cases we don't know base currency.
        // Currenclayer on free plan uses your address for base currency, and in
        // Drupal you could have different default value.
        $data['base'] = $json['source'];
        foreach ($json['quotes'] as $code => $rate) {
          $data['rates'][str_replace($json['source'], '', $code)] = $rate;
        }
      }
    }
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencylayerExchanger::apiUrl public function URL from remote provider upon API call should be made. Overrides ExchangerProviderRemoteInterface::apiUrl
CurrencylayerExchanger::getRemoteData public function Fetch external data. Overrides ExchangerProviderRemoteInterface::getRemoteData
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
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.