18
18
/**
19
19
* Currency rate import model (https://apilayer.com/marketplace/fixer-api)
20
20
*/
21
- class FixerIoApiLayer extends AbstractImport
21
+ class FixerIoApiLayer implements \ Magento \ Directory \ Model \ Currency \ Import \ImportInterface
22
22
{
23
23
private const CURRENCY_CONVERTER_HOST = 'https://api.apilayer.com ' ;
24
24
private const CURRENCY_CONVERTER_URL_PATH = '/fixer/latest? '
25
25
. 'apikey={{ACCESS_KEY}}&base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}} ' ;
26
26
27
+ /**
28
+ * Messages
29
+ *
30
+ * @var array
31
+ */
32
+ protected $ _messages = [];
33
+
27
34
/**
28
35
* @var HttpClientFactory
29
36
*/
30
37
private $ httpClientFactory ;
31
38
39
+ /**
40
+ * @var CurrencyFactory
41
+ */
42
+ protected $ _currencyFactory ;
43
+
32
44
/**
33
45
* Core scope config
34
46
*
@@ -48,11 +60,23 @@ public function __construct(
48
60
ScopeConfigInterface $ scopeConfig ,
49
61
HttpClientFactory $ httpClientFactory
50
62
) {
51
- parent :: __construct ( $ currencyFactory) ;
63
+ $ this -> _currencyFactory = $ currencyFactory ;
52
64
$ this ->scopeConfig = $ scopeConfig ;
53
65
$ this ->httpClientFactory = $ httpClientFactory ;
54
66
}
55
67
68
+ /**
69
+ * Import rates
70
+ *
71
+ * @return $this
72
+ */
73
+ public function importRates ()
74
+ {
75
+ $ data = $ this ->fetchRates ();
76
+ $ this ->_saveRates ($ data );
77
+ return $ this ;
78
+ }
79
+
56
80
/**
57
81
* @inheritdoc
58
82
*/
@@ -72,6 +96,14 @@ public function fetchRates()
72
96
return $ data ;
73
97
}
74
98
99
+ /**
100
+ * @return array
101
+ */
102
+ public function getMessages ()
103
+ {
104
+ return $ this ->_messages ;
105
+ }
106
+
75
107
/**
76
108
* Return currencies convert rates in batch mode
77
109
*
@@ -110,28 +142,32 @@ private function convertBatch(array $data, string $currencyFrom, array $currenci
110
142
111
143
foreach ($ currenciesTo as $ currencyTo ) {
112
144
if ($ currencyFrom == $ currencyTo ) {
113
- $ data [$ currencyFrom ][$ currencyTo ] = $ this -> _numberFormat ( 1 ) ;
145
+ $ data [$ currencyFrom ][$ currencyTo ] = 1 ;
114
146
} else {
115
147
if (empty ($ response ['rates ' ][$ currencyTo ])) {
116
148
$ message = 'We can \'t retrieve a rate from %1 for %2. ' ;
117
149
$ this ->_messages [] = __ ($ message , self ::CURRENCY_CONVERTER_HOST , $ currencyTo );
118
150
$ data [$ currencyFrom ][$ currencyTo ] = null ;
119
151
} else {
120
- $ data [$ currencyFrom ][$ currencyTo ] = $ this ->_numberFormat (
121
- (double )$ response ['rates ' ][$ currencyTo ]
122
- );
152
+ $ data [$ currencyFrom ][$ currencyTo ] = (double )$ response ['rates ' ][$ currencyTo ];
123
153
}
124
154
}
125
155
}
126
156
return $ data ;
127
157
}
128
158
129
159
/**
130
- * @inheritdoc
160
+ * Saving currency rates
161
+ *
162
+ * @param array $rates
163
+ * @return \Magento\Directory\Model\Currency\Import\FixerIoApiLayer
131
164
*/
132
- protected function _convert ( $ currencyFrom , $ currencyTo )
165
+ protected function _saveRates ( array $ rates )
133
166
{
134
- return 1 ;
167
+ foreach ($ rates as $ currencyCode => $ currencyRates ) {
168
+ $ this ->_currencyFactory ->create ()->setId ($ currencyCode )->setRates ($ currencyRates )->save ();
169
+ }
170
+ return $ this ;
135
171
}
136
172
137
173
/**
@@ -204,4 +240,24 @@ private function validateResponse(array $response, string $baseCurrency): bool
204
240
205
241
return false ;
206
242
}
243
+
244
+ /**
245
+ * Retrieve currency codes
246
+ *
247
+ * @return array
248
+ */
249
+ private function _getCurrencyCodes ()
250
+ {
251
+ return $ this ->_currencyFactory ->create ()->getConfigAllowCurrencies ();
252
+ }
253
+
254
+ /**
255
+ * Retrieve default currency codes
256
+ *
257
+ * @return array
258
+ */
259
+ private function _getDefaultCurrencyCodes ()
260
+ {
261
+ return $ this ->_currencyFactory ->create ()->getConfigBaseCurrencies ();
262
+ }
207
263
}
0 commit comments