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\Registry; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Configuration; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\AliasRegistryInterface; |
14
|
|
|
use RunOpenCode\ExchangeRate\Contract\ManagerInterface; |
15
|
|
|
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AliasRegistry |
19
|
|
|
* |
20
|
|
|
* Default implementation of alias registry. |
21
|
|
|
* |
22
|
|
|
* @package RunOpenCode\ExchangeRate\Registry |
23
|
|
|
*/ |
24
|
|
|
class AliasRegistry implements AliasRegistryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $registry; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ManagerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $manager; |
35
|
|
|
|
36
|
|
|
public function __construct(ManagerInterface $manager, RatesConfigurationRegistryInterface $configurations) |
37
|
|
|
{ |
38
|
|
|
$this->manager = $manager; |
39
|
|
|
$this->registry = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Configuration $configuration |
43
|
|
|
*/ |
44
|
|
|
foreach ($configurations as $configuration) { |
45
|
|
|
|
46
|
|
|
if ($configuration->getAlias() !== null) { |
47
|
|
|
$this->registry[$configuration->getAlias()] = $configuration; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function has($alias, $date = null) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
if (array_key_exists($alias, $this->registry)) { |
58
|
|
|
/** |
59
|
|
|
* @var Configuration $configuration |
60
|
|
|
*/ |
61
|
|
|
$configuration = $this->registry[$alias]; |
62
|
|
|
|
63
|
|
|
return $this->manager->has( |
64
|
|
|
$configuration->getSource(), |
65
|
|
|
$configuration->getCurrencyCode(), |
66
|
|
|
$date, |
67
|
|
|
$configuration->getRateType() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new \RuntimeException(sprintf('Unknown alias "%s".', $alias)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function get($alias, $date = null) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
if (array_key_exists($alias, $this->registry)) { |
80
|
|
|
/** |
81
|
|
|
* @var Configuration $configuration |
82
|
|
|
*/ |
83
|
|
|
$configuration = $this->registry[$alias]; |
84
|
|
|
|
85
|
|
|
return $this->manager->get( |
86
|
|
|
$configuration->getSource(), |
87
|
|
|
$configuration->getCurrencyCode(), |
88
|
|
|
$date, |
89
|
|
|
$configuration->getRateType() |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw new \RuntimeException(sprintf('Unknown alias "%s".', $alias)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
public function latest($alias, $date = null) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
if (array_key_exists($alias, $this->registry)) { |
102
|
|
|
/** |
103
|
|
|
* @var Configuration $configuration |
104
|
|
|
*/ |
105
|
|
|
$configuration = $this->registry[$alias]; |
106
|
|
|
|
107
|
|
|
return $this->manager->latest( |
108
|
|
|
$configuration->getSource(), |
109
|
|
|
$configuration->getCurrencyCode(), |
110
|
|
|
$configuration->getRateType() |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw new \RuntimeException(sprintf('Unknown alias "%s".', $alias)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function today($alias) |
121
|
|
|
{ |
122
|
|
|
if (array_key_exists($alias, $this->registry)) { |
123
|
|
|
/** |
124
|
|
|
* @var Configuration $configuration |
125
|
|
|
*/ |
126
|
|
|
$configuration = $this->registry[$alias]; |
127
|
|
|
|
128
|
|
|
return $this->manager->today( |
129
|
|
|
$configuration->getSource(), |
130
|
|
|
$configuration->getCurrencyCode(), |
131
|
|
|
$configuration->getRateType() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
throw new \RuntimeException(sprintf('Unknown alias "%s".', $alias)); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.