Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractImport.php
Go to the documentation of this file.
1 <?php
11 
16 abstract class AbstractImport implements \Magento\Directory\Model\Currency\Import\ImportInterface
17 {
23  protected $_messages = [];
24 
28  protected $_currencyFactory;
29 
33  public function __construct(\Magento\Directory\Model\CurrencyFactory $currencyFactory)
34  {
35  $this->_currencyFactory = $currencyFactory;
36  }
37 
43  protected function _getCurrencyCodes()
44  {
45  return $this->_currencyFactory->create()->getConfigAllowCurrencies();
46  }
47 
53  protected function _getDefaultCurrencyCodes()
54  {
55  return $this->_currencyFactory->create()->getConfigBaseCurrencies();
56  }
57 
65  abstract protected function _convert($currencyFrom, $currencyTo);
66 
73  protected function _saveRates($rates)
74  {
75  foreach ($rates as $currencyCode => $currencyRates) {
76  $this->_currencyFactory->create()->setId($currencyCode)->setRates($currencyRates)->save();
77  }
78  return $this;
79  }
80 
86  public function importRates()
87  {
88  $data = $this->fetchRates();
89  $this->_saveRates($data);
90  return $this;
91  }
92 
96  public function fetchRates()
97  {
98  $data = [];
99  $currencies = $this->_getCurrencyCodes();
100  $defaultCurrencies = $this->_getDefaultCurrencyCodes();
101  set_time_limit(0);
102  foreach ($defaultCurrencies as $currencyFrom) {
103  if (!isset($data[$currencyFrom])) {
104  $data[$currencyFrom] = [];
105  }
106 
107  foreach ($currencies as $currencyTo) {
108  if ($currencyFrom == $currencyTo) {
109  $data[$currencyFrom][$currencyTo] = $this->_numberFormat(1);
110  } else {
111  $data[$currencyFrom][$currencyTo] = $this->_numberFormat(
112  $this->_convert($currencyFrom, $currencyTo)
113  );
114  }
115  }
116  ksort($data[$currencyFrom]);
117  }
118  ini_restore('max_execution_time');
119 
120  return $data;
121  }
122 
127  protected function _numberFormat($number)
128  {
129  return $number;
130  }
131 
135  public function getMessages()
136  {
137  return $this->_messages;
138  }
139 }
$number
Definition: details.phtml:22
$rates
Definition: tax.phtml:35
__construct(\Magento\Directory\Model\CurrencyFactory $currencyFactory)