You are here

class PluginBasedExchangeRateProvider in Currency 8.3

Provides currency exchange rates through plugins.

Hierarchy

Expanded class hierarchy of PluginBasedExchangeRateProvider

4 files declare their use of PluginBasedExchangeRateProvider
PluginBasedExchangeRateProviderForm.php in src/Form/PluginBasedExchangeRateProviderForm.php
PluginBasedExchangeRateProviderFormTest.php in tests/src/Unit/Controller/PluginBasedExchangeRateProviderFormTest.php
PluginBasedExchangeRateProviderFormTest.php in tests/src/Unit/Form/PluginBasedExchangeRateProviderFormTest.php
PluginBasedExchangeRateProviderTest.php in tests/src/Unit/PluginBasedExchangeRateProviderTest.php
1 string reference to 'PluginBasedExchangeRateProvider'
currency.services.yml in ./currency.services.yml
currency.services.yml
1 service uses PluginBasedExchangeRateProvider
currency.exchange_rate_provider in ./currency.services.yml
Drupal\currency\PluginBasedExchangeRateProvider

File

src/PluginBasedExchangeRateProvider.php, line 12

Namespace

Drupal\currency
View source
class PluginBasedExchangeRateProvider extends AbstractStackedExchangeRateProvider {

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

  /**
   * The currency exchanger plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $currencyExchangeRateProviderManager;

  /**
   * Constructs a new instance.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $currency_exchange_rate_provider_manager
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   */
  function __construct(PluginManagerInterface $currency_exchange_rate_provider_manager, ConfigFactoryInterface $config_factory) {
    $this->currencyExchangeRateProviderManager = $currency_exchange_rate_provider_manager;
    $this->configFactory = $config_factory;
  }

  /**
   * Loads the plugin configuration.
   *
   * @return bool[]
   *   Keys are currency_exchanger plugin names. Values are booleans that
   *   describe whether the plugins are enabled. Items are ordered by weight.
   */
  public function loadConfiguration() {
    $definitions = $this->currencyExchangeRateProviderManager
      ->getDefinitions();
    $configuration_data = $this->configFactory
      ->get('currency.exchange_rate_provider')
      ->get('plugins');
    $configuration = array();
    foreach ($configuration_data as $data) {
      $configuration[$data['plugin_id']] = $data['status'];
    }
    return $configuration + array_fill_keys(array_keys($definitions), FALSE);
  }

  /**
   * Saves the configuration.
   *
   * @param bool[] $configuration
   *   Keys are currency_exchanger plugin names. Values are booleans that
   *   describe whether the plugins are enabled. Items are ordered by weight.
   *
   * @return $this
   */
  public function saveConfiguration(array $configuration) {
    $config = $this->configFactory
      ->getEditable('currency.exchange_rate_provider');

    // Massage the configuration into a format that can be stored, as
    // associative arrays are not supported by the config system
    $configuration_data = array();
    foreach ($configuration as $plugin_id => $status) {
      $configuration_data[] = array(
        'plugin_id' => $plugin_id,
        'status' => $status,
      );
    }
    $config
      ->set('plugins', $configuration_data);
    $config
      ->save();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  protected function getExchangeRateProviders() {
    $plugin_ids = array_keys(array_filter($this
      ->loadConfiguration()));
    $plugins = array();
    foreach ($plugin_ids as $plugin_id) {
      $plugins[$plugin_id] = $this->currencyExchangeRateProviderManager
        ->createInstance($plugin_id);
    }
    return $plugins;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBasedExchangeRateProvider::$configFactory protected property The configuration factory.
PluginBasedExchangeRateProvider::$currencyExchangeRateProviderManager protected property The currency exchanger plugin manager.
PluginBasedExchangeRateProvider::getExchangeRateProviders protected function
PluginBasedExchangeRateProvider::loadConfiguration public function Loads the plugin configuration.
PluginBasedExchangeRateProvider::saveConfiguration public function Saves the configuration.
PluginBasedExchangeRateProvider::__construct function Constructs a new instance.