ExchangerProviderRemoteBase.php in Commerce Exchanger 8
File
src/Plugin/Commerce/ExchangerProvider/ExchangerProviderRemoteBase.php
View source
<?php
namespace Drupal\commerce_exchanger\Plugin\Commerce\ExchangerProvider;
use Drupal\commerce_exchanger\ExchangerProviderRates;
use GuzzleHttp\Exception\GuzzleException;
abstract class ExchangerProviderRemoteBase extends ExchangerProviderBase implements ExchangerProviderRemoteInterface {
public function getMethod() {
return $this->pluginDefinition['method'] ?? 'GET';
}
public function isEnterprise() {
return $this->configuration['enterprise'] ?? FALSE;
}
public function getApiKey() {
return $this->configuration['api_key'];
}
public function getAuthData() {
return $this->configuration['auth'];
}
public function getBaseCurrency() {
return $this->configuration['base_currency'];
}
public function useCrossSync() {
return $this->configuration['use_cross_sync'];
}
public function transformRates() {
return $this->pluginDefinition['transform_rates'] ?? FALSE;
}
public function apiClient(array $options) {
$data = [];
$client = $this->httpClientFactory
->fromOptions();
try {
$response = $client
->request($this
->getMethod(), $this
->apiUrl(), $options);
$data = $response
->getBody()
->getContents();
} catch (GuzzleException $e) {
$this->logger
->error($e
->getMessage());
}
return $data;
}
protected function buildExchangeRates() {
if ($this
->isEnterprise() && !$this
->useCrossSync()) {
return $this
->importEnterprise();
}
return $this
->importCrossSync();
}
protected function importCrossSync() {
$exchange_rates_data = $this
->processRemoteData();
return $this
->crossSyncCalculate($exchange_rates_data);
}
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;
}
protected function processRemoteData(string $base_currency = NULL) {
$remote_data = $this
->getRemoteData($base_currency);
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;
}
$exchange_rates['currencies'] = $this
->getCurrencies();
$exchange_rates['transform'] = $this
->transformRates();
return new ExchangerProviderRates($exchange_rates);
}
protected function crossSyncCalculate(ExchangerProviderRates $exchange_rates) {
$calculated_rates = [];
$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;
}
protected function mapExchangeRates(ExchangerProviderRates $exchange_rates) {
$mapping = $this->configFactory
->get($this
->getConfigName())
->getRawData();
$rates = $exchange_rates
->getRates();
$base_currency = $exchange_rates
->getBaseCurrency();
$calculated_rates = [];
$calculated_rates[$base_currency] = [];
foreach ($rates as $currency => $rate) {
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;
}
protected function recalculateRates(string $target_currency, ExchangerProviderRates $data) {
$rates = $data
->getRates();
$base_currency = $data
->getBaseCurrency();
$rate_target_currency = $rates[$target_currency] ?? 1;
$currency_default = round(1 / $rate_target_currency, 6);
$recalculated = [];
$recalculated[$base_currency] = $currency_default;
foreach ($rates as $currency => $rate) {
if ($currency !== $target_currency) {
$recalculated[$currency] = round($rate * $currency_default, 6);
}
}
return new ExchangerProviderRates([
'base' => $target_currency,
'rates' => $recalculated,
]);
}
public function import() {
$exchange_rates = $this
->buildExchangeRates();
if (!empty($exchange_rates)) {
$file = $this->configFactory
->getEditable($this
->getConfigName());
$file
->setData([
'rates' => $exchange_rates,
]);
$file
->save();
}
}
}