AeliaWCEU_VAT_AssistantExchange_Rates_DNB_Model

Retrieves the exchange rates from the Danish National Bank.

Defined (1)

The class is defined in the following location(s).

/src/lib/classes/exchange_rates/aelia-wc-exchangerates-dnb.php  
  1. class Exchange_Rates_DNB_Model extends \Aelia\WC\ExchangeRatesModel { 
  2. // @var string The URL template to use to query DNB 
  3. private $dnb_api_rates_url = 'http://www.nationalbanken.dk/_vti_bin/DN/DataService.svc/CurrencyRatesXML?lang=en'; 
  4.  
  5. /** 
  6. * Tranforms the exchange rates received from DNB into an array of 
  7. * currency code => exchange rate pairs. 
  8. * @param string dnb_rates The XML received from DNB. 
  9. * @retur array 
  10. */ 
  11. protected function decode_rates($dnb_rates) { 
  12. $exchange_rates = array(); 
  13.  
  14. foreach($dnb_rates->dailyrates->currency as $rate) { 
  15. /** DNB provides exchange rates work in its own peculiar way. The "rate" 
  16. * does not indicate how many units of the target currency correspond to 
  17. * one unit of the base currency, but how many units of the base currency 
  18. * are needed to get 100 units of the target currency. Because of that,  
  19. * we have to make a simple calculation to get the actual exchange rate. 
  20. */ 
  21. $exchange_rates[(string)$rate['code']] = 100 / (float)$rate['rate']; 
  22. // DNB feed is based against DKK, but it doesn't contain such currency. We 
  23. // can safely add it manually, with an exchange rate of 1 
  24. $exchange_rates['DKK'] = 1; 
  25. return $exchange_rates; 
  26.  
  27. /** 
  28. * Fetches all exchange rates from DNB API. 
  29. * @return object|bool An object containing the response from Open Exchange, or 
  30. * False in case of failure. 
  31. */ 
  32. private function fetch_all_rates() { 
  33. try { 
  34. $response = \Httpful\Request::get($this->dnb_api_rates_url) 
  35. ->expectsXml() 
  36. ->send(); 
  37.  
  38. // Debug 
  39. //var_dump("DNB RATES RESPONSE:", $response); die(); 
  40. if($response->hasErrors()) { 
  41. // OpenExchangeRates sends error details in response body 
  42. if($response->hasBody()) { 
  43. $response_data = $response->body; 
  44.  
  45. $this->add_error(self::ERR_ERROR_RETURNED,  
  46. sprintf(__('Error returned by DNB. ' . 
  47. 'Error code: %s. Error message: %s - %s.',  
  48. Definitions::TEXT_DOMAIN),  
  49. $response_data->status,  
  50. $response_data->message,  
  51. $response_data->description)); 
  52. return false; 
  53. return $response->body; 
  54. catch(Exception $e) { 
  55. $this->add_error(self::ERR_EXCEPTION_OCCURRED,  
  56. sprintf(__('Exception occurred while retrieving the exchange rates from DNB. ' . 
  57. 'Error message: %s.',  
  58. Definitions::TEXT_DOMAIN),  
  59. $e->getMessage())); 
  60. return null; 
  61.  
  62. /** 
  63. * Returns current exchange rates for the specified currency. 
  64. * @param string base_currency The base currency. 
  65. * @return array An array of Currency => Exchange Rate pairs. 
  66. */ 
  67. private function current_rates($base_currency) { 
  68. if(empty($this->_current_rates) || 
  69. $this->_base_currency != $base_currency) { 
  70.  
  71. $cache_key = md5(get_class($this)) . $base_currency; 
  72. // Try to get the cached rates for the specified base currency, if any 
  73. $this->_current_rates = $this->get_cached_exchange_rates($cache_key); 
  74. if(!empty($this->_current_rates)) { 
  75. return $this->_current_rates; 
  76.  
  77. // Fetch exchange rates 
  78. $dnb_exchange_rates = $this->fetch_all_rates(); 
  79. if($dnb_exchange_rates === false) { 
  80. return null; 
  81.  
  82. // Debug 
  83. //var_dump($dnb_exchange_rates);die(); 
  84.  
  85. // DNB rates are returned as JSON representation of an array of objects. 
  86. // We need to transform it into an array of currency => rate pairs 
  87. $exchange_rates = $this->decode_rates($dnb_exchange_rates); 
  88.  
  89. // Debug 
  90. //var_dump($exchange_rates);die(); 
  91.  
  92. if(!is_array($exchange_rates)) { 
  93. $this->add_error(self::ERR_UNEXPECTED_ERROR_FETCHING_EXCHANGE_RATES,  
  94. __('An unexpected error occurred while fetching exchange rates ' . 
  95. 'from DNB. The most common cause of this issue is the ' . 
  96. 'absence of PHP CURL extension. Please make sure that ' . 
  97. 'PHP CURL is installed and configured in your system.',  
  98. Definitions::TEXT_DOMAIN)); 
  99. return array(); 
  100.  
  101. // Since we didn't get the exchange rates related to the base currency,  
  102. // but in the default base currency used by OpenExchange, we need to 
  103. // recalculate them against the base currency we would like to use 
  104. $this->_current_rates = $this->rebase_rates($exchange_rates, $base_currency); 
  105. $this->_base_currency = $base_currency; 
  106.  
  107. // Cache the exchange rates 
  108. $this->cache_exchange_rates($cache_key, $this->_current_rates); 
  109. return $this->_current_rates; 
  110.  
  111. /** 
  112. * Recaculates the exchange rates using another base currency. This method 
  113. * is invoked because the rates fetched from DNB are relative to BitCoin,  
  114. * but another currency is most likely is used by WooCommerce. 
  115. * @param array exchange_rates The exchange rates retrieved from DNB. 
  116. * @param string base_currency The base currency against which the rates should 
  117. * be recalculated. 
  118. * @return array An array of currency => exchange rate pairs. 
  119. */ 
  120. private function rebase_rates(array $exchange_rates, $base_currency) { 
  121. $recalc_rate = get_value($base_currency, $exchange_rates); 
  122. //var_dump($base_currency, $exchange_rates); 
  123.  
  124. if(empty($recalc_rate)) { 
  125. $this->add_error(self::ERR_BASE_CURRENCY_NOT_FOUND,  
  126. sprintf(__('Could not rebase rates against base currency "%s". ' . 
  127. 'Currency not found in data returned by DNB.',  
  128. Definitions::TEXT_DOMAIN),  
  129. $base_currency)); 
  130. return null; 
  131.  
  132. $result = array(); 
  133. foreach($exchange_rates as $currency => $rate) { 
  134. $result[$currency] = $rate / $recalc_rate; 
  135.  
  136. // Debug 
  137. //var_dump($result); die(); 
  138. return $result; 
  139.  
  140. /** 
  141. * Returns the exchange rate of a currency in respect to a base currency. 
  142. * @param string base_currency The code of the base currency. 
  143. * @param string currency The code of the currency for which to find the 
  144. * Exchange Rate. 
  145. * @return float 
  146. */ 
  147. protected function get_rate($base_currency, $currency) { 
  148. $current_rates = $this->current_rates($base_currency); 
  149. return get_value($currency, $current_rates); 
  150.  
  151. /** 
  152. * Class constructor. 
  153. * @param array An array of Settings that can be used to override the ones 
  154. * currently saved in the configuration. 
  155. * @return Exchange_Rates_DNB_Model. 
  156. */ 
  157. public function __construct($settings = null) { 
  158. parent::__construct($settings);