1818/**
1919 * Currency rate import model (https://apilayer.com/marketplace/fixer-api)
2020 */
21- class FixerIoApiLayer extends AbstractImport
21+ class FixerIoApiLayer implements \ Magento \ Directory \ Model \ Currency \ Import \ImportInterface
2222{
2323 private const CURRENCY_CONVERTER_HOST = 'https://api.apilayer.com ' ;
2424 private const CURRENCY_CONVERTER_URL_PATH = '/fixer/latest? '
2525 . 'apikey={{ACCESS_KEY}}&base={{CURRENCY_FROM}}&symbols={{CURRENCY_TO}} ' ;
2626
27+ /**
28+ * Messages
29+ *
30+ * @var array
31+ */
32+ protected $ _messages = [];
33+
2734 /**
2835 * @var HttpClientFactory
2936 */
3037 private $ httpClientFactory ;
3138
39+ /**
40+ * @var CurrencyFactory
41+ */
42+ protected $ _currencyFactory ;
43+
3244 /**
3345 * Core scope config
3446 *
@@ -48,11 +60,23 @@ public function __construct(
4860 ScopeConfigInterface $ scopeConfig ,
4961 HttpClientFactory $ httpClientFactory
5062 ) {
51- parent :: __construct ( $ currencyFactory) ;
63+ $ this -> _currencyFactory = $ currencyFactory ;
5264 $ this ->scopeConfig = $ scopeConfig ;
5365 $ this ->httpClientFactory = $ httpClientFactory ;
5466 }
5567
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+
5680 /**
5781 * @inheritdoc
5882 */
@@ -72,6 +96,14 @@ public function fetchRates()
7296 return $ data ;
7397 }
7498
99+ /**
100+ * @return array
101+ */
102+ public function getMessages ()
103+ {
104+ return $ this ->_messages ;
105+ }
106+
75107 /**
76108 * Return currencies convert rates in batch mode
77109 *
@@ -110,28 +142,32 @@ private function convertBatch(array $data, string $currencyFrom, array $currenci
110142
111143 foreach ($ currenciesTo as $ currencyTo ) {
112144 if ($ currencyFrom == $ currencyTo ) {
113- $ data [$ currencyFrom ][$ currencyTo ] = $ this -> _numberFormat ( 1 ) ;
145+ $ data [$ currencyFrom ][$ currencyTo ] = 1 ;
114146 } else {
115147 if (empty ($ response ['rates ' ][$ currencyTo ])) {
116148 $ message = 'We can \'t retrieve a rate from %1 for %2. ' ;
117149 $ this ->_messages [] = __ ($ message , self ::CURRENCY_CONVERTER_HOST , $ currencyTo );
118150 $ data [$ currencyFrom ][$ currencyTo ] = null ;
119151 } else {
120- $ data [$ currencyFrom ][$ currencyTo ] = $ this ->_numberFormat (
121- (double )$ response ['rates ' ][$ currencyTo ]
122- );
152+ $ data [$ currencyFrom ][$ currencyTo ] = (double )$ response ['rates ' ][$ currencyTo ];
123153 }
124154 }
125155 }
126156 return $ data ;
127157 }
128158
129159 /**
130- * @inheritdoc
160+ * Saving currency rates
161+ *
162+ * @param array $rates
163+ * @return \Magento\Directory\Model\Currency\Import\FixerIoApiLayer
131164 */
132- protected function _convert ( $ currencyFrom , $ currencyTo )
165+ protected function _saveRates ( array $ rates )
133166 {
134- return 1 ;
167+ foreach ($ rates as $ currencyCode => $ currencyRates ) {
168+ $ this ->_currencyFactory ->create ()->setId ($ currencyCode )->setRates ($ currencyRates )->save ();
169+ }
170+ return $ this ;
135171 }
136172
137173 /**
@@ -204,4 +240,24 @@ private function validateResponse(array $response, string $baseCurrency): bool
204240
205241 return false ;
206242 }
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+ }
207263}
0 commit comments