PluginBasedExchangeRateProvider.php in Currency 8.3
File
src/PluginBasedExchangeRateProvider.php
View source
<?php
namespace Drupal\currency;
use Commercie\CurrencyExchange\AbstractStackedExchangeRateProvider;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
class PluginBasedExchangeRateProvider extends AbstractStackedExchangeRateProvider {
protected $configFactory;
protected $currencyExchangeRateProviderManager;
function __construct(PluginManagerInterface $currency_exchange_rate_provider_manager, ConfigFactoryInterface $config_factory) {
$this->currencyExchangeRateProviderManager = $currency_exchange_rate_provider_manager;
$this->configFactory = $config_factory;
}
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);
}
public function saveConfiguration(array $configuration) {
$config = $this->configFactory
->getEditable('currency.exchange_rate_provider');
$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;
}
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;
}
}