6 declare(strict_types=1);
15 const CURRENCY_CONVERTER_URL =
'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra';
22 private $httpClientFactory;
39 \
Magento\Directory\Model\CurrencyFactory $currencyFactory,
41 \
Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
43 parent::__construct($currencyFactory);
44 $this->scopeConfig = $scopeConfig;
45 $this->httpClientFactory = $httpClientFactory;
57 foreach ($defaultCurrencies as $currencyFrom) {
58 if (!isset(
$data[$currencyFrom])) {
59 $data[$currencyFrom] = [];
61 $data = $this->convertBatch(
$data, $currencyFrom, $currencies);
62 ksort(
$data[$currencyFrom]);
75 private function convertBatch(
$data, $currencyFrom, $currenciesTo)
77 foreach ($currenciesTo as $to) {
80 $url = str_replace(
'{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
81 $url = str_replace(
'{{CURRENCY_TO}}', $to,
$url);
83 if ($currencyFrom == $to) {
87 $this->_messages[] =
__(
'We can\'t retrieve a rate from %1 for %2.',
$url, $to);
88 $data[$currencyFrom][$to] =
null;
91 (
double)
$response[$currencyFrom .
'_' . $to]
96 ini_restore(
'max_execution_time');
110 private function getServiceResponse(
$url, $retry = 0)
113 $httpClient = $this->httpClientFactory->create();
117 $jsonResponse = $httpClient->setUri(
121 'timeout' => $this->scopeConfig->getValue(
122 'currency/currencyconverterapi/timeout',
123 \
Magento\Store\Model\ScopeInterface::SCOPE_STORE
130 $response = json_decode($jsonResponse,
true);
131 }
catch (\Exception $e) {
142 protected function _convert($currencyFrom, $currencyTo)
const CURRENCY_CONVERTER_URL
__construct(\Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory)
_getDefaultCurrencyCodes()
_convert($currencyFrom, $currencyTo)