You are here

class FixedRates in Currency 8.3

Provides manually entered exchange rates.

Plugin annotation


@CurrencyExchangeRateProvider(
  id = "currency_fixed_rates",
  label = @Translation("Fixed rates"),
  operations_provider = "\Drupal\currency\Plugin\Currency\ExchangeRateProvider\FixedRatesOperationsProvider"
)

Hierarchy

Expanded class hierarchy of FixedRates

4 files declare their use of FixedRates
FixedRatesFormTest.php in tests/src/Unit/Controller/FixedRatesFormTest.php
FixedRatesFormTest.php in tests/src/Unit/Form/FixedRatesFormTest.php
FixedRatesOverviewTest.php in tests/src/Unit/Controller/FixedRatesOverviewTest.php
FixedRatesTest.php in tests/src/Unit/Plugin/Currency/ExchangeRateProvider/FixedRatesTest.php

File

src/Plugin/Currency/ExchangeRateProvider/FixedRates.php, line 20

Namespace

Drupal\currency\Plugin\Currency\ExchangeRateProvider
View source
class FixedRates extends PluginBase implements ExchangeRateProviderInterface, ContainerFactoryPluginInterface {
  use FixedExchangeRateProviderTrait;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new class instance
   *
   * @param mixed[] $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed[] $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, ConfigFactoryInterface $config_factory) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function loadAll() {
    $rates_data = $this->configFactory
      ->get('currency.exchanger.fixed_rates')
      ->get('rates');
    $rates = array();
    foreach ($rates_data as $rate_data) {
      $rates[$rate_data['currency_code_from']][$rate_data['currency_code_to']] = $rate_data['rate'];
    }
    return $rates;
  }

  /**
   * Saves an exchange rate.
   *
   * @param string $currency_code_from
   * @param string $currency_code_to
   * @param string $rate
   *
   * @return $this
   */
  public function save($currency_code_from, $currency_code_to, $rate) {
    $config = $this->configFactory
      ->getEditable('currency.exchanger.fixed_rates');
    $rates = $this
      ->loadAll();
    $rates[$currency_code_from][$currency_code_to] = $rate;

    // Massage the rates into a format that can be stored, as associative
    // arrays are not supported by the config system
    $rates_data = array();
    foreach ($rates as $currency_code_from => $currency_code_from_rates) {
      foreach ($currency_code_from_rates as $currency_code_to => $rate) {
        $rates_data[] = array(
          'currency_code_from' => $currency_code_from,
          'currency_code_to' => $currency_code_to,
          'rate' => $rate,
        );
      }
    }
    $config
      ->set('rates', $rates_data);
    $config
      ->save();
    return $this;
  }

  /**
   * Deletes an exchange rate.
   *
   * @param string $currency_code_from
   * @param string $currency_code_to
   *
   * @return NULL
   */
  public function delete($currency_code_from, $currency_code_to) {
    $config = $this->configFactory
      ->getEditable('currency.exchanger.fixed_rates');
    $rates = $config
      ->get('rates');
    foreach ($rates as $i => $rate) {
      if ($rate['currency_code_from'] == $currency_code_from && $rate['currency_code_to'] == $currency_code_to) {
        unset($rates[$i]);
      }
    }
    $config
      ->set('rates', $rates);
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExchangeRateProviderInterface::load public function 1
ExchangeRateProviderInterface::loadMultiple public function 1
FixedRates::$configFactory protected property The config factory.
FixedRates::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FixedRates::delete public function Deletes an exchange rate.
FixedRates::loadAll public function
FixedRates::save public function Saves an exchange rate.
FixedRates::__construct public function Constructs a new class instance Overrides PluginBase::__construct
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.