1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate Bundle, 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\Bundle\ExchangeRate\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\Bundle\ExchangeRate\DependencyInjection\Configuration as TreeConfiguration; |
13
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
14
|
|
|
use Symfony\Component\Config\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Extension |
22
|
|
|
* |
23
|
|
|
* Bundle extension. |
24
|
|
|
* |
25
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
26
|
|
|
*/ |
27
|
|
|
class Extension extends BaseExtension |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
4 |
|
public function getAlias() |
33
|
|
|
{ |
34
|
4 |
|
return "run_open_code_exchange_rate"; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
2 |
|
public function load(array $config, ContainerBuilder $container) |
41
|
|
|
{ |
42
|
2 |
|
$configuration = new TreeConfiguration(); |
43
|
2 |
|
$config = $this->processConfiguration($configuration, $config); |
44
|
|
|
|
45
|
2 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
46
|
2 |
|
$loader->load('services.xml'); |
47
|
|
|
|
48
|
2 |
|
$this->configureExchangeRateService($config, $container); |
49
|
2 |
|
$this->configureSourcesRegistry($config, $container); |
50
|
2 |
|
$this->configureProcessorsRegistry($config, $container); |
51
|
2 |
|
$this->configureRatesRegistry($config, $container); |
52
|
2 |
|
$this->configureFileRepository($config, $container); |
53
|
2 |
|
$this->configureController($config, $container); |
54
|
2 |
|
$this->configureDebugCommand($config, $container); |
55
|
2 |
|
$this->configureCurrencyType($config, $container); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Configure exchange rate service. |
60
|
|
|
* |
61
|
|
|
* @param array $config |
62
|
|
|
* @param ContainerBuilder $container |
63
|
2 |
|
*/ |
64
|
|
|
protected function configureExchangeRateService(array $config, ContainerBuilder $container) |
65
|
2 |
|
{ |
66
|
|
|
if ($container->hasDefinition('run_open_code.exchange_rate')) { |
67
|
2 |
|
|
68
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate'); |
69
|
2 |
|
|
70
|
2 |
|
$definition->setArguments(array( |
71
|
2 |
|
$config['base_currency'], |
72
|
2 |
|
new Reference($config['repository']), |
73
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.sources'), |
74
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.processors'), |
75
|
1 |
|
new Reference('run_open_code.exchange_rate.registry.rates') |
76
|
1 |
|
)); |
77
|
2 |
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Configure sources registry. |
82
|
|
|
* |
83
|
|
|
* @param array $config |
84
|
|
|
* @param ContainerBuilder $container |
85
|
2 |
|
*/ |
86
|
|
|
protected function configureSourcesRegistry(array $config, ContainerBuilder $container) |
87
|
2 |
|
{ |
88
|
|
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.sources')) { |
89
|
2 |
|
|
90
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.sources'); |
91
|
|
|
$requiredSources = $this->getRequiredSources($config); |
92
|
|
|
|
93
|
2 |
|
|
94
|
|
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.source') as $id => $tags) { |
95
|
2 |
|
|
96
|
|
|
foreach ($tags as $attributes) { |
97
|
2 |
|
|
98
|
|
|
if (array_key_exists($attributes['alias'], $requiredSources)) { |
99
|
2 |
|
|
100
|
2 |
|
$definition->addMethodCall('add', array( |
101
|
1 |
|
new Reference($id) |
102
|
|
|
)); |
103
|
2 |
|
|
104
|
1 |
|
unset($requiredSources[$attributes['alias']]); |
105
|
1 |
|
} |
106
|
1 |
|
} |
107
|
|
|
} |
108
|
2 |
|
|
109
|
|
|
if (count($requiredSources) > 0) { |
110
|
|
|
throw new InvalidConfigurationException(sprintf('Required source(s) "%s" does not exists.', implode(', ', $requiredSources))); |
111
|
1 |
|
} |
112
|
2 |
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Configure processors registry. |
117
|
|
|
* |
118
|
|
|
* @param array $config |
119
|
|
|
* @param ContainerBuilder $container |
120
|
2 |
|
*/ |
121
|
|
|
protected function configureProcessorsRegistry(array $config, ContainerBuilder $container) |
122
|
2 |
|
{ |
123
|
|
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.processors')) { |
124
|
2 |
|
|
125
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.processors'); |
126
|
2 |
|
|
127
|
|
|
$processors = array(); |
128
|
2 |
|
|
129
|
|
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.processor') as $id => $tags) { |
130
|
2 |
|
|
131
|
2 |
|
if (!in_array($id, $config['processors'], true)) { |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
2 |
|
|
135
|
2 |
|
foreach ($tags as $attributes) { |
136
|
1 |
|
$processors[$id] = (isset($attributes['priority'])) ? intval($attributes['priority']) : 0; |
137
|
1 |
|
} |
138
|
|
|
} |
139
|
2 |
|
|
140
|
|
|
asort($processors); |
141
|
2 |
|
|
142
|
2 |
|
foreach ($processors as $id => $processor) { |
143
|
2 |
|
$definition->addMethodCall('add', array( |
144
|
1 |
|
new Reference($id) |
145
|
1 |
|
)); |
146
|
1 |
|
} |
147
|
2 |
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Configure rates. |
152
|
|
|
* |
153
|
|
|
* @param array $config |
154
|
|
|
* @param ContainerBuilder $container |
155
|
2 |
|
*/ |
156
|
|
|
protected function configureRatesRegistry(array $config, ContainerBuilder $container) |
157
|
2 |
|
{ |
158
|
|
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.rates')) { |
159
|
2 |
|
|
160
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.rates'); |
161
|
2 |
|
|
162
|
|
|
$aliases = array(); |
163
|
2 |
|
|
164
|
|
|
foreach ($config['rates'] as $rateConfiguration) { |
165
|
2 |
|
|
166
|
2 |
|
if ($rateConfiguration['alias'] === null) { |
167
|
|
|
continue; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (array_key_exists($rateConfiguration['alias'], $aliases)) { |
171
|
|
|
throw new InvalidConfigurationException(sprintf('Rate with alias "%s" is already defined.', $rateConfiguration['alias'])); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
$aliases[$rateConfiguration['alias']] = $rateConfiguration; |
175
|
|
|
} |
176
|
2 |
|
|
177
|
1 |
|
$definition->setArguments(array(array_values($config['rates']))); |
178
|
2 |
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Configure file registry, if used. |
183
|
|
|
* |
184
|
|
|
* @param array $config |
185
|
|
|
* @param ContainerBuilder $container |
186
|
2 |
|
*/ |
187
|
|
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
188
|
|
|
{ |
189
|
2 |
|
if ( |
190
|
1 |
|
$config['repository'] === 'run_open_code.exchange_rate.repository.file_repository' |
191
|
2 |
|
&& |
192
|
1 |
|
$container->hasDefinition('run_open_code.exchange_rate.repository.file_repository') |
193
|
|
|
) { |
194
|
2 |
|
|
195
|
2 |
|
if (!empty($config['file_repository']) && !empty($config['file_repository']['path'])) { |
196
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.repository.file_repository'); |
197
|
2 |
|
$definition->setArguments(array( |
198
|
1 |
|
$config['file_repository']['path'] |
199
|
1 |
|
)); |
200
|
1 |
|
} else { |
201
|
|
|
throw new InvalidConfigurationException('You must configure location to the file where file repository will store exchange rates.'); |
202
|
|
|
} |
203
|
1 |
|
|
204
|
|
|
} elseif ($config['repository'] === 'run_open_code.exchange_rate.repository.file_repository') { |
205
|
|
|
throw new InvalidConfigurationException('File repository is used to store exchange rates, but it is not available in container.'); |
206
|
|
|
} else { |
207
|
|
|
$container->removeDefinition('run_open_code.exchange_rate.repository.file_repository'); |
208
|
2 |
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Configure controller - view layer. |
213
|
|
|
* |
214
|
|
|
* @param array $config |
215
|
|
|
* @param ContainerBuilder $container |
216
|
2 |
|
*/ |
217
|
|
|
protected function configureController(array $config, ContainerBuilder $container) |
218
|
2 |
|
{ |
219
|
2 |
|
if ($container->has('run_open_code.exchange_rate.controller')) { |
220
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.controller'); |
221
|
2 |
|
$definition->setArguments(array( |
222
|
2 |
|
new Reference($config['repository']), |
223
|
2 |
|
$config['base_currency'], |
224
|
1 |
|
$config['view'] |
225
|
1 |
|
)); |
226
|
2 |
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Configure debug command. |
231
|
|
|
* |
232
|
|
|
* @param array $config |
233
|
|
|
* @param ContainerBuilder $container |
234
|
2 |
|
*/ |
235
|
|
View Code Duplication |
protected function configureDebugCommand(array $config, ContainerBuilder $container) |
|
|
|
|
236
|
2 |
|
{ |
237
|
2 |
|
if ($container->has('run_open_code.exchange_rate.command.configuration_debug')) { |
238
|
|
|
$definition = $container->getDefinition('run_open_code.exchange_rate.command.configuration_debug'); |
239
|
2 |
|
|
240
|
2 |
|
$arguments = $definition->getArguments(); |
241
|
|
|
$arguments[3] = new Reference($config['repository']); |
242
|
2 |
|
|
243
|
1 |
|
$definition->setArguments($arguments); |
244
|
2 |
|
} |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Configure currency type. |
249
|
|
|
* |
250
|
|
|
* @param array $config |
251
|
|
|
* @param ContainerBuilder $container |
252
|
2 |
|
*/ |
253
|
|
View Code Duplication |
protected function configureCurrencyType(array $config, ContainerBuilder $container) |
|
|
|
|
254
|
2 |
|
{ |
255
|
|
|
if ($container->has('run_open_code.exchange_rate.type.currency_type')) { |
256
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.type.currency_type'); |
257
|
2 |
|
|
258
|
1 |
|
$arguments = $definition->getArguments(); |
259
|
|
|
$arguments[2] = $config['base_currency']; |
260
|
2 |
|
|
261
|
|
|
$definition->setArguments($arguments); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Extract name of only required sources from configuration. |
267
|
|
|
* |
268
|
|
|
* @param array $config |
269
|
|
|
* @return array |
270
|
|
|
*/ |
271
|
|
|
protected function getRequiredSources(array $config) |
272
|
|
|
{ |
273
|
|
|
$requiredSources = array(); |
274
|
|
|
|
275
|
|
|
foreach ($config['rates'] as $rate) { |
276
|
|
|
$requiredSources[$rate['source']] = $rate['source']; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $requiredSources; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
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.