@@ -94,25 +94,10 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
94
94
$ url = str_replace ('{{CURRENCY_FROM}} ' , $ currencyFrom , self ::CURRENCY_CONVERTER_URL );
95
95
$ url = str_replace ('{{CURRENCY_TO}} ' , $ to , $ url );
96
96
$ url = str_replace ('{{API_KEY}} ' , $ apiKey , $ url );
97
- $ response = $ this ->getServiceResponse ($ url );
98
97
if ($ currencyFrom == $ to ) {
99
98
$ data [$ currencyFrom ][$ to ] = $ this ->_numberFormat (1 );
100
99
} else {
101
- if (empty ($ response )) {
102
- $ this ->_messages [] = __ ('We can \'t retrieve a rate from %1 for %2. ' , $ url , $ to );
103
- $ data [$ currencyFrom ][$ to ] = null ;
104
- } else {
105
- if (isset ($ response ['error ' ]) && $ response ['error ' ]) {
106
- if (!in_array ($ response ['error ' ], $ this ->_messages )) {
107
- $ this ->_messages [] = $ response ['error ' ];
108
- }
109
- $ data [$ currencyFrom ][$ to ] = null ;
110
- } else {
111
- $ data [$ currencyFrom ][$ to ] = $ this ->_numberFormat (
112
- (double )$ response [$ currencyFrom . '_ ' . $ to ]
113
- );
114
- }
115
- }
100
+ $ data [$ currencyFrom ][$ to ] = $ this ->getCurrencyRate ($ currencyFrom , $ to , $ url );
116
101
}
117
102
} finally {
118
103
ini_restore ('max_execution_time ' );
@@ -122,6 +107,36 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
122
107
return $ data ;
123
108
}
124
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
+
125
140
/**
126
141
* Get Fixer.io service response
127
142
*
0 commit comments