Skip to content

Commit a011309

Browse files
committed
refactor code to pass static test
1 parent 3fbdb4d commit a011309

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

app/code/Magento/Directory/Model/Currency/Import/CurrencyConverterApi.php

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,10 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
9494
$url = str_replace('{{CURRENCY_FROM}}', $currencyFrom, self::CURRENCY_CONVERTER_URL);
9595
$url = str_replace('{{CURRENCY_TO}}', $to, $url);
9696
$url = str_replace('{{API_KEY}}', $apiKey, $url);
97-
$response = $this->getServiceResponse($url);
9897
if ($currencyFrom == $to) {
9998
$data[$currencyFrom][$to] = $this->_numberFormat(1);
10099
} 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);
116101
}
117102
} finally {
118103
ini_restore('max_execution_time');
@@ -122,6 +107,36 @@ private function convertBatch($data, $currencyFrom, $currenciesTo)
122107
return $data;
123108
}
124109

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+
125140
/**
126141
* Get Fixer.io service response
127142
*

0 commit comments

Comments
 (0)