7
7
8
8
namespace Magento \Directory \Model \Currency \Import ;
9
9
10
+ /**
11
+ * Currency rate import model (From http://free.currencyconverterapi.com/)
12
+ *
13
+ * Class \Magento\Directory\Model\Currency\Import\CurrencyConverterApi
14
+ */
10
15
class CurrencyConverterApi extends AbstractImport
11
16
{
12
17
/**
13
18
* @var string
14
19
*/
15
- const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra ' ; //@codingStandardsIgnoreLine
20
+ const CURRENCY_CONVERTER_URL = 'http://free.currencyconverterapi.com/api/v3/convert?q={{CURRENCY_FROM}}_{{CURRENCY_TO}}&compact=ultra&apiKey={{API_KEY}} ' ; //@codingStandardsIgnoreLine
16
21
17
22
/**
18
23
* Http Client Factory
@@ -46,7 +51,7 @@ public function __construct(
46
51
}
47
52
48
53
/**
49
- * { @inheritdoc}
54
+ * @inheritdoc
50
55
*/
51
56
public function fetchRates ()
52
57
{
@@ -74,23 +79,25 @@ public function fetchRates()
74
79
*/
75
80
private function convertBatch ($ data , $ currencyFrom , $ currenciesTo )
76
81
{
82
+ $ apiKey = $ this ->scopeConfig ->getValue (
83
+ 'currency/currencyconverterapi/api_key ' ,
84
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORE
85
+ );
86
+ if (!$ apiKey ) {
87
+ $ this ->_messages [] = __ ('No API Key was specified. ' );
88
+ return $ data ;
89
+ }
77
90
foreach ($ currenciesTo as $ to ) {
91
+ //phpcs:ignore Magento2.Functions.DiscouragedFunction
78
92
set_time_limit (0 );
79
93
try {
80
94
$ url = str_replace ('{{CURRENCY_FROM}} ' , $ currencyFrom , self ::CURRENCY_CONVERTER_URL );
81
95
$ url = str_replace ('{{CURRENCY_TO}} ' , $ to , $ url );
82
- $ response = $ this -> getServiceResponse ( $ url );
96
+ $ url = str_replace ( ' {{API_KEY}} ' , $ apiKey , $ url );
83
97
if ($ currencyFrom == $ to ) {
84
98
$ data [$ currencyFrom ][$ to ] = $ this ->_numberFormat (1 );
85
99
} 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
- }
100
+ $ data [$ currencyFrom ][$ to ] = $ this ->getCurrencyRate ($ currencyFrom , $ to , $ url );
94
101
}
95
102
} finally {
96
103
ini_restore ('max_execution_time ' );
@@ -100,6 +107,36 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
100
107
return $ data ;
101
108
}
102
109
110
+ /**
111
+ * Get currency rate from api
112
+ *
113
+ * @param string $currencyFrom
114
+ * @param string $to
115
+ * @param string $url
116
+ * @return double
117
+ */
118
+ private function getCurrencyRate ($ currencyFrom , $ to , $ url )
119
+ {
120
+ $ rate = null ;
121
+ $ response = $ this ->getServiceResponse ($ url );
122
+ if (empty ($ response )) {
123
+ $ this ->_messages [] = __ ('We can \'t retrieve a rate from %1 for %2. ' , $ url , $ to );
124
+ $ rate = null ;
125
+ } else {
126
+ if (isset ($ response ['error ' ]) && $ response ['error ' ]) {
127
+ if (!in_array ($ response ['error ' ], $ this ->_messages )) {
128
+ $ this ->_messages [] = $ response ['error ' ];
129
+ }
130
+ $ rate = null ;
131
+ } else {
132
+ $ rate = $ this ->_numberFormat (
133
+ (double )$ response [$ currencyFrom . '_ ' . $ to ]
134
+ );
135
+ }
136
+ }
137
+ return $ rate ;
138
+ }
139
+
103
140
/**
104
141
* Get Fixer.io service response
105
142
*
@@ -137,7 +174,7 @@ private function getServiceResponse($url, $retry = 0)
137
174
}
138
175
139
176
/**
140
- * { @inheritdoc}
177
+ * @inheritdoc
141
178
*/
142
179
protected function _convert ($ currencyFrom , $ currencyTo )
143
180
{
0 commit comments