abstract class AbstractExchangerCalculator in Commerce Exchanger 8
Class AbstractExchangerCalculator.
@package Drupal\commerce_exchanger
Hierarchy
- class \Drupal\commerce_exchanger\AbstractExchangerCalculator implements ExchangerCalculatorInterface
Expanded class hierarchy of AbstractExchangerCalculator
File
- src/
AbstractExchangerCalculator.php, line 16
Namespace
Drupal\commerce_exchangerView source
abstract class AbstractExchangerCalculator implements ExchangerCalculatorInterface {
/**
* Entity manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* List of providers.
*
* @var \Drupal\commerce_exchanger\Entity\ExchangeRatesInterface[]
*/
protected $providers;
/**
* Config factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* Drupal commerce price rounder service.
*
* @var \Drupal\commerce_price\RounderInterface
*/
protected $rounder;
/**
* DefaultExchangerCalculator constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Entity type manager.
* @param \Drupal\Core\Config\ConfigFactory $config_factory
* Drupal config factory.
* @param \Drupal\commerce_price\RounderInterface $rounder
* Drupal commerce price rounder service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactory $config_factory, RounderInterface $rounder) {
$this->configFactory = $config_factory;
$this->providers = $entity_type_manager
->getStorage('commerce_exchange_rates')
->loadMultiple();
$this->rounder = $rounder;
}
/**
* {@inheritdoc}
*/
public function priceConversion(Price $price, string $target_currency) {
$exchange_rates_config = $this
->getExchangerId();
if (empty($exchange_rates_config)) {
throw new ExchangeRatesDataMismatchException('Not any active Exchange rates present');
}
// Price currency.
$price_currency = $price
->getCurrencyCode();
// If someone is trying to convert same currency.
if ($price_currency == $target_currency) {
return $price;
}
$exchange_rates = $this
->getExchangeRates();
// Determine rate.
$rate = $exchange_rates[$price_currency][$target_currency]['value'] ?? 0;
// Don't allow multiply with zero or one.
if (empty($rate)) {
throw new ExchangeRatesDataMismatchException('There are no exchange rates set for ' . $price_currency . ' and ' . $target_currency);
}
// Convert amount to target currency.
$price = $price
->convert($target_currency, (string) $rate);
$price = $this->rounder
->round($price);
return $price;
}
/**
* {@inheritdoc}
*/
public function getExchangeRates() {
return $this->configFactory
->get($this
->getExchangerId())
->get('rates') ?? [];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AbstractExchangerCalculator:: |
protected | property | Config factory. | |
AbstractExchangerCalculator:: |
protected | property | Entity manager. | |
AbstractExchangerCalculator:: |
protected | property | List of providers. | |
AbstractExchangerCalculator:: |
protected | property | Drupal commerce price rounder service. | |
AbstractExchangerCalculator:: |
public | function |
Get all exchange rates. Overrides ExchangerCalculatorInterface:: |
|
AbstractExchangerCalculator:: |
public | function |
Preform currency conversion for prices. Overrides ExchangerCalculatorInterface:: |
|
AbstractExchangerCalculator:: |
public | function | DefaultExchangerCalculator constructor. | |
ExchangerCalculatorInterface:: |
public | function | Return configuration file of active provider or NULL. | 1 |