|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RunOpenCode\ExchangeRate; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
6
|
|
|
use RunOpenCode\ExchangeRate\Contract\ManagerInterface; |
|
7
|
|
|
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface; |
|
8
|
|
|
use RunOpenCode\ExchangeRate\Contract\ProcessorsRegistryInterface; |
|
9
|
|
|
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface; |
|
10
|
|
|
use RunOpenCode\ExchangeRate\Contract\RepositoryInterface; |
|
11
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourceInterface; |
|
12
|
|
|
use RunOpenCode\ExchangeRate\Contract\SourcesRegistryInterface; |
|
13
|
|
|
use RunOpenCode\ExchangeRate\Utils\CurrencyCode; |
|
14
|
|
|
|
|
15
|
|
|
class Manager implements ManagerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use LoggerAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $baseCurrency; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var RepositoryInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $repository; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var SourcesRegistryInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $sources; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ProcessorsRegistryInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $processors; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RatesConfigurationRegistryInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $configurations; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->baseCurrency = CurrencyCode::validate($baseCurrency); |
|
47
|
|
|
$this->repository = $repository; |
|
48
|
|
|
$this->configurations = $configurations; |
|
49
|
|
|
$this->sources = $sources; |
|
50
|
|
|
$this->processors = $processors; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function has($currencyCode, $date = null, $rateType = 'default') |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->repository->has(CurrencyCode::validate($currencyCode), $date, $rateType); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function get($currencyCode, $date = null, $rateType = 'default') |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->repository->get(CurrencyCode::validate($currencyCode), $date, $rateType); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function latest($currencyCode, $rateType = 'default') |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->repository->latest(CurrencyCode::validate($currencyCode), $rateType); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function today($currencyCode, $rateType = 'default') |
|
81
|
|
|
{ |
|
82
|
|
|
$currencyCode = CurrencyCode::validate($currencyCode); |
|
83
|
|
|
$today = new \DateTime('now'); |
|
84
|
|
|
|
|
85
|
|
|
if ($this->has($currencyCode, $rateType, $today)) { |
|
|
|
|
|
|
86
|
|
|
return $this->get($currencyCode, $rateType, $today); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ((int)$today->format('N') >= 6) { |
|
90
|
|
|
$today = new \DateTime('last Friday'); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function fetch($sourceName = null, $date = null) |
|
98
|
|
|
{ |
|
99
|
|
|
$sources = $this->sources->all(); |
|
100
|
|
|
|
|
101
|
|
|
if (!is_null($sourceName)) { |
|
102
|
|
|
$sources = array(); |
|
103
|
|
|
$sourceNames = is_array($sourceName) ? $sourceName : array($sourceName); |
|
104
|
|
|
|
|
105
|
|
|
foreach ($sourceNames as $sourceName) { |
|
106
|
|
|
$sources[] = $this->sources->get($sourceName); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$rates = array(); |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @var SourceInterface $source |
|
114
|
|
|
*/ |
|
115
|
|
|
foreach ($sources as $source) { |
|
116
|
|
|
$configurations = $this->configurations->find($source->getName()); |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @var Configuration $configuration |
|
120
|
|
|
*/ |
|
121
|
|
|
foreach ($configurations as $configuration) { |
|
122
|
|
|
$rates[] = $source->fetch($configuration->getCurrencyCode(), $configuration->getRateType(), $date); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @var ProcessorInterface $processor |
|
128
|
|
|
*/ |
|
129
|
|
|
foreach ($this->processors as $processor) { |
|
130
|
|
|
$rates = $processor->process($this->baseCurrency, $this->configurations, $rates); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$this->repository->save($rates); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: