|
1
|
|
|
<?php |
|
2
|
|
|
namespace CurrencyConverter\Controller\Component; |
|
3
|
|
|
|
|
4
|
|
|
use Cake\Controller\Component; |
|
5
|
|
|
use Cake\Datasource\ConnectionManager; |
|
6
|
|
|
use Cake\ORM\TableRegistry; |
|
7
|
|
|
use Cake\I18n\Time; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* CurrencyConverter Component to convert currency. |
|
11
|
|
|
* |
|
12
|
|
|
* Convert an amount number from one currency to an other currency |
|
13
|
|
|
* Return currency rate from one currency to an other |
|
14
|
|
|
* output type from HTML to JSON format. |
|
15
|
|
|
*/ |
|
16
|
|
|
class CurrencyConverterComponent extends Component |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Using database |
|
20
|
|
|
* |
|
21
|
|
|
* @var bool |
|
22
|
|
|
*/ |
|
23
|
|
|
private $database; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Time interval for refreshing database |
|
27
|
|
|
* |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $refresh; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Number of decimal to use for formatting converted price |
|
34
|
|
|
* |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
private $decimal; |
|
38
|
|
|
|
|
39
|
8 |
|
/** |
|
40
|
|
|
* Number to divise 1 and get the sup step to round price to |
|
41
|
|
|
* |
|
42
|
|
|
* @var float |
|
43
|
|
|
*/ |
|
44
|
|
|
private $round; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
8 |
|
* Session |
|
48
|
8 |
|
* |
|
49
|
8 |
|
* @var \Cake\Http\Session |
|
50
|
8 |
|
*/ |
|
51
|
8 |
|
private $session; |
|
52
|
8 |
|
|
|
53
|
8 |
|
/** |
|
54
|
|
|
* CurrencyratesTable Class |
|
55
|
8 |
|
* @var \Cake\ORM\Table |
|
56
|
7 |
|
*/ |
|
57
|
|
|
private $currencyratesTable; |
|
58
|
7 |
|
|
|
59
|
5 |
|
/** |
|
60
|
5 |
|
* Default CurrencyConverterComponent settings. |
|
61
|
|
|
* |
|
62
|
5 |
|
* When calling CurrencyConverterComponent() these settings will be merged with the configuration |
|
63
|
|
|
* you provide. |
|
64
|
5 |
|
* |
|
65
|
5 |
|
* - `database` - Mention if Component have to store currency rate in database |
|
66
|
|
|
* - `refresh` - Time interval for Component to refresh currency rate in database |
|
67
|
5 |
|
* - `decimal` - Number of decimal to use when formatting amount float number |
|
68
|
|
|
* - `round` - Number to divise 1 and get the sup step to round price to (eg: 4 for 0.25 step) |
|
69
|
|
|
* |
|
70
|
2 |
|
* @var array |
|
71
|
|
|
*/ |
|
72
|
2 |
|
protected $_defaultConfig = [ |
|
73
|
|
|
'database' => true, // Mention if Component have to store currency rate in database |
|
74
|
|
|
'refresh' => 24, // Time interval for Component to refresh currency rate in database |
|
75
|
1 |
|
'decimal' => 2, // Number of decimal to use when formatting amount float number |
|
76
|
|
|
'round' => false, // Number to divise 1 and get the sup step to round price to (eg: 4 for 0.25 step) |
|
77
|
|
|
]; |
|
78
|
7 |
|
|
|
79
|
|
|
/** |
|
80
|
7 |
|
* @param array $config |
|
81
|
1 |
|
* @return void |
|
82
|
1 |
|
*/ |
|
83
|
|
|
public function initialize(array $config = []) { |
|
84
|
7 |
|
$config = $this->getConfig(); |
|
85
|
1 |
|
|
|
86
|
1 |
|
$this->database = $config['database']; |
|
87
|
7 |
|
$this->refresh = $config['refresh']; |
|
88
|
|
|
$this->decimal = $config['decimal']; |
|
89
|
5 |
|
$this->round = $config['round']; |
|
90
|
|
|
if ($this->round == 0) { |
|
91
|
5 |
|
$this->round = false; |
|
|
|
|
|
|
92
|
5 |
|
} |
|
93
|
|
|
$this->session = $this->request->getSession(); |
|
|
|
|
|
|
94
|
5 |
|
$this->currencyratesTable = TableRegistry::get('CurrencyConverter.Currencyrates'); |
|
|
|
|
|
|
95
|
5 |
|
} |
|
96
|
|
|
|
|
97
|
5 |
|
/** |
|
98
|
2 |
|
* Convert method take an amount as first parameter and convert it using $from currency and $to currency. |
|
99
|
|
|
* |
|
100
|
2 |
|
* @param float|string $amount the amount to convert. |
|
101
|
2 |
|
* @param string $from currency to convert from |
|
102
|
2 |
|
* @param string $to currency to convert to |
|
103
|
|
|
* @return string $amount converted |
|
104
|
2 |
|
*/ |
|
105
|
|
|
public function convert($amount, $from, $to) |
|
106
|
|
|
{ |
|
107
|
2 |
|
$amount = floatval($amount); |
|
108
|
|
|
$rate = $this->_getRateToUse($from, $to); |
|
109
|
5 |
|
|
|
110
|
|
|
return $convert = $this->_formatConvert($rate * $amount); |
|
|
|
|
|
|
111
|
5 |
|
} |
|
112
|
3 |
|
|
|
113
|
3 |
|
/** |
|
114
|
5 |
|
* Rate method return the rate of two currencies |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $from currency to get the rate from |
|
117
|
|
|
* @param string $to currency to get the rate to |
|
118
|
|
|
* @return float|null $rate |
|
119
|
|
|
*/ |
|
120
|
|
|
public function rate($from, $to) |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->_getRateToUse($from, $to); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* getRateToUse return rate to use |
|
127
|
|
|
* Using $from and $to parameters representing currency to deal with and the configuration settings |
|
128
|
|
|
* This method save or update currencyrates Table if necesseray too. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $from currency to get the rate from |
|
131
|
|
|
* @param string $to currency to get the rate to |
|
132
|
3 |
|
* @return float|null $rate |
|
133
|
|
|
*/ |
|
134
|
3 |
|
private function _getRateToUse($from, $to) |
|
135
|
|
|
{ |
|
136
|
|
|
$rate = 1; |
|
137
|
3 |
|
if ($from !== $to) { |
|
138
|
3 |
|
if ($this->database) { |
|
139
|
3 |
|
$rate = $this->_getRateFromSession($from, $to); |
|
140
|
3 |
|
if (!$rate) { |
|
141
|
3 |
|
$rate = $this->_getRateFromDatabase($from, $to); |
|
142
|
3 |
|
} |
|
143
|
|
|
} else { |
|
144
|
3 |
|
$rate = $this->_getRateFromAPI($from, $to); |
|
|
|
|
|
|
145
|
3 |
|
} |
|
146
|
3 |
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
return $rate; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
2 |
|
/** |
|
152
|
2 |
|
* Format number using configuration |
|
153
|
2 |
|
* |
|
154
|
2 |
|
* @param float number to format |
|
155
|
2 |
|
* @return string formatted number |
|
156
|
2 |
|
*/ |
|
157
|
|
|
private function _formatConvert($number) |
|
158
|
|
|
{ |
|
159
|
5 |
|
if ($this->round) { |
|
160
|
|
|
$n = floor($number); |
|
161
|
5 |
|
$fraction = ($number - $n); |
|
162
|
5 |
|
if ($fraction != 0) { |
|
163
|
|
|
$step = 1/$this->round; |
|
164
|
5 |
|
$decimal = (((int)($fraction/$step) + 1) * $step); |
|
165
|
5 |
|
$number = $n + $decimal; |
|
166
|
5 |
|
} |
|
167
|
5 |
|
} |
|
168
|
|
|
$number = number_format($number, $this->decimal); |
|
169
|
5 |
|
|
|
170
|
|
|
return number_format($number, $this->decimal); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
5 |
|
/** |
|
174
|
5 |
|
* Check session to see if rate exists in. |
|
175
|
|
|
* |
|
176
|
5 |
|
* @param string $from currency to get the rate from. |
|
177
|
|
|
* @param string $to currency to get the rate to. |
|
178
|
|
|
* @return float|null $rate. |
|
179
|
|
|
*/ |
|
180
|
5 |
|
private function _getRateFromSession($from, $to) |
|
181
|
|
|
{ |
|
182
|
|
|
$session = $this->session->read('CurrencyConverter.' . $from . '-' . $to); |
|
183
|
5 |
|
if ($session) { |
|
184
|
|
|
$modified = new Time($session['modified']); |
|
185
|
5 |
|
if ($modified->wasWithinLast($this->refresh . ' hours')) { |
|
186
|
|
|
return $rate = $session['rate']; |
|
|
|
|
|
|
187
|
5 |
|
} |
|
188
|
5 |
|
} |
|
189
|
|
|
|
|
190
|
|
|
return null; |
|
191
|
5 |
|
} |
|
192
|
5 |
|
|
|
193
|
5 |
|
/** |
|
194
|
5 |
|
* Get a rate from database. |
|
195
|
5 |
|
* |
|
196
|
|
|
* It queries currencyratesTable and ... |
|
197
|
|
|
* if rate exists and has not to be modified, it returns this rate. |
|
198
|
|
|
* if rate exists and has to be modified, it call _getRateFromAPI method to get a fresh rate, then update in table and store in session this rate. |
|
199
|
|
|
* if rate does not exist, it call _getRateFromAPI to get a fresh rate, then create in table and store this rate. |
|
200
|
|
|
* |
|
201
|
5 |
|
* @param string $from currency to get the rate from |
|
202
|
5 |
|
* @param string $to currency to get the rate to |
|
203
|
|
|
* @return float|null $rate |
|
204
|
|
|
*/ |
|
205
|
|
|
private function _getRateFromDatabase($from, $to) |
|
206
|
|
|
{ |
|
207
|
|
|
$result = $this->currencyratesTable->find('all')->where(['from_currency' => $from, 'to_currency' => $to])->first(); |
|
208
|
5 |
|
if ($result) { |
|
209
|
|
|
if ($result->get('modified')->wasWithinLast($this->refresh . ' hours')) { |
|
210
|
5 |
|
$rate = $result->get('rate'); |
|
211
|
|
|
$this->_storeRateInSession($result); |
|
|
|
|
|
|
212
|
|
|
} else { |
|
213
|
7 |
|
$rate = $this->_getRateFromAPI($from, $to); |
|
|
|
|
|
|
214
|
|
|
if ($rate) { |
|
|
|
|
|
|
215
|
7 |
|
$result->rate = $rate; |
|
216
|
7 |
|
$this->currencyratesTable->save($result); |
|
217
|
|
|
$this->_storeRateInSession($result); |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
} else { |
|
221
|
|
|
$rate = $this->_getRateFromAPI($from, $to); |
|
|
|
|
|
|
222
|
|
|
if ($rate) { |
|
|
|
|
|
|
223
|
|
|
$result = $this->currencyratesTable->newEntity([ |
|
224
|
|
|
'from_currency' => $from, |
|
225
|
|
|
'to_currency' => $to, |
|
226
|
|
|
'rate' => $rate |
|
227
|
|
|
]); |
|
228
|
|
|
$this->currencyratesTable->save($result); |
|
229
|
|
|
$this->_storeRateInSession($result); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $rate; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Store in session a rate and his modified datetime |
|
238
|
|
|
* |
|
239
|
|
|
* @param \Cake\ORM\Entity $entity |
|
240
|
|
|
* @return void |
|
241
|
|
|
*/ |
|
242
|
|
|
private function _storeRateInSession($entity) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->session->write('CurrencyConverter.' . $entity->get('from_currency') . '-' . $entity->get('to_currency'), [ |
|
245
|
|
|
'rate' => $entity->get('rate'), |
|
246
|
|
|
'modified' => $entity->get('modified')->i18nFormat('yyyy-MM-dd HH:mm:ss') |
|
247
|
|
|
]); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Call free.currencyconverterapi.com API to get a rate for one currency to an other one currency. |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $from the currency. |
|
254
|
|
|
* @param string $to the currency. |
|
255
|
|
|
* @return int|null $rate. |
|
256
|
|
|
*/ |
|
257
|
|
|
private function _getRateFromAPI($from, $to) |
|
258
|
|
|
{ |
|
259
|
|
|
$rate = null; |
|
260
|
|
|
|
|
261
|
|
|
$url = 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $from . '_' . $to . '&compact=ultra'; |
|
262
|
|
|
$request = @fopen($url, 'r'); |
|
263
|
|
|
if ($request) { |
|
|
|
|
|
|
264
|
|
|
$response = fgets($request, 4096); |
|
265
|
|
|
fclose($request); |
|
266
|
|
|
$response = json_decode($response, true); |
|
267
|
|
|
if (isset($response[$from . '_' . $to])) { |
|
268
|
|
|
$rate = $response[$from . '_' . $to]; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $rate; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.