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 Psr\Log\LoggerInterface; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\ManagerInterface; |
14
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
15
|
|
|
use RunOpenCode\ExchangeRate\Enum\RateType; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class LoggableManager |
19
|
|
|
* |
20
|
|
|
* @package RunOpenCode\ExchangeRate |
21
|
|
|
*/ |
22
|
|
|
class LoggableManager implements ManagerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ManagerInterface |
26
|
|
|
*/ |
27
|
|
|
private $manager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var LoggerInterface |
31
|
|
|
*/ |
32
|
|
|
private $logger; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* LoggableManager constructor. |
36
|
|
|
* |
37
|
|
|
* @param ManagerInterface $manager |
38
|
|
|
* @param LoggerInterface $logger |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct(ManagerInterface $manager, LoggerInterface $logger) |
41
|
|
|
{ |
42
|
7 |
|
$this->manager = $manager; |
43
|
7 |
|
$this->logger = $logger; |
44
|
7 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
View Code Duplication |
public function has($sourceName, $currencyCode, \DateTime $date = null, $rateType = RateType::MEDIAN) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
try { |
52
|
1 |
|
return $this->manager->has($sourceName, $currencyCode, $date, $rateType); |
53
|
1 |
|
} catch (\Exception $e) { |
54
|
1 |
|
$this->logger->error('Unable to determine if rate for {currency_code} of type {rate_type} from source {source} on {date} exists.', [ |
55
|
1 |
|
'currency_code' => $currencyCode, |
56
|
1 |
|
'rate_type' => $rateType, |
57
|
1 |
|
'source' => $sourceName, |
58
|
1 |
|
'date' => (null === $date) ? date('Y-m-d') : $date->format('Y-m-d'), |
59
|
1 |
|
'exception' => $e, |
60
|
|
|
]); |
61
|
1 |
|
throw $e; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
1 |
View Code Duplication |
public function get($sourceName, $currencyCode, \DateTime $date, $rateType = RateType::MEDIAN) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
try { |
71
|
1 |
|
return $this->manager->get($sourceName, $currencyCode, $date, $rateType); |
72
|
1 |
|
} catch (\Exception $e) { |
73
|
1 |
|
$this->logger->error('Unable to fetch rate for {currency_code} of type {rate_type} from source {source} on {date}.', [ |
74
|
1 |
|
'currency_code' => $currencyCode, |
75
|
1 |
|
'rate_type' => $rateType, |
76
|
1 |
|
'source' => $sourceName, |
77
|
1 |
|
'date' => (null === $date) ? date('Y-m-d') : $date->format('Y-m-d'), |
78
|
1 |
|
'exception' => $e, |
79
|
|
|
]); |
80
|
1 |
|
throw $e; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
View Code Duplication |
public function latest($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
try { |
90
|
1 |
|
return $this->manager->latest($sourceName, $currencyCode, $rateType); |
91
|
1 |
|
} catch (\Exception $e) { |
92
|
1 |
|
$this->logger->error('Unable to fetch latest rate for {currency_code} of type {rate_type} from source {source}.', [ |
93
|
1 |
|
'currency_code' => $currencyCode, |
94
|
1 |
|
'rate_type' => $rateType, |
95
|
1 |
|
'source' => $sourceName, |
96
|
1 |
|
'exception' => $e, |
97
|
|
|
]); |
98
|
1 |
|
throw $e; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
1 |
View Code Duplication |
public function today($sourceName, $currencyCode, $rateType = RateType::MEDIAN) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
try { |
108
|
1 |
|
return $this->manager->today($sourceName, $currencyCode, $rateType); |
109
|
1 |
|
} catch (\Exception $e) { |
110
|
1 |
|
$this->logger->error('Unable to fetch today\'s rate for {currency_code} of type {rate_type} from source {source}.', [ |
111
|
1 |
|
'currency_code' => $currencyCode, |
112
|
1 |
|
'rate_type' => $rateType, |
113
|
1 |
|
'source' => $sourceName, |
114
|
1 |
|
'exception' => $e, |
115
|
|
|
]); |
116
|
1 |
|
throw $e; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
1 |
View Code Duplication |
public function historical($sourceName, $currencyCode, \DateTime $date, $rateType = RateType::MEDIAN) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
try { |
126
|
1 |
|
return $this->manager->historical($sourceName, $currencyCode, $date, $rateType); |
127
|
1 |
|
} catch (\Exception $e) { |
128
|
1 |
|
$this->logger->error('Unable to fetch historical rate for {currency_code} of type {rate_type} from source {source} on {date}.', [ |
129
|
1 |
|
'currency_code' => $currencyCode, |
130
|
1 |
|
'rate_type' => $rateType, |
131
|
1 |
|
'source' => $sourceName, |
132
|
1 |
|
'date' => (null === $date) ? date('Y-m-d') : $date->format('Y-m-d'), |
133
|
1 |
|
'exception' => $e, |
134
|
|
|
]); |
135
|
1 |
|
throw $e; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
2 |
|
public function fetch($sourceName = null, \DateTime $date = null) |
143
|
|
|
{ |
144
|
|
|
try { |
145
|
2 |
|
$rates = $this->manager->fetch($sourceName, $date); |
146
|
1 |
|
} catch (\Exception $e) { |
147
|
1 |
|
$this->logger->error('Unable to fetch rates from source {source} on {date}.', [ |
148
|
1 |
|
'source' => $sourceName, |
149
|
1 |
|
'date' => (null === $date) ? date('Y-m-d') : $date->format('Y-m-d'), |
150
|
1 |
|
'exception' => $e, |
151
|
|
|
]); |
152
|
1 |
|
throw $e; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @var RateInterface $rate |
157
|
|
|
*/ |
158
|
1 |
|
foreach ($rates as $rate) { |
159
|
1 |
|
$this->logger->debug('Fetched rate for {currency_code} of type {rate_type} from source {source} on {date}.', [ |
160
|
1 |
|
'currency_code' => $rate->getCurrencyCode(), |
161
|
1 |
|
'rate_type' => $rate->getRateType(), |
162
|
1 |
|
'source' => $rate->getSourceName(), |
163
|
1 |
|
'date' => $rate->getDate()->format('Y-m-d'), |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return $rates; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
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.