1 | <?php |
||
10 | class CurrencyConverterComponent extends Component |
||
11 | { |
||
12 | private $fromCurrency; |
||
13 | |||
14 | private $toCurrency; |
||
15 | |||
16 | private $amount; |
||
17 | |||
18 | private $hourDifference; |
||
19 | |||
20 | private $saveIntoDb; |
||
21 | |||
22 | private $dataSource; |
||
23 | |||
24 | private $rate; |
||
25 | |||
26 | private $currencyTable; |
||
27 | |||
28 | /** |
||
29 | * Convertion function |
||
30 | * |
||
31 | * @param string $fromCurrency the starting currency that user wants to convert to. |
||
32 | * @param string $toCurrency the ending currency that user wants to convert to. |
||
33 | * @param float $amount the amount to convert. |
||
34 | * @param boolean $saveIntoDb if develop wants to store convertion rate for use it without resending data to yahoo service. |
||
35 | * @param int $hourDifference the hour difference to check if the last convertion is passed, if yes make a new call to yahoo finance api. |
||
36 | * @param string $dataSource which dataSOurce need to use |
||
37 | * @return float the total amount converted into the new currency |
||
38 | */ |
||
39 | 7 | public function convert( |
|
40 | $fromCurrency, |
||
41 | $toCurrency, |
||
42 | $amount, |
||
43 | $saveIntoDb = true, |
||
44 | $hourDifference = 1, |
||
45 | $dataSource = 'default' |
||
46 | ) { |
||
47 | 7 | $this->fromCurrency = $fromCurrency; |
|
48 | 7 | $this->toCurrency = $toCurrency; |
|
49 | 7 | $this->amount = $amount; |
|
50 | 7 | $this->saveIntoDb = (bool)$saveIntoDb; |
|
51 | 7 | $this->hourDifference = $hourDifference; |
|
52 | 7 | $this->dataSource = $dataSource; |
|
53 | 7 | $this->rate = 0; |
|
54 | |||
55 | 7 | if ($this->fromCurrency != $this->toCurrency) { |
|
56 | 6 | $this->fixFromToCurrency(); |
|
57 | |||
58 | 6 | if ($this->saveIntoDb === true) { |
|
59 | 4 | $this->currencyTable = TableRegistry::get('CurrencyConverter', [ |
|
|
|||
60 | 4 | 'className' => 'CurrencyConverter\Model\Table\CurrencyConvertersTable', |
|
61 | 'table' => 'currency_converter' |
||
62 | 4 | ]); |
|
63 | |||
64 | $this->ensureIfExistTable(); |
||
65 | $this->saveIntoDatabase(); |
||
66 | |||
67 | return $this->calculateValue(); |
||
68 | } |
||
69 | |||
70 | 2 | $this->rate = $this->getRates(); |
|
71 | |||
72 | 2 | return $this->calculateValue(); |
|
73 | } |
||
74 | |||
75 | 1 | return number_format((double)$this->amount, 2, '.', ''); |
|
76 | } |
||
77 | |||
78 | 6 | private function fixFromToCurrency() |
|
88 | |||
89 | private function saveIntoDatabase() |
||
90 | { |
||
91 | $query = $this->currencyTable->find('all') |
||
92 | ->where(['fromCurrency' => $this->fromCurrency, 'toCurrency' => $this->toCurrency ]); |
||
93 | |||
94 | $query->enableHydration(false); |
||
95 | $result = $query->toArray(); |
||
96 | |||
97 | foreach ($result as $row){ |
||
98 | $lastUpdated = $row['modified']; |
||
99 | |||
100 | $now = date('Y-m-d H:i:s'); |
||
101 | $dStart = new \DateTime($now); |
||
102 | $diff = $dStart->diff($lastUpdated); |
||
103 | |||
104 | if ($this->ensureNeedToUpdateDatabase($diff, $row)) { |
||
105 | $this->updateDatabase($row); |
||
106 | } else { |
||
107 | $this->rate = $row['rates']; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | if (count($result) <= 0) { |
||
112 | $this->insertIntoDatabase(); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | private function updateDatabase($row) |
||
131 | |||
132 | private function insertIntoDatabase() |
||
133 | { |
||
147 | |||
148 | private function ensureNeedToUpdateDatabase($diff, $row) |
||
158 | |||
159 | 2 | private function getRates() |
|
182 | |||
183 | private function ensureIfExistTable() |
||
205 | |||
206 | 2 | private function calculateValue() |
|
211 | } |
||
212 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.