1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\ExchangeRate; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Contract\ManagerInterface; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface; |
14
|
|
|
use RunOpenCode\ExchangeRate\Contract\ProcessorsRegistryInterface; |
15
|
|
|
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface; |
16
|
|
|
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface; |
17
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourceInterface; |
18
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface; |
19
|
|
|
use RunOpenCode\ExchangeRate\Enum\RateType; |
20
|
|
|
use RunOpenCode\ExchangeRate\Exception\ExchangeRateException; |
21
|
|
|
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class Manager |
25
|
|
|
* |
26
|
|
|
* Default implementation of manager. |
27
|
|
|
* |
28
|
|
|
* @package RunOpenCode\ExchangeRate |
29
|
|
|
*/ |
30
|
|
|
class Manager implements ManagerInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $baseCurrency; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RepositoryInterface |
39
|
|
|
*/ |
40
|
|
|
protected $repository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var SourcesRegistryInterface |
44
|
|
|
*/ |
45
|
|
|
protected $sources; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ProcessorsRegistryInterface |
49
|
|
|
*/ |
50
|
|
|
protected $processors; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var RatesConfigurationRegistryInterface |
54
|
|
|
*/ |
55
|
|
|
protected $configurations; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Manager constructor. |
59
|
|
|
* |
60
|
|
|
* @param string $baseCurrency |
61
|
|
|
* @param RepositoryInterface $repository |
62
|
|
|
* @param SourcesRegistryInterface $sources |
63
|
|
|
* @param ProcessorsRegistryInterface $processors |
64
|
|
|
* @param RatesConfigurationRegistryInterface $configurations |
65
|
|
|
* |
66
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
67
|
|
|
*/ |
68
|
14 |
|
public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations) |
69
|
|
|
{ |
70
|
14 |
|
$this->baseCurrency = CurrencyCodeUtil::clean($baseCurrency); |
71
|
14 |
|
$this->repository = $repository; |
72
|
14 |
|
$this->configurations = $configurations; |
73
|
14 |
|
$this->sources = $sources; |
74
|
14 |
|
$this->processors = $processors; |
75
|
14 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get base currency |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function getBaseCurrency() |
83
|
|
|
{ |
84
|
1 |
|
return $this->baseCurrency; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
* |
90
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
91
|
|
|
*/ |
92
|
8 |
|
public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
93
|
|
|
{ |
94
|
8 |
|
return $this->repository->has($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
* |
100
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
101
|
|
|
*/ |
102
|
7 |
|
public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
103
|
|
|
{ |
104
|
7 |
|
return $this->repository->get($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
2 |
|
public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN, \DateTimeInterface $date = null) |
111
|
|
|
{ |
112
|
2 |
|
return $this->repository->latest($sourceName, CurrencyCodeUtil::clean($currencyCode), $rateType, $date); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
* |
118
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
119
|
|
|
*/ |
120
|
4 |
|
public function today($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
121
|
|
|
{ |
122
|
4 |
|
$currencyCode = CurrencyCodeUtil::clean($currencyCode); |
123
|
4 |
|
$today = new \DateTime('@'.time()); |
124
|
|
|
|
125
|
4 |
|
if ($this->has($sourceName, $currencyCode, $today, $rateType)) { |
126
|
1 |
|
return $this->get($sourceName, $currencyCode, $today, $rateType); |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
View Code Duplication |
if ((int) $today->format('N') >= 6 && $this->has($sourceName, $currencyCode, $lastFriday = new \DateTime('last Friday'), $rateType)) { |
|
|
|
|
130
|
2 |
|
return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
throw new ExchangeRateException(sprintf('Rate for currency code "%s" of type "%s" from source "%s" is not available for today "%s".', $currencyCode, $rateType, $sourceName, date('Y-m-d'))); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
* |
139
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
140
|
|
|
*/ |
141
|
3 |
|
public function historical($sourceName, $currencyCode, \DateTime $date, $rateType = RateType::MEDIAN) |
142
|
|
|
{ |
143
|
3 |
|
$currencyCode = CurrencyCodeUtil::clean($currencyCode); |
144
|
|
|
|
145
|
3 |
|
if ($this->has($sourceName, $currencyCode, $date, $rateType)) { |
146
|
1 |
|
return $this->get($sourceName, $currencyCode, $date, $rateType); |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
View Code Duplication |
if ((int) $date->format('N') === 6 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P1D')), $rateType)) { |
|
|
|
|
150
|
1 |
|
return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
View Code Duplication |
if ((int) $date->format('N') === 7 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P2D')), $rateType)) { |
|
|
|
|
154
|
1 |
|
return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
throw new ExchangeRateException(sprintf('Rate for currency code "%s" of type "%s" from source "%s" is not available for historical date "%s".', $currencyCode, $rateType, $sourceName, $date->format('Y-m-d'))); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
1 |
|
public function fetch($sourceName = null, \DateTime $date = null) |
164
|
|
|
{ |
165
|
1 |
|
$rates = array(); |
166
|
|
|
|
167
|
1 |
|
$filteredSourceNames = ($sourceName === null) ? array_map(function (SourceInterface $source) { |
168
|
|
|
return $source->getName(); |
169
|
1 |
|
}, $this->sources->all()) : (array) $sourceName; |
170
|
|
|
|
171
|
1 |
|
foreach ($filteredSourceNames as $name) { |
172
|
|
|
|
173
|
1 |
|
$source = $this->sources->get($name); |
174
|
|
|
|
175
|
1 |
|
$filteredConfigurations = $this->configurations->all(array( |
176
|
1 |
|
'sourceName' => $name, |
177
|
|
|
)); |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @var Configuration $configuration |
181
|
|
|
*/ |
182
|
1 |
|
foreach ($filteredConfigurations as $configuration) { |
183
|
1 |
|
$rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @var ProcessorInterface $processor |
189
|
|
|
*/ |
190
|
1 |
|
foreach ($this->processors->all() as $processor) { |
191
|
1 |
|
$rates = $processor->process($this->baseCurrency, $this->configurations, $rates); |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
$this->repository->save($rates); |
195
|
|
|
|
196
|
1 |
|
return $rates; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.