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\Log\LoggerAwareTrait; |
22
|
|
|
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Manager |
26
|
|
|
* |
27
|
|
|
* Default implementation of manager. |
28
|
|
|
* |
29
|
|
|
* @package RunOpenCode\ExchangeRate |
30
|
|
|
*/ |
31
|
|
|
class Manager implements ManagerInterface |
32
|
|
|
{ |
33
|
|
|
use LoggerAwareTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $baseCurrency; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var RepositoryInterface |
42
|
|
|
*/ |
43
|
|
|
protected $repository; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var SourcesRegistryInterface |
47
|
|
|
*/ |
48
|
|
|
protected $sources; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ProcessorsRegistryInterface |
52
|
|
|
*/ |
53
|
|
|
protected $processors; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var RatesConfigurationRegistryInterface |
57
|
|
|
*/ |
58
|
|
|
protected $configurations; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Manager constructor. |
62
|
|
|
* |
63
|
|
|
* @param string $baseCurrency |
64
|
|
|
* @param RepositoryInterface $repository |
65
|
|
|
* @param SourcesRegistryInterface $sources |
66
|
|
|
* @param ProcessorsRegistryInterface $processors |
67
|
|
|
* @param RatesConfigurationRegistryInterface $configurations |
68
|
|
|
* |
69
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
70
|
|
|
*/ |
71
|
5 |
|
public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations) |
72
|
|
|
{ |
73
|
5 |
|
$this->baseCurrency = CurrencyCodeUtil::clean($baseCurrency); |
74
|
5 |
|
$this->repository = $repository; |
75
|
5 |
|
$this->configurations = $configurations; |
76
|
5 |
|
$this->sources = $sources; |
77
|
5 |
|
$this->processors = $processors; |
78
|
5 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
* |
83
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
84
|
|
|
*/ |
85
|
4 |
|
public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
86
|
|
|
{ |
87
|
4 |
|
return $this->repository->has($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
* |
93
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
94
|
|
|
*/ |
95
|
3 |
|
public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
96
|
|
|
{ |
97
|
3 |
|
return $this->repository->get($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
1 |
|
public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
104
|
|
|
{ |
105
|
1 |
|
return $this->repository->latest($sourceName, CurrencyCodeUtil::clean($currencyCode), $rateType); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
* |
111
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
112
|
|
|
*/ |
113
|
2 |
|
public function today($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
114
|
|
|
{ |
115
|
2 |
|
$currencyCode = CurrencyCodeUtil::clean($currencyCode); |
116
|
2 |
|
$today = new \DateTime('now'); |
117
|
|
|
|
118
|
2 |
|
if ($this->has($sourceName, $currencyCode, $today, $rateType)) { |
119
|
|
|
return $this->get($sourceName, $currencyCode, $today, $rateType); |
120
|
|
|
} |
121
|
|
|
|
122
|
2 |
View Code Duplication |
if ((int) $today->format('N') >= 6 && $this->has($sourceName, $currencyCode, $lastFriday = new \DateTime('last Friday'), $rateType)) { |
|
|
|
|
123
|
1 |
|
return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
$message = 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')); |
127
|
1 |
|
$this->getLogger()->critical($message); |
128
|
1 |
|
throw new ExchangeRateException($message); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
* |
134
|
|
|
* @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
135
|
|
|
*/ |
136
|
1 |
|
public function historical($sourceName, $currencyCode, \DateTime $date, $rateType = RateType::MEDIAN) |
137
|
|
|
{ |
138
|
1 |
|
$currencyCode = CurrencyCodeUtil::clean($currencyCode); |
139
|
|
|
|
140
|
1 |
|
if ($this->has($sourceName, $currencyCode, $date, $rateType)) { |
141
|
|
|
return $this->get($sourceName, $currencyCode, $date, $rateType); |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
if ((int) $date->format('N') === 6 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P1D')), $rateType)) { |
145
|
1 |
|
return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
146
|
|
View Code Duplication |
} elseif ((int) $date->format('N') === 7 && $this->has($sourceName, $currencyCode, $lastFriday = $date->sub(new \DateInterval('P2D')), $rateType)) { |
|
|
|
|
147
|
|
|
return $this->get($sourceName, $currencyCode, $lastFriday, $rateType); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$message = 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')); |
151
|
|
|
$this->getLogger()->critical($message); |
152
|
|
|
throw new ExchangeRateException($message); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
1 |
|
public function fetch($sourceName = null, \DateTime $date = null) |
159
|
|
|
{ |
160
|
1 |
|
$rates = array(); |
161
|
|
|
|
162
|
1 |
|
$filteredSourceNames = ($sourceName === null) ? array_map(function (SourceInterface $source) { |
163
|
|
|
return $source->getName(); |
164
|
1 |
|
}, $this->sources->all()) : (array) $sourceName; |
165
|
|
|
|
166
|
1 |
|
foreach ($filteredSourceNames as $name) { |
167
|
|
|
|
168
|
1 |
|
$source = $this->sources->get($name); |
169
|
|
|
|
170
|
1 |
|
$filteredConfigurations = $this->configurations->all(array( |
171
|
1 |
|
'sourceName' => $name, |
172
|
|
|
)); |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @var Configuration $configuration |
176
|
|
|
*/ |
177
|
1 |
|
foreach ($filteredConfigurations as $configuration) { |
178
|
1 |
|
$rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @var ProcessorInterface $processor |
184
|
|
|
*/ |
185
|
1 |
|
foreach ($this->processors->all() as $processor) { |
186
|
1 |
|
$rates = $processor->process($this->baseCurrency, $this->configurations, $rates); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
$this->repository->save($rates); |
190
|
|
|
|
191
|
1 |
|
return $rates; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Get base currency |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function getBaseCurrency() |
200
|
|
|
{ |
201
|
|
|
return $this->baseCurrency; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
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.