1 | <?php |
||
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 | 10 | public function __construct($baseCurrency, RepositoryInterface $repository, SourcesRegistryInterface $sources, ProcessorsRegistryInterface $processors, RatesConfigurationRegistryInterface $configurations) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 8 | public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = 'default') |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | */ |
||
80 | 8 | public function get($sourceName, $currencyCode, \DateTime $date = null, $rateType = 'default') |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 2 | public function latest($sourceName, $currencyCode, $rateType = 'default') |
|
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 | return $this->get($sourceName, $currencyCode, $today, $rateType); |
||
103 | } |
||
104 | |||
105 | 4 | if ((int)$today->format('N') >= 6) { |
|
106 | 4 | return $this->get($sourceName, $currencyCode, new \DateTime('last Friday'), $rateType); |
|
107 | } |
||
108 | |||
109 | $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')); |
||
110 | $this->getLogger()->critical($message); |
||
111 | throw new ExchangeRateException($message); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 2 | public function historical($sourceName, $currencyCode, \DateTime $date, $rateType = 'default') |
|
118 | { |
||
119 | 2 | $currencyCode = CurrencyCodeUtil::clean($currencyCode); |
|
120 | |||
121 | 2 | if ($this->has($sourceName, $currencyCode, $date, $rateType)) { |
|
122 | return $this->get($sourceName, $currencyCode, $date, $rateType); |
||
123 | } |
||
124 | |||
125 | 2 | if ((int)$date->format('N') === 6) { |
|
126 | $this->get($sourceName, $currencyCode, $date->sub(new \DateInterval('P1D')), $rateType); |
||
127 | 2 | } elseif ((int)$date->format('N') === 7) { |
|
128 | 2 | $this->get($sourceName, $currencyCode, $date->sub(new \DateInterval('P2D')), $rateType); |
|
129 | 2 | } |
|
130 | |||
131 | 2 | $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')); |
|
132 | 2 | $this->getLogger()->critical($message); |
|
133 | 2 | throw new ExchangeRateException($message); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 2 | public function fetch($sourceName = null, \DateTime $date = null) |
|
174 | } |
||
175 |