Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AlphaVantage adapter #55

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Adapter/AdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public function createOerAdapter($adapterClass = null)
}

/**
* Create an YahooCurrencyAdapter.
* Create an AlphaCurrencyAdapter.
*
* @return Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter
* @return Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter
*/
public function createYahooAdapter($adapterClass = null)
public function createAlphaAdapter($adapterClass = null)
{
if (null == $adapterClass) {
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter';
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter';
}

return $this->create($adapterClass);
Expand Down
112 changes: 112 additions & 0 deletions Adapter/AlphaCurrencyAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace Lexik\Bundle\CurrencyBundle\Adapter;

use Lexik\Bundle\CurrencyBundle\Exception\CurrencyNotFoundException;

/**
* Alpha Vantage Currency Adapter
*
* @author Jonas Dambacher <[email protected]>
*/
class AlphaCurrencyAdapter extends AbstractCurrencyAdapter
{
/**
* @var string
*/
private $apiKey;

/**
* @var array
*/
private $currencyCodes = array();

/**
* Set the Alpha Vantage API key.
*
* @param string $apiKey
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;
}

/**
* Init object storage
*/
public function attachAll()
{
foreach ($this->managedCurrencies as $managedCurrency) {
$this->addCurrency($managedCurrency);
}

$defaultRate = 1;

// Add default currency (euro in this example)
$defaultCurrency = new $this->currencyClass;
$defaultCurrency->setCode('EUR');
$defaultCurrency->setRate($defaultRate);

$this[$defaultCurrency->getCode()] = $defaultCurrency;

$currencies = [];
// Build query
foreach ($this->currencyCodes as $index=>$currencyCode) {
$currencies[$currencyCode] = $this->getExchangeRate($currencyCode);
}

foreach ($currencies as $code => $rate) {
if (in_array($code, $this->managedCurrencies)) { // you can check if the currency is in the managed currencies
$currency = new $this->currencyClass;
$currency->setCode($code);
$currency->setRate($rate);

$this[$currency->getCode()] = $currency;
}
}

// get the default rate from the default currency defined in the configuration
if (isset($this[$this->defaultCurrency])) {
$defaultRate = $this[$this->defaultCurrency]->getRate();
}

// convert rates according to the default one.
$this->convertAll($defaultRate);
}

/**
* {@inheritdoc}
*/
public function getIdentifier()
{
return 'alpha';
}

/**
* Add currency to the query
*
* @param $code
*/
private function addCurrency($code)
{
$this->currencyCodes[] = $code;
}

/**
* @param $currency
* @return float
*/
private function getExchangeRate($currency)
{
if (!$this->apiKey) {
throw new \InvalidArgumentException('ALPHA_API_KEY must be set in order to use AlphaCurrencyAdapter');
}

$url = sprintf('https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=%s&to_currency=%s&apikey=%s', $this->defaultCurrency, $currency, $this->apiKey);
$json = file_get_contents($url);
$data = json_decode($json, true);

return $data['Realtime Currency Exchange Rate']['5. Exchange Rate'];
}

}
134 changes: 0 additions & 134 deletions Adapter/YahooCurrencyAdapter.php

This file was deleted.

4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public function getConfigTreeBuilder()
->defaultValue(null)
->end()

->scalarNode('yahoo_url')
->scalarNode('alpha_api_key')
->cannotBeEmpty()
->defaultValue('https://query.yahooapis.com/v1/public/yql')
->defaultValue(null)
->end()

->end()
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/LexikCurrencyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('lexik_currency.ecb_url', $config['ecb_url']);
$container->setParameter('lexik_currency.oer_url', $config['oer_url']);
$container->setParameter('lexik_currency.oer_app_id', $config['oer_app_id']);
$container->setParameter('lexik_currency.yahoo_url', $config['yahoo_url']);
$container->setParameter('lexik_currency.alpha_api_key', $config['alpha_api_key']);
$container->setParameter('lexik_currency.decimal_part.precision', $config['decimal_part']['precision']);
$container->setParameter('lexik_currency.decimal_part.round_mode', $config['decimal_part']['round_mode']);

Expand Down
12 changes: 6 additions & 6 deletions Resources/config/adapters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parameter key="lexik_currency.abstract_adapter.class">Lexik\Bundle\CurrencyBundle\Adapter\AbstractCurrencyAdapter</parameter>
<parameter key="lexik_currency.ecb_adapter.class">Lexik\Bundle\CurrencyBundle\Adapter\EcbCurrencyAdapter</parameter>
<parameter key="lexik_currency.oer_adapter.class">Lexik\Bundle\CurrencyBundle\Adapter\OerCurrencyAdapter</parameter>
<parameter key="lexik_currency.yahoo_adapter.class">Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter</parameter>
<parameter key="lexik_currency.alpha_adapter.class">Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter</parameter>
<parameter key="lexik_currency.doctrine_adapter.class">Lexik\Bundle\CurrencyBundle\Adapter\DoctrineCurrencyAdapter</parameter>
<parameter key="lexik_currency.doctrine.orm.entity_manager">default</parameter>
</parameters>
Expand Down Expand Up @@ -56,12 +56,12 @@
<tag name="lexik_currency.adapter" alias="oer_currency_adapter" />
</service>

<service id="lexik_currency.yahoo_adapter" class="%lexik_currency.yahoo_adapter.class%" parent="lexik_currency.abstract_adapter">
<factory service="lexik_currency.adapter_factory" method="createYahooAdapter" />
<call method="setYahooUrl">
<argument>%lexik_currency.yahoo_url%</argument>
<service id="lexik_currency.alpha_adapter" class="%lexik_currency.alpha_adapter.class%" parent="lexik_currency.abstract_adapter">
<factory service="lexik_currency.adapter_factory" method="createAlphaAdapter" />
<call method="setApiKey">
<argument>%lexik_currency.alpha_api_key%</argument>
</call>
<tag name="lexik_currency.adapter" alias="yahoo_currency_adapter" />
<tag name="lexik_currency.adapter" alias="alpha_currency_adapter" />
</service>
</services>

Expand Down
6 changes: 3 additions & 3 deletions Tests/Unit/Adapter/AdapterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public function testCreateEcbAdapter()
$this->assertEquals(0, count($adapter));
}

public function testCreateYahooAdapter()
public function testCreateAlphaAdapter()
{
$factory = new AdapterFactory($this->doctrine, 'EUR', array('EUR', 'USD'), self::CURRENCY_ENTITY);
$adapter = $factory->createYahooAdapter();
$adapter = $factory->createAlphaAdapter();

$this->assertInstanceOf('Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter', $adapter);
$this->assertInstanceOf('Lexik\Bundle\CurrencyBundle\Adapter\AlphaCurrencyAdapter', $adapter);
$this->assertEquals('EUR', $adapter->getDefaultCurrency());
$this->assertEquals(array('EUR', 'USD'), $adapter->getManagedCurrencies());
$this->assertEquals(0, count($adapter));
Expand Down