You are here

public function FixerExchanger::getRemoteData in Commerce Exchanger 8

Fetch external data.

Parameters

string|null $base_currency: If we fetch data based on specific currency.

Overrides ExchangerProviderRemoteInterface::getRemoteData

File

src/Plugin/Commerce/ExchangerProvider/FixerExchanger.php, line 31

Class

FixerExchanger
Provides the Fixer.io exchange rates.

Namespace

Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider

Code

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

  // Add base currency if we use enterprise model.
  if (!empty($base_currency) && $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.
      // Fixer.io on free plan uses your address for base currency, and in
      // Drupal you could have different default value.
      unset($json['timestamp'], $json['success'], $json['date']);

      // This structure is what we need.
      $data = $json;
    }
  }
  return $data;
}