|
| 1 | +<?php |
| 2 | +namespace Magento\Directory\Model\Currency\Import; |
| 3 | + |
| 4 | +class CurrencyConverterApi extends AbstractImport |
| 5 | +{ |
| 6 | + /** |
| 7 | + * @var string |
| 8 | + */ |
| 9 | + const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; |
| 10 | + |
| 11 | + /** |
| 12 | + * Http Client Factory |
| 13 | + * |
| 14 | + * @var \Magento\Framework\HTTP\ZendClientFactory |
| 15 | + */ |
| 16 | + protected $httpClientFactory; |
| 17 | + |
| 18 | + /** |
| 19 | + * Core scope config |
| 20 | + * |
| 21 | + * @var \Magento\Framework\App\Config\ScopeConfigInterface |
| 22 | + */ |
| 23 | + private $scopeConfig; |
| 24 | + |
| 25 | + /** |
| 26 | + * Initialize dependencies |
| 27 | + * |
| 28 | + * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory |
| 29 | + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig |
| 30 | + * @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + \Magento\Directory\Model\CurrencyFactory $currencyFactory, |
| 34 | + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
| 35 | + \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory |
| 36 | + ) { |
| 37 | + parent::__construct($currencyFactory); |
| 38 | + $this->scopeConfig = $scopeConfig; |
| 39 | + $this->httpClientFactory = $httpClientFactory; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + */ |
| 45 | + public function fetchRates() |
| 46 | + { |
| 47 | + $data = []; |
| 48 | + $currencies = $this->_getCurrencyCodes(); |
| 49 | + $defaultCurrencies = $this->_getDefaultCurrencyCodes(); |
| 50 | + |
| 51 | + foreach ($defaultCurrencies as $currencyFrom) { |
| 52 | + if (!isset($data[$currencyFrom])) { |
| 53 | + $data[$currencyFrom] = []; |
| 54 | + } |
| 55 | + $data = $this->convertBatch($data, $currencyFrom, $currencies); |
| 56 | + ksort($data[$currencyFrom]); |
| 57 | + } |
| 58 | + return $data; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Return currencies convert rates in batch mode |
| 63 | + * |
| 64 | + * @param array $data |
| 65 | + * @param string $currencyFrom |
| 66 | + * @param array $currenciesTo |
| 67 | + * @return array |
| 68 | + */ |
| 69 | + private function convertBatch($data, $currencyFrom, $currenciesTo) |
| 70 | + { |
| 71 | + foreach($currenciesTo as $to) { |
| 72 | + set_time_limit(0); |
| 73 | + try { |
| 74 | + $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL); |
| 75 | + $url = str_replace('{{CURRENCY_TO}}', $to, $url); |
| 76 | + $response = $this->getServiceResponse($url); |
| 77 | + if ($currencyFrom == $to) { |
| 78 | + $data[$currencyFrom][$to] = $this->_numberFormat(1); |
| 79 | + } else { |
| 80 | + if (empty($response)) { |
| 81 | + $this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to); |
| 82 | + $data[$currencyFrom][$to] = null; |
| 83 | + } else { |
| 84 | + $data[$currencyFrom][$to] = $this->_numberFormat( |
| 85 | + (double)$response[$currencyFrom . '_' . $to] |
| 86 | + ); |
| 87 | + } |
| 88 | + } |
| 89 | + } finally { |
| 90 | + ini_restore('max_execution_time'); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return $data; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get Fixer.io service response |
| 99 | + * |
| 100 | + * @param string $url |
| 101 | + * @param int $retry |
| 102 | + * @return array |
| 103 | + */ |
| 104 | + private function getServiceResponse($url, $retry = 0) |
| 105 | + { |
| 106 | + /** @var \Magento\Framework\HTTP\ZendClient $httpClient */ |
| 107 | + $httpClient = $this->httpClientFactory->create(); |
| 108 | + $response = []; |
| 109 | + |
| 110 | + try { |
| 111 | + $jsonResponse = $httpClient->setUri( |
| 112 | + $url |
| 113 | + )->setConfig( |
| 114 | + [ |
| 115 | + 'timeout' => $this->scopeConfig->getValue( |
| 116 | + 'currency/currencyconverterapi/timeout', |
| 117 | + \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 118 | + ), |
| 119 | + ] |
| 120 | + )->request( |
| 121 | + 'GET' |
| 122 | + )->getBody(); |
| 123 | + |
| 124 | + $response = json_decode($jsonResponse, true); |
| 125 | + } catch (\Exception $e) { |
| 126 | + if ($retry == 0) { |
| 127 | + $response = $this->getServiceResponse($url, 1); |
| 128 | + } |
| 129 | + } |
| 130 | + return $response; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * {@inheritdoc} |
| 135 | + */ |
| 136 | + protected function _convert($currencyFrom, $currencyTo) |
| 137 | + { |
| 138 | + } |
| 139 | +} |
0 commit comments