Skip to content

Instantly share code, notes, and snippets.

@harshvardhanmalpani
Created July 19, 2018 08:45
Show Gist options
  • Save harshvardhanmalpani/9721989801709f308cf7791b35d571d0 to your computer and use it in GitHub Desktop.
Save harshvardhanmalpani/9721989801709f308cf7791b35d571d0 to your computer and use it in GitHub Desktop.
fixer.io replacement
<?php
#/vendor/magento/module-directory/Model/Currency/Import/FixerIo.php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Directory\Model\Currency\Import;
/**
* Currency rate import model (From http://fixer.io/)
*/
class FixerIo extends \Magento\Directory\Model\Currency\Import\AbstractImport
{
/**
* @var string
*/
#const CURRENCY_CONVERTER_URL = 'http://api.fixer.io/latest?base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}}';
#const CURRENCY_CONVERTER_URL = 'http://data.fixer.io/api/latest?access_key=b74d411f6c842debd4f0d668766ddcd0&base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}}';
const CURRENCY_CONVERTER_URL = 'https://free.currencyconverterapi.com/api/v6/convert?q=INR_USD&compact=y';
/**
* Http Client Factory
*
* @var \Magento\Framework\HTTP\ZendClientFactory
*/
protected $httpClientFactory;
/**
* Core scope config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;
/**
* Initialize dependencies
*
* @param \Magento\Directory\Model\CurrencyFactory $currencyFactory
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
*/
public function __construct(
\Magento\Directory\Model\CurrencyFactory $currencyFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
) {
parent::__construct($currencyFactory);
$this->scopeConfig = $scopeConfig;
$this->httpClientFactory = $httpClientFactory;
}
/**
* {@inheritdoc}
*/
public function fetchRates()
{
$data = [];
$currencies = $this->_getCurrencyCodes();
$defaultCurrencies = $this->_getDefaultCurrencyCodes();
foreach ($defaultCurrencies as $currencyFrom) {
if (!isset($data[$currencyFrom])) {
$data[$currencyFrom] = [];
}
$data = $this->convertBatch($data, $currencyFrom, $currencies);
ksort($data[$currencyFrom]);
}
return $data;
}
/**
* Return currencies convert rates in batch mode
*
* @param array $data
* @param string $currencyFrom
* @param array $currenciesTo
* @return array
*/
private function convertBatch($data, $currencyFrom, $currenciesTo)
{
$currenciesStr = implode(',', $currenciesTo);
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
$url = str_replace('{{CURRENCY_TO}}', $currenciesStr, $url);
set_time_limit(0);
try {
$response = $this->getServiceResponse($url);
} finally {
ini_restore('max_execution_time');
}
//$this->_messages[]=print_r($response,1);
foreach ($currenciesTo as $currencyTo) {
if ($currencyFrom == $currencyTo) {
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(1);
} else {
if (empty($response["{$currencyFrom}_{$currencyTo}"]['val'])) {
$this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $currencyTo);
$data[$currencyFrom][$currencyTo] = null;
} else {
$data[$currencyFrom][$currencyTo] = $this->_numberFormat(
(double)$response["{$currencyFrom}_{$currencyTo}"]['val']
);
}
}
}
return $data;
}
/**
* Get Fixer.io service response
*
* @param string $url
* @param int $retry
* @return array
*/
private function getServiceResponse($url, $retry = 0)
{
/** @var \Magento\Framework\HTTP\ZendClient $httpClient */
$httpClient = $this->httpClientFactory->create();
$response = [];
try {
$jsonResponse = $httpClient->setUri(
$url
)->setConfig(
[
'timeout' => $this->scopeConfig->getValue(
'currency/fixerio/timeout',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
),
]
)->request(
'GET'
)->getBody();
$response = json_decode($jsonResponse, true);
} catch (\Exception $e) {
if ($retry == 0) {
$response = $this->getServiceResponse($url, 1);
}
}
return $response;
}
/**
* {@inheritdoc}
*/
protected function _convert($currencyFrom, $currencyTo)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment