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+
95+
96+ return $ data ;
97+ }
98+
99+ /**
100+ * Get Fixer.io service response
101+ *
102+ * @param string $url
103+ * @param int $retry
104+ * @return array
105+ */
106+ private function getServiceResponse ($ url , $ retry = 0 )
107+ {
108+ /** @var \Magento\Framework\HTTP\ZendClient $httpClient */
109+ $ httpClient = $ this ->httpClientFactory ->create ();
110+ $ response = [];
111+
112+ try {
113+ $ jsonResponse = $ httpClient ->setUri (
114+ $ url
115+ )->setConfig (
116+ [
117+ 'timeout ' => $ this ->scopeConfig ->getValue (
118+ 'currency/currencyconverterapi/timeout ' ,
119+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
120+ ),
121+ ]
122+ )->request (
123+ 'GET '
124+ )->getBody ();
125+
126+ $ response = json_decode ($ jsonResponse , true );
127+ } catch (\Exception $ e ) {
128+ if ($ retry == 0 ) {
129+ $ response = $ this ->getServiceResponse ($ url , 1 );
130+ }
131+ }
132+ return $ response ;
133+ }
134+
135+ /**
136+ * {@inheritdoc}
137+ */
138+ protected function _convert ($ currencyFrom , $ currencyTo )
139+ {
140+ }
141+ }
0 commit comments