From ebcad153a9840771a9073e3240fe8f92b7b6d2fc Mon Sep 17 00:00:00 2001 From: Andrea Zambon Date: Thu, 10 Jun 2021 15:37:42 +0200 Subject: [PATCH 1/7] Imports the system config arrangement from Magento 1.9.4.3 --- .../Philwinkle/Fixerio/Model/Import.php | 3 +-- .../Philwinkle/Fixerio/etc/config.xml | 5 +++-- .../Philwinkle/Fixerio/etc/system.xml | 19 ++++++++++++++----- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/app/code/community/Philwinkle/Fixerio/Model/Import.php b/app/code/community/Philwinkle/Fixerio/Model/Import.php index ac1c7f6..a65c18a 100644 --- a/app/code/community/Philwinkle/Fixerio/Model/Import.php +++ b/app/code/community/Philwinkle/Fixerio/Model/Import.php @@ -35,7 +35,7 @@ public function __construct() */ protected function _getConfigAccessKey() { - if ($accessKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('currency/fixerio/access_key'))) { + if ($accessKey = Mage::helper('core')->decrypt(Mage::getStoreConfig('currency/fixerio/api_key'))) { return $accessKey; } @@ -66,7 +66,6 @@ public function getEndpointUrl() */ protected function _convert($currencyFrom, $currencyTo, $retry = 0) { - $queryParams = array( 'access_key' => $this->_getConfigAccessKey(), 'symbols' => implode(',', array($currencyFrom, $currencyTo)) diff --git a/app/code/community/Philwinkle/Fixerio/etc/config.xml b/app/code/community/Philwinkle/Fixerio/etc/config.xml index 879d35a..9dcb1a6 100644 --- a/app/code/community/Philwinkle/Fixerio/etc/config.xml +++ b/app/code/community/Philwinkle/Fixerio/etc/config.xml @@ -30,8 +30,9 @@ - 60 - + 100 + + 1 diff --git a/app/code/community/Philwinkle/Fixerio/etc/system.xml b/app/code/community/Philwinkle/Fixerio/etc/system.xml index 7337830..1012517 100644 --- a/app/code/community/Philwinkle/Fixerio/etc/system.xml +++ b/app/code/community/Philwinkle/Fixerio/etc/system.xml @@ -10,24 +10,33 @@ 0 0 + + + select + adminhtml/system_config_source_yesno + 0 + 1 + 0 + 0 + text - 0 + 10 1 0 0 - - + + Fixer.io]]> obscure adminhtml/system_config_backend_encrypted - 1 + 20 1 0 0 - + From 834046b47c7cdbbfc7cce75dafc22efd66a7f558 Mon Sep 17 00:00:00 2001 From: Andrea Zambon Date: Thu, 10 Jun 2021 15:45:22 +0200 Subject: [PATCH 2/7] Makes the module's implementation replace Magento's one, if present --- .../community/Philwinkle/Fixerio/Helper/Data.php | 3 --- .../community/Philwinkle/Fixerio/Model/Import.php | 15 +++++++++++---- .../community/Philwinkle/Fixerio/etc/config.xml | 11 ++++++++++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/app/code/community/Philwinkle/Fixerio/Helper/Data.php b/app/code/community/Philwinkle/Fixerio/Helper/Data.php index 7f3693f..eae519c 100644 --- a/app/code/community/Philwinkle/Fixerio/Helper/Data.php +++ b/app/code/community/Philwinkle/Fixerio/Helper/Data.php @@ -2,7 +2,4 @@ class Philwinkle_Fixerio_Helper_Data extends Mage_Core_Helper_Abstract { - - } - diff --git a/app/code/community/Philwinkle/Fixerio/Model/Import.php b/app/code/community/Philwinkle/Fixerio/Model/Import.php index a65c18a..c07c24a 100644 --- a/app/code/community/Philwinkle/Fixerio/Model/Import.php +++ b/app/code/community/Philwinkle/Fixerio/Model/Import.php @@ -4,17 +4,25 @@ * */ +//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')) { + class Philwinkle_Fixerio_Model_Import_Base extends Mage_Directory_Model_Currency_Import_Fixerio {} +} else { + class Philwinkle_Fixerio_Model_Import_Base extends Mage_Directory_Model_Currency_Import_Abstract {} +} + /** * Philwinkle_Fixerio_Model_Import class * * @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_Import_Base { - protected $_url = 'http://data.fixer.io/api/latest'; - protected $_messages = array(); + protected $_messages = []; /** * HTTP client @@ -119,5 +127,4 @@ protected function _convert($currencyFrom, $currencyTo, $retry = 0) return null; } - } diff --git a/app/code/community/Philwinkle/Fixerio/etc/config.xml b/app/code/community/Philwinkle/Fixerio/etc/config.xml index 9dcb1a6..b883a91 100644 --- a/app/code/community/Philwinkle/Fixerio/etc/config.xml +++ b/app/code/community/Philwinkle/Fixerio/etc/config.xml @@ -10,7 +10,7 @@ - Fixer.io + Fixer.IO fixerio/import @@ -20,6 +20,15 @@ Philwinkle_Fixerio_Model + + + + Philwinkle_Fixerio_Model_Import + + From e231eb62215aca1c3a957faf89fe1b33befb06c4 Mon Sep 17 00:00:00 2001 From: Andrea Zambon Date: Thu, 10 Jun 2021 16:02:10 +0200 Subject: [PATCH 3/7] Makes sure to disable Magento's Fixerio implementation --- app/code/community/Philwinkle/Fixerio/Model/Import.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/code/community/Philwinkle/Fixerio/Model/Import.php b/app/code/community/Philwinkle/Fixerio/Model/Import.php index c07c24a..0d5df57 100644 --- a/app/code/community/Philwinkle/Fixerio/Model/Import.php +++ b/app/code/community/Philwinkle/Fixerio/Model/Import.php @@ -63,6 +63,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 * From aff199c168d0a6b7140ac36542a9666e21f457d0 Mon Sep 17 00:00:00 2001 From: Andrea Zambon Date: Thu, 10 Jun 2021 16:02:58 +0200 Subject: [PATCH 4/7] Refactoring --- .../Philwinkle/Fixerio/Model/Import.php | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/app/code/community/Philwinkle/Fixerio/Model/Import.php b/app/code/community/Philwinkle/Fixerio/Model/Import.php index 0d5df57..158cc1f 100644 --- a/app/code/community/Philwinkle/Fixerio/Model/Import.php +++ b/app/code/community/Philwinkle/Fixerio/Model/Import.php @@ -21,6 +21,16 @@ class Philwinkle_Fixerio_Model_Import_Base extends Mage_Directory_Model_Currency */ class Philwinkle_Fixerio_Model_Import extends Philwinkle_Fixerio_Model_Import_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 = []; @@ -36,23 +46,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/api_key'))) { - return $accessKey; - } - - $this->_messages[] = Mage::helper('directory') - ->__('Fixer.io access key missing. Please obtain access key from fixer.io.'); - - return false; - } - /** * getEndpointUrl * @@ -94,7 +87,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(); @@ -133,4 +126,22 @@ 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; + } } From d5fd4650badec0ee29736e7f01ff13b15a6a0369 Mon Sep 17 00:00:00 2001 From: Andrea Zambon Date: Thu, 10 Jun 2021 16:03:53 +0200 Subject: [PATCH 5/7] Makes the recursive call more generic in case one wants to retry more than two times by changing the if() condition --- app/code/community/Philwinkle/Fixerio/Model/Import.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/community/Philwinkle/Fixerio/Model/Import.php b/app/code/community/Philwinkle/Fixerio/Model/Import.php index 158cc1f..2176e7f 100644 --- a/app/code/community/Philwinkle/Fixerio/Model/Import.php +++ b/app/code/community/Philwinkle/Fixerio/Model/Import.php @@ -118,7 +118,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); From 8b8b42930aead16518a8815bf920b533e9e1ff18 Mon Sep 17 00:00:00 2001 From: Andrea Zambon Date: Thu, 10 Jun 2021 16:13:37 +0200 Subject: [PATCH 6/7] Declares the middle class abstract since it doesn't implement any abstract method of the base abstract class and thus cannot be concrete --- app/code/community/Philwinkle/Fixerio/Model/Import.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/community/Philwinkle/Fixerio/Model/Import.php b/app/code/community/Philwinkle/Fixerio/Model/Import.php index 2176e7f..f614ec9 100644 --- a/app/code/community/Philwinkle/Fixerio/Model/Import.php +++ b/app/code/community/Philwinkle/Fixerio/Model/Import.php @@ -8,9 +8,9 @@ //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')) { - class Philwinkle_Fixerio_Model_Import_Base extends Mage_Directory_Model_Currency_Import_Fixerio {} + abstract class Philwinkle_Fixerio_Model_Import_Base extends Mage_Directory_Model_Currency_Import_Fixerio {} } else { - class Philwinkle_Fixerio_Model_Import_Base extends Mage_Directory_Model_Currency_Import_Abstract {} + abstract class Philwinkle_Fixerio_Model_Import_Base extends Mage_Directory_Model_Currency_Import_Abstract {} } /** From a5e633098969efbde80c20492116bbaaa9c3cb3b Mon Sep 17 00:00:00 2001 From: Andrea Zambon Date: Thu, 10 Jun 2021 16:17:39 +0200 Subject: [PATCH 7/7] Moves the middle class to a separate file --- app/code/community/Philwinkle/Fixerio/Model/Base.php | 10 ++++++++++ .../community/Philwinkle/Fixerio/Model/Import.php | 11 +---------- 2 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 app/code/community/Philwinkle/Fixerio/Model/Base.php diff --git a/app/code/community/Philwinkle/Fixerio/Model/Base.php b/app/code/community/Philwinkle/Fixerio/Model/Base.php new file mode 100644 index 0000000..0aab5c5 --- /dev/null +++ b/app/code/community/Philwinkle/Fixerio/Model/Base.php @@ -0,0 +1,10 @@ +