You are here

abstract class AbstractExchangerCalculator in Commerce Exchanger 8

Class AbstractExchangerCalculator.

@package Drupal\commerce_exchanger

Hierarchy

Expanded class hierarchy of AbstractExchangerCalculator

File

src/AbstractExchangerCalculator.php, line 16

Namespace

Drupal\commerce_exchanger
View 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

Namesort descending Modifiers Type Description Overrides
AbstractExchangerCalculator::$configFactory protected property Config factory.
AbstractExchangerCalculator::$entityTypeManager protected property Entity manager.
AbstractExchangerCalculator::$providers protected property List of providers.
AbstractExchangerCalculator::$rounder protected property Drupal commerce price rounder service.
AbstractExchangerCalculator::getExchangeRates public function Get all exchange rates. Overrides ExchangerCalculatorInterface::getExchangeRates
AbstractExchangerCalculator::priceConversion public function Preform currency conversion for prices. Overrides ExchangerCalculatorInterface::priceConversion
AbstractExchangerCalculator::__construct public function DefaultExchangerCalculator constructor.
ExchangerCalculatorInterface::getExchangerId public function Return configuration file of active provider or NULL. 1