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

Makes the module compatible with Magento 1.9.4.3+ #34

Open
wants to merge 7 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
3 changes: 0 additions & 3 deletions app/code/community/Philwinkle/Fixerio/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

class Philwinkle_Fixerio_Helper_Data extends Mage_Core_Helper_Abstract
{


}

10 changes: 10 additions & 0 deletions app/code/community/Philwinkle/Fixerio/Model/Base.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

//For Magento 1.9.4.3+ this model will extend Magento's implementation, which in turn extends the abstract class
//For older Magento versions, this model extends the abstract class directly, as there is no Magento implementation
//to extend
if (class_exists('Mage_Directory_Model_Currency_Import_Fixerio')) {
abstract class Philwinkle_Fixerio_Model_Base extends Mage_Directory_Model_Currency_Import_Fixerio {}
} else {
abstract class Philwinkle_Fixerio_Model_Base extends Mage_Directory_Model_Currency_Import_Abstract {}
}
58 changes: 36 additions & 22 deletions app/code/community/Philwinkle/Fixerio/Model/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@
* @category Philwinkle
* @package Philwinkle_Fixerio
*/
class Philwinkle_Fixerio_Model_Import extends Mage_Directory_Model_Currency_Import_Abstract
class Philwinkle_Fixerio_Model_Import extends Philwinkle_Fixerio_Model_Base
{
/**
* XML path to Fixer.IO timeout setting
*/
const XML_PATH_FIXERIO_TIMEOUT = 'currency/fixerio/timeout';

/**
* XML path to Fixer.IO API key setting
*/
const XML_PATH_FIXERIO_API_KEY = 'currency/fixerio/api_key';

protected $_url = 'http://data.fixer.io/api/latest';
protected $_messages = array();
protected $_messages = [];

/**
* HTTP client
Expand All @@ -28,23 +37,6 @@ public function __construct()
$this->_httpClient = new Varien_Http_Client();
}

/**
* _getConfigAccessKey
*
* @return bool|mixed
*/
protected function _getConfigAccessKey()
{
if ($accessKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('currency/fixerio/access_key'))) {
return $accessKey;
}

$this->_messages[] = Mage::helper('directory')
->__('Fixer.io access key missing. Please obtain access key from fixer.io.');

return false;
}

/**
* getEndpointUrl
*
Expand All @@ -55,6 +47,12 @@ public function getEndpointUrl()
return $this->_url;
}

public function fetchRates()
{
//Make sure to disable Magento's implementation by invoking the implementation in the abstract class
return Mage_Directory_Model_Currency_Import_Abstract::fetchRates();
}

/**
* _convert
*
Expand All @@ -66,7 +64,6 @@ public function getEndpointUrl()
*/
protected function _convert($currencyFrom, $currencyTo, $retry = 0)
{

$queryParams = array(
'access_key' => $this->_getConfigAccessKey(),
'symbols' => implode(',', array($currencyFrom, $currencyTo))
Expand All @@ -81,7 +78,7 @@ protected function _convert($currencyFrom, $currencyTo, $retry = 0)

$response = $this->_httpClient
->setUri($url)
->setConfig(array('timeout' => Mage::getStoreConfig('currency/fixerio/timeout')))
->setConfig(array('timeout' => Mage::getStoreConfig(self::XML_PATH_FIXERIO_TIMEOUT)))
->request('GET')
->getBody();

Expand Down Expand Up @@ -112,7 +109,7 @@ protected function _convert($currencyFrom, $currencyTo, $retry = 0)
} catch (Exception $e) {
Mage::logException($e);
if ($retry === 0) {
return $this->_convert($currencyFrom, $currencyTo, 1);
return $this->_convert($currencyFrom, $currencyTo, $retry + 1);
}

$this->_messages[] = Mage::helper('directory')->__('Cannot retrieve rate from %s.', $url);
Expand All @@ -121,4 +118,21 @@ protected function _convert($currencyFrom, $currencyTo, $retry = 0)
return null;
}

/**
* _getConfigAccessKey
*
* @return bool|mixed
*/
protected function _getConfigAccessKey()
{
$accessKey = Mage::helper('core')->decrypt(Mage::getStoreConfig(self::XML_PATH_FIXERIO_API_KEY));
if ($accessKey) {
return $accessKey;
}

$this->_messages[] = Mage::helper('directory')
->__('Fixer.io access key missing. Please obtain it from fixer.io.');

return false;
}
}
16 changes: 13 additions & 3 deletions app/code/community/Philwinkle/Fixerio/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<import>
<services>
<fixerio>
<name>Fixer.io</name>
<name>Fixer.IO</name>
<model>fixerio/import</model>
</fixerio>
</services>
Expand All @@ -20,6 +20,15 @@
<fixerio>
<class>Philwinkle_Fixerio_Model</class>
</fixerio>
<directory>
<rewrite>
<!--
In Magento versions 1.9.4.3+ this module's implementation rewrites Magento's one, in case someone
tries to get an instance of that model directly
-->
<currency_import_fixerio>Philwinkle_Fixerio_Model_Import</currency_import_fixerio>
</rewrite>
</directory>
</models>
<helpers>
<fixerio>
Expand All @@ -30,8 +39,9 @@
<default>
<currency>
<fixerio>
<timeout>60</timeout>
<access_key></access_key>
<timeout>100</timeout>
<api_key backend_model="adminhtml/system_config_backend_encrypted"/>
<active>1</active>
</fixerio>
</currency>
</default>
Expand Down
19 changes: 14 additions & 5 deletions app/code/community/Philwinkle/Fixerio/etc/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,33 @@
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<active translate="label">
<label>Enabled</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</active>
<timeout translate="label">
<label>Connection Timeout in Seconds</label>
<frontend_type>text</frontend_type>
<sort_order>0</sort_order>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</timeout>
<access_key translate="label">
<label>API Access Key from fixer.io</label>
<api_key translate="label">
<label>API Access Key</label>
<comment><![CDATA[Sign up for a free account at <a href="https://fixer.io/signup/free" target="_blank">Fixer.io</a>]]></comment>
<frontend_type>obscure</frontend_type>
<backend_model>adminhtml/system_config_backend_encrypted</backend_model>
<sort_order>1</sort_order>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</access_key>
</api_key>
</fields>
</fixerio>
</groups>
Expand Down