Skip to content

Commit

Permalink
Merge pull request #35 from mvar/driver_config
Browse files Browse the repository at this point in the history
Simplify driver config
  • Loading branch information
saimaz committed Dec 18, 2015
2 parents 5283d10 + b849711 commit 45f7efb
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 33 deletions.
20 changes: 7 additions & 13 deletions DependencyInjection/Configuration.php
Expand Up @@ -59,19 +59,13 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('driver')
->children()
->scalarNode('service')
->defaultValue('ongr_currency_exchange.ecb_driver')
->info('set currency driver service id')
->end()
->arrayNode('setters')
->prototype('array')
->prototype('scalar')
->end()
->end()
->end()
->end()
->scalarNode('driver')
->defaultValue('ongr_currency_exchange.ecb_driver')
->info('Elasticsearch manager to use in router')
->end()
->scalarNode('open_exchange_rates_api_id')
->defaultNull()
->info('Open Exchange Rates API ID')
->end()
->end();

Expand Down
22 changes: 13 additions & 9 deletions DependencyInjection/ONGRCurrencyExchangeExtension.php
Expand Up @@ -11,13 +11,14 @@

namespace ONGR\CurrencyExchangeBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration.
Expand Down Expand Up @@ -50,14 +51,17 @@ public function load(array $configs, ContainerBuilder $container)
*/
private function configCurrencyRatesService(array $config, ContainerBuilder $container)
{
$driver = $config['driver']['service'];
$setters = $config['driver']['setters'];
$driver = $config['driver'];
$ecbApiId = $config['open_exchange_rates_api_id'];

if ($driver == 'ongr_currency_exchange.open_exchange_driver' && !$ecbApiId) {
throw new InvalidConfigurationException(
'"open_exchange_rates_api_id" must be set when using ' .
'"ongr_currency_exchange.open_exchange_driver" driver.'
);
}

if ($container->hasDefinition($driver)) {
if (!empty($setters)) {
foreach ($setters as $name => $value) {
$container->findDefinition($driver)->addMethodCall($name, array_values($value));
}
}
$def = new Definition(
$container->getParameter('ongr_currency_exchange.currency_rates_service.class'),
[
Expand Down
2 changes: 0 additions & 2 deletions README.md
Expand Up @@ -118,8 +118,6 @@ ongr_currency_exchange:
currencies:
EUR: "%s €" # %s stands for the price itself
USD: "$ %s"
driver:
service: ongr_currency_exchange.ecb_driver
```

That's it for setup, jump to the next chapter to learn how to use this bundle.
Expand Down
3 changes: 1 addition & 2 deletions Resources/doc/currency_driver.md
Expand Up @@ -78,8 +78,7 @@ Last, set new driver in config:
# app/config/config.yml
ongr_currency_exchange:
# ...
driver:
service: app.currency_driver
driver: app.currency_driver
```

Now you can test your custom driver by running currency update command:
Expand Down
5 changes: 4 additions & 1 deletion Tests/Unit/DependencyInjection/ConfigurationTest.php
Expand Up @@ -32,7 +32,10 @@ public function testConfiguration()
'default_currency' => 'EUR',
'currencies' => [],
'separators' => ['decimal' => ',', 'thousands' => '.'],
'driver' => 'ongr_currency_exchange.ecb_driver',
'open_exchange_rates_api_id' => null,
];
$this->assertEquals($processorConfig, $expectedConfiguration);

$this->assertEquals($expectedConfiguration, $processorConfig);
}
}
Expand Up @@ -34,7 +34,7 @@ public function getTestLoadCurrencyData()

$config = [
'cache' => 'stash.memcache',
'driver' => ['service' => 'ongr_currency_exchange.open_exchange_driver'],
'driver' => 'ongr_currency_exchange.ecb_driver',
];
// Case #0 we need currency rates service.
$out[] = [$config, $container, 'ongr_currency_exchange.currency_rates_service'];
Expand Down Expand Up @@ -71,7 +71,6 @@ public function testDefaultCurrency()
$config = [
'ongr_currency_exchange' => [
'cache' => 'stash.files_cache',
'driver' => [],
],
];
$extension = new ONGRCurrencyExchangeExtension();
Expand All @@ -81,4 +80,23 @@ public function testDefaultCurrency()
$this->assertTrue($container->hasParameter('ongr_currency_exchange.default_currency'));
$this->assertEquals('EUR', $container->getParameter('ongr_currency_exchange.default_currency'));
}

/**
* Test if exception is thrown when Open Exchange Rates driver is configured without API ID.
*
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage "open_exchange_rates_api_id" must be set
*/
public function testOpenExchangeRatesApiIdMissing()
{
$config = [
'ongr_currency_exchange' => [
'cache' => 'stash.files_cache',
'driver' => 'ongr_currency_exchange.open_exchange_driver',
],
];

$extension = new ONGRCurrencyExchangeExtension();
$extension->load($config, new ContainerBuilder());
}
}
6 changes: 2 additions & 4 deletions Tests/app/config/config_test.yml
Expand Up @@ -67,10 +67,8 @@ ongr_currency_exchange:
currencies:
EUR: "%s €"
USD: "$ %s"
driver:
service: ongr_currency_exchange.open_exchange_driver
setters:
setAppId: ['8b447edc6e0e4661b584772ab6aa7611']
driver: ongr_currency_exchange.open_exchange_driver
open_exchange_rates_api_id: '8b447edc6e0e4661b584772ab6aa7611'

services:
custom_exchange_driver_service:
Expand Down

0 comments on commit 45f7efb

Please sign in to comment.