|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Directory\Model\Currency\Import; |
| 7 | + |
| 8 | +/** |
| 9 | + * Currency rate import model (From http://query.yahooapis.com/) |
| 10 | + */ |
| 11 | +class YahooFinance extends \Magento\Directory\Model\Currency\Import\AbstractImport |
| 12 | +{ |
| 13 | + /** |
| 14 | + * Currency converter url string |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + private $currencyConverterUrl = 'http://query.yahooapis.com/v1/public/yql?format=json&q={{YQL_STRING}}' |
| 19 | + .'&env=store://datatables.org/alltableswithkeys'; |
| 20 | + |
| 21 | + /** |
| 22 | + * Config path for service timeout |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private $timeoutConfigPath = 'currency/yahoofinance/timeout'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Http Client Factory |
| 30 | + * |
| 31 | + * @var \Magento\Framework\HTTP\ZendClientFactory |
| 32 | + */ |
| 33 | + protected $httpClientFactory; |
| 34 | + |
| 35 | + /** |
| 36 | + * Core scope config |
| 37 | + * |
| 38 | + * @var \Magento\Framework\App\Config\ScopeConfigInterface |
| 39 | + */ |
| 40 | + private $scopeConfig; |
| 41 | + |
| 42 | + /** |
| 43 | + * Initialize dependencies |
| 44 | + * |
| 45 | + * @param \Magento\Directory\Model\CurrencyFactory $currencyFactory |
| 46 | + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig |
| 47 | + * @param \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory |
| 48 | + */ |
| 49 | + public function __construct( |
| 50 | + \Magento\Directory\Model\CurrencyFactory $currencyFactory, |
| 51 | + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
| 52 | + \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory |
| 53 | + ) { |
| 54 | + parent::__construct($currencyFactory); |
| 55 | + $this->scopeConfig = $scopeConfig; |
| 56 | + $this->httpClientFactory = $httpClientFactory; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function fetchRates() |
| 63 | + { |
| 64 | + $data = []; |
| 65 | + $currencies = $this->_getCurrencyCodes(); |
| 66 | + $defaultCurrencies = $this->_getDefaultCurrencyCodes(); |
| 67 | + |
| 68 | + foreach ($defaultCurrencies as $currencyFrom) { |
| 69 | + if (!isset($data[$currencyFrom])) { |
| 70 | + $data[$currencyFrom] = []; |
| 71 | + } |
| 72 | + $data = $this->convertBatch($data, $currencyFrom, $currencies); |
| 73 | + ksort($data[$currencyFrom]); |
| 74 | + } |
| 75 | + return $data; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Return currencies convert rates in batch mode |
| 80 | + * |
| 81 | + * @param array $data |
| 82 | + * @param string $currencyFrom |
| 83 | + * @param array $currenciesTo |
| 84 | + * @return array |
| 85 | + */ |
| 86 | + private function convertBatch($data, $currencyFrom, $currenciesTo) |
| 87 | + { |
| 88 | + $url = $this->buildUrl($currencyFrom, $currenciesTo); |
| 89 | + set_time_limit(0); |
| 90 | + try { |
| 91 | + $response = $this->getServiceResponse($url); |
| 92 | + } finally { |
| 93 | + ini_restore('max_execution_time'); |
| 94 | + } |
| 95 | + |
| 96 | + foreach ($currenciesTo as $currencyTo) { |
| 97 | + if ($currencyFrom == $currencyTo) { |
| 98 | + $data[$currencyFrom][$currencyTo] = $this->_numberFormat(1); |
| 99 | + } else { |
| 100 | + if (empty($response[$currencyFrom . $currencyTo])) { |
| 101 | + $this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $currencyTo); |
| 102 | + $data[$currencyFrom][$currencyTo] = null; |
| 103 | + } else { |
| 104 | + $data[$currencyFrom][$currencyTo] = $this->_numberFormat( |
| 105 | + (double)$response[$currencyFrom . $currencyTo] |
| 106 | + ); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return $data; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get Fixer.io service response |
| 115 | + * |
| 116 | + * @param string $url |
| 117 | + * @param int $retry |
| 118 | + * @return array |
| 119 | + */ |
| 120 | + private function getServiceResponse($url, $retry = 0) |
| 121 | + { |
| 122 | + /** @var \Magento\Framework\HTTP\ZendClient $httpClient */ |
| 123 | + $httpClient = $this->httpClientFactory->create(); |
| 124 | + $response = []; |
| 125 | + try { |
| 126 | + $jsonResponse = $httpClient->setUri( |
| 127 | + $url |
| 128 | + )->setConfig( |
| 129 | + [ |
| 130 | + 'timeout' => $this->scopeConfig->getValue( |
| 131 | + $this->timeoutConfigPath, |
| 132 | + \Magento\Store\Model\ScopeInterface::SCOPE_STORE |
| 133 | + ), |
| 134 | + ] |
| 135 | + )->request( |
| 136 | + 'GET' |
| 137 | + )->getBody(); |
| 138 | + |
| 139 | + $jsonResponse = json_decode($jsonResponse, true); |
| 140 | + if (!empty($jsonResponse['query']['results']['rate'])) { |
| 141 | + $response = array_column($jsonResponse['query']['results']['rate'], 'Rate', 'id'); |
| 142 | + } |
| 143 | + } catch (\Exception $e) { |
| 144 | + if ($retry == 0) { |
| 145 | + $response = $this->getServiceResponse($url, 1); |
| 146 | + } |
| 147 | + } |
| 148 | + return $response; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * {@inheritdoc} |
| 153 | + */ |
| 154 | + protected function _convert($currencyFrom, $currencyTo) |
| 155 | + { |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Build url for Currency Service |
| 160 | + * |
| 161 | + * @param string $currencyFrom |
| 162 | + * @param string[] $currenciesTo |
| 163 | + * @return string |
| 164 | + */ |
| 165 | + private function buildUrl($currencyFrom, $currenciesTo) |
| 166 | + { |
| 167 | + $query = urlencode('select ') . '*' . urlencode(' from yahoo.finance.xchange where pair in ('); |
| 168 | + $query .= |
| 169 | + urlencode( |
| 170 | + implode( |
| 171 | + ',', |
| 172 | + array_map( |
| 173 | + function ($currencyTo) use ($currencyFrom) { |
| 174 | + return '"' . $currencyFrom . $currencyTo . '"'; |
| 175 | + }, |
| 176 | + $currenciesTo |
| 177 | + ) |
| 178 | + ) |
| 179 | + ); |
| 180 | + $query .= ')'; |
| 181 | + return str_replace('{{YQL_STRING}}', $query, $this->currencyConverterUrl); |
| 182 | + } |
| 183 | +} |
0 commit comments