1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2016 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\Exception\ExchangeRateException; |
20
|
|
|
use RunOpenCode\ExchangeRate\Log\LoggerAwareTrait; |
21
|
|
|
use RunOpenCode\ExchangeRate\Registry\SourcesRegistry; |
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
|
8 |
|
public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations) |
61
|
|
|
{ |
62
|
8 |
|
$this->baseCurrency = CurrencyCodeUtil::clean($baseCurrency); |
63
|
8 |
|
$this->repository = $repository; |
64
|
8 |
|
$this->configurations = $configurations; |
65
|
8 |
|
$this->sources = $sources; |
66
|
8 |
|
$this->processors = $processors; |
67
|
8 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
6 |
|
public function has($sourceName, $currencyCode, $date = null, $rateType = 'default') |
73
|
|
|
{ |
74
|
6 |
|
return $this->repository->has($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
4 |
|
public function get($sourceName, $currencyCode, $date = null, $rateType = 'default') |
81
|
|
|
{ |
82
|
4 |
|
return $this->repository->get($sourceName, CurrencyCodeUtil::clean($currencyCode), $date, $rateType); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
2 |
|
public function latest($sourceName, $currencyCode, $rateType = 'default') |
89
|
|
|
{ |
90
|
2 |
|
return $this->repository->latest($sourceName, CurrencyCodeUtil::clean($currencyCode), $rateType); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
4 |
|
public function today($sourceName, $currencyCode, $rateType = 'default') |
97
|
|
|
{ |
98
|
4 |
|
$currencyCode = CurrencyCodeUtil::clean($currencyCode); |
99
|
4 |
|
$today = new \DateTime('now'); |
100
|
|
|
|
101
|
4 |
|
if ($this->has($sourceName, $currencyCode, $today, $rateType)) { |
102
|
2 |
|
return $this->get($sourceName, $currencyCode, $today, $rateType); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
if ((int)$today->format('N') >= 6) { |
106
|
|
|
$today = new \DateTime('last Friday'); |
107
|
|
|
return $this->get($sourceName, $currencyCode, $today, $rateType); |
108
|
|
|
} |
109
|
|
|
|
110
|
2 |
|
$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')); |
111
|
2 |
|
$this->getLogger()->critical($message); |
112
|
2 |
|
throw new ExchangeRateException($message); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
2 |
|
public function fetch($sourceName = null, $date = null) |
119
|
|
|
{ |
120
|
2 |
|
$rates = array(); |
121
|
|
|
|
122
|
2 |
|
$sourceNames = ($sourceName === null) ? array_map(function(SourceInterface $source) { |
123
|
|
|
return $source->getName(); |
124
|
2 |
|
}, $this->sources->all()) : (array) $sourceName; |
125
|
|
|
|
126
|
2 |
|
foreach ($sourceNames as $sourceName) { |
127
|
|
|
|
128
|
2 |
|
$source = $this->sources->get($sourceName); |
129
|
|
|
|
130
|
2 |
|
$configurations = $this->configurations->all(array( |
131
|
1 |
|
'sourceName' => $sourceName |
132
|
1 |
|
)); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @var Configuration $configuration |
136
|
|
|
*/ |
137
|
2 |
|
foreach ($configurations as $configuration) { |
138
|
2 |
|
$rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date); |
139
|
1 |
|
} |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @var ProcessorInterface $processor |
144
|
|
|
*/ |
145
|
2 |
|
foreach ($this->processors->all() as $processor) { |
146
|
2 |
|
$rates = $processor->process($this->baseCurrency, $this->configurations, $rates); |
147
|
1 |
|
} |
148
|
|
|
|
149
|
2 |
|
$this->repository->save($rates); |
150
|
2 |
|
} |
151
|
|
|
} |
152
|
|
|
|