1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awin\ReportTask\Bundle\ReportBundle\Service; |
4
|
|
|
|
5
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Exception\CurrencyNotFoundException; |
6
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Model\CurrencyConverter; |
7
|
|
|
use Awin\ReportTask\Bundle\ReportBundle\Model\CurrencyWebservice; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The currencies aggregation services on currency conversion process |
11
|
|
|
* |
12
|
|
|
* @date 24/06/2017 |
13
|
|
|
* @time 18:49 |
14
|
|
|
* @author Peng Yue <[email protected]> |
15
|
|
|
* @copyright 2004-2017 Peng Yue |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
class CurrencyService implements CurrencyServiceInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The available currencies |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $currencies = [ |
26
|
|
|
'GBP' => '£', |
27
|
|
|
'EUR' => '€', |
28
|
|
|
'USD' => '$' |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var CurrencyConverter |
33
|
|
|
*/ |
34
|
|
|
private $currencyConverter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var CurrencyWebservice |
38
|
|
|
*/ |
39
|
|
|
private $currencyWebservice; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* CurrencyService constructor. |
43
|
|
|
*/ |
44
|
12 |
|
public function __construct() |
45
|
|
|
{ |
46
|
12 |
|
$this->currencyWebservice = new CurrencyWebservice(); |
47
|
12 |
|
$this->currencyConverter = new CurrencyConverter($this->currencyWebservice, $this->currencies); |
48
|
12 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the instance of the currency converter |
52
|
|
|
* |
53
|
|
|
* @return CurrencyConverter |
54
|
|
|
*/ |
55
|
2 |
|
public function getCurrencyConverter() |
56
|
|
|
{ |
57
|
2 |
|
return $this->currencyConverter; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the instance of the currency webservice |
62
|
|
|
* |
63
|
|
|
* @return CurrencyWebservice |
64
|
|
|
*/ |
65
|
1 |
|
public function getCurrencyWebservice() |
66
|
|
|
{ |
67
|
1 |
|
return $this->currencyWebservice; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the available currencies list |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
2 |
|
public function getCurrencies() |
76
|
|
|
{ |
77
|
2 |
|
return $this->currencies; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the currency symbol with currency ISO code |
82
|
|
|
* |
83
|
|
|
* @param string $currency |
84
|
|
|
* |
85
|
|
|
* @throw \Awin\ReportTask\Bundle\ReportBundle\Exception\CurrencyNotFoundException |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
4 |
|
public function currencyToSymbol($currency) |
89
|
|
|
{ |
90
|
4 |
|
if (!array_key_exists($currency, $this->currencies)) { |
91
|
1 |
|
throw new CurrencyNotFoundException($currency); |
92
|
|
|
} |
93
|
3 |
|
return $this->currencies[$currency]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the currency ISO code with currency symbol |
98
|
|
|
* |
99
|
|
|
* @param string $symbol |
100
|
|
|
* |
101
|
|
|
* @throw \Awin\ReportTask\Bundle\ReportBundle\Exception\CurrencyNotFoundException |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
5 |
|
public function symbolToCurrency($symbol) |
105
|
|
|
{ |
106
|
5 |
|
$currencies = array_flip($this->currencies); |
107
|
5 |
|
if (!array_key_exists($symbol, $currencies)) { |
108
|
1 |
|
throw new CurrencyNotFoundException($symbol); |
109
|
|
|
} |
110
|
4 |
|
return $currencies[$symbol]; |
111
|
|
|
} |
112
|
|
|
} |