Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurrencyConverterApi.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 {
15  const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra'; //@codingStandardsIgnoreLine
16 
22  private $httpClientFactory;
23 
29  private $scopeConfig;
30 
38  public function __construct(
39  \Magento\Directory\Model\CurrencyFactory $currencyFactory,
40  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
41  \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory
42  ) {
43  parent::__construct($currencyFactory);
44  $this->scopeConfig = $scopeConfig;
45  $this->httpClientFactory = $httpClientFactory;
46  }
47 
51  public function fetchRates()
52  {
53  $data = [];
54  $currencies = $this->_getCurrencyCodes();
55  $defaultCurrencies = $this->_getDefaultCurrencyCodes();
56 
57  foreach ($defaultCurrencies as $currencyFrom) {
58  if (!isset($data[$currencyFrom])) {
59  $data[$currencyFrom] = [];
60  }
61  $data = $this->convertBatch($data, $currencyFrom, $currencies);
62  ksort($data[$currencyFrom]);
63  }
64  return $data;
65  }
66 
75  private function convertBatch($data, $currencyFrom, $currenciesTo)
76  {
77  foreach ($currenciesTo as $to) {
78  set_time_limit(0);
79  try {
80  $url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
81  $url = str_replace('{{CURRENCY_TO}}', $to, $url);
82  $response = $this->getServiceResponse($url);
83  if ($currencyFrom == $to) {
84  $data[$currencyFrom][$to] = $this->_numberFormat(1);
85  } else {
86  if (empty($response)) {
87  $this->_messages[] = __('We can\'t retrieve a rate from %1 for %2.', $url, $to);
88  $data[$currencyFrom][$to] = null;
89  } else {
90  $data[$currencyFrom][$to] = $this->_numberFormat(
91  (double)$response[$currencyFrom . '_' . $to]
92  );
93  }
94  }
95  } finally {
96  ini_restore('max_execution_time');
97  }
98  }
99 
100  return $data;
101  }
102 
110  private function getServiceResponse($url, $retry = 0)
111  {
113  $httpClient = $this->httpClientFactory->create();
114  $response = [];
115 
116  try {
117  $jsonResponse = $httpClient->setUri(
118  $url
119  )->setConfig(
120  [
121  'timeout' => $this->scopeConfig->getValue(
122  'currency/currencyconverterapi/timeout',
123  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
124  ),
125  ]
126  )->request(
127  'GET'
128  )->getBody();
129 
130  $response = json_decode($jsonResponse, true);
131  } catch (\Exception $e) {
132  if ($retry == 0) {
133  $response = $this->getServiceResponse($url, 1);
134  }
135  }
136  return $response;
137  }
138 
142  protected function _convert($currencyFrom, $currencyTo)
143  {
144  return 1;
145  }
146 }
$response
Definition: 404.php:11
__()
Definition: __.php:13
__construct(\Magento\Directory\Model\CurrencyFactory $currencyFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\HTTP\ZendClientFactory $httpClientFactory)