|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
11
|
|
|
|
|
12
|
|
|
class Extension extends BaseExtension |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* {@inheritdoc} |
|
16
|
|
|
*/ |
|
17
|
2 |
|
public function load(array $config, ContainerBuilder $container) |
|
18
|
|
|
{ |
|
19
|
2 |
|
$configuration = new Configuration(); |
|
20
|
2 |
|
$config = $this->processConfiguration($configuration, $config); |
|
21
|
|
|
|
|
22
|
2 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
23
|
2 |
|
$loader->load('services.xml'); |
|
24
|
|
|
|
|
25
|
2 |
|
$this->configureExchangeRateService($config, $container); |
|
26
|
2 |
|
$this->configureSourcesRegistry($config, $container); |
|
27
|
2 |
|
$this->configureProcessorsRegistry($config, $container); |
|
28
|
2 |
|
$this->configureRatesRegistry($config, $container); |
|
29
|
2 |
|
$this->configureFileRepository($config, $container); |
|
30
|
2 |
|
$this->configureController($config, $container); |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
View Code Duplication |
protected function configureExchangeRateService(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate')) { |
|
36
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate'); |
|
37
|
|
|
|
|
38
|
2 |
|
$definition->setArguments(array( |
|
39
|
2 |
|
$config['base_currency'], |
|
40
|
2 |
|
new Reference($config['repository']), |
|
41
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.sources'), |
|
42
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.processors'), |
|
43
|
2 |
|
new Reference('run_open_code.exchange_rate.registry.rates') |
|
44
|
1 |
|
)); |
|
45
|
1 |
|
} |
|
46
|
2 |
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
protected function configureSourcesRegistry(array $config, ContainerBuilder $container) |
|
49
|
|
|
{ |
|
50
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.sources')) { |
|
51
|
|
|
|
|
52
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.sources'); |
|
53
|
|
|
|
|
54
|
2 |
|
$requiredSources = array(); |
|
55
|
|
|
|
|
56
|
2 |
|
foreach ($config['rates'] as $rate) { |
|
57
|
2 |
|
$requiredSources[$rate['source']] = $rate['source']; |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
2 |
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.source') as $id => $tags) { |
|
62
|
|
|
|
|
63
|
2 |
|
foreach ($tags as $attributes) { |
|
64
|
|
|
|
|
65
|
2 |
|
if (array_key_exists($attributes['alias'], $requiredSources)) { |
|
66
|
|
|
|
|
67
|
2 |
|
$definition->addMethodCall('add', array( |
|
68
|
2 |
|
new Reference($id) |
|
69
|
1 |
|
)); |
|
70
|
|
|
|
|
71
|
2 |
|
unset($requiredSources[$attributes['alias']]); |
|
72
|
1 |
|
} |
|
73
|
1 |
|
} |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
if (count($requiredSources) > 0) { |
|
77
|
|
|
throw new InvalidConfigurationException(sprintf('Required source(s) "%s" does not exists.', implode(', ', $requiredSources))); |
|
78
|
|
|
} |
|
79
|
1 |
|
} |
|
80
|
2 |
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
protected function configureProcessorsRegistry(array $config, ContainerBuilder $container) |
|
83
|
|
|
{ |
|
84
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.processors')) { |
|
85
|
|
|
|
|
86
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.processors'); |
|
87
|
|
|
|
|
88
|
2 |
|
$processors = array(); |
|
89
|
|
|
|
|
90
|
2 |
|
foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.processor') as $id => $tags) { |
|
91
|
|
|
|
|
92
|
2 |
|
if (!in_array($id, $config['processors'], true)) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
foreach ($tags as $attributes) { |
|
97
|
2 |
|
$processors[$id] = (isset($attributes['priority'])) ? intval($attributes['priority']) : 0; |
|
98
|
1 |
|
} |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
asort($processors); |
|
102
|
|
|
|
|
103
|
2 |
|
foreach (array_keys($processors) as $id) { |
|
104
|
2 |
|
$definition->addMethodCall('add', array( |
|
105
|
2 |
|
new Reference($id) |
|
106
|
1 |
|
)); |
|
107
|
1 |
|
} |
|
108
|
1 |
|
} |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
protected function configureRatesRegistry(array $config, ContainerBuilder $container) |
|
112
|
|
|
{ |
|
113
|
2 |
|
if ($container->hasDefinition('run_open_code.exchange_rate.registry.rates')) { |
|
114
|
|
|
|
|
115
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.registry.rates'); |
|
116
|
|
|
|
|
117
|
2 |
|
$processed = array(); |
|
118
|
|
|
|
|
119
|
2 |
|
foreach ($config['rates'] as $rateConfiguration) { |
|
120
|
|
|
|
|
121
|
2 |
|
$key = sprintf('%s_%s', $rateConfiguration['currency_code'], $rateConfiguration['rate_type']); |
|
122
|
|
|
|
|
123
|
2 |
|
if (isset($processed[$key])) { |
|
124
|
|
|
throw new InvalidConfigurationException(sprintf('Currency code "%s" for rate type "%s" is configured twice.', $rateConfiguration['currency_code'], $rateConfiguration['rate_type'])); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
2 |
|
$rateConfiguration['extra'] = (isset($ratesConfiguration['extra'])) ? $ratesConfiguration['extra'] : array(); |
|
128
|
|
|
|
|
129
|
2 |
|
$processed[$key] = $rateConfiguration; |
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
$definition->setArguments(array( |
|
133
|
2 |
|
array_values($processed) |
|
134
|
1 |
|
)); |
|
135
|
1 |
|
} |
|
136
|
2 |
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
139
|
|
|
{ |
|
140
|
|
|
if ( |
|
141
|
2 |
|
$config['repository'] === 'run_open_code.exchange_rate.repository.file_repository' |
|
142
|
1 |
|
&& |
|
143
|
2 |
|
$container->hasDefinition('run_open_code.exchange_rate.repository.file_repository') |
|
144
|
1 |
|
) { |
|
145
|
|
|
|
|
146
|
2 |
|
if (!empty($config['file_repository']) && !empty($config['file_repository']['path'])) { |
|
147
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.repository.file_repository'); |
|
148
|
2 |
|
$definition->setArguments(array( |
|
149
|
2 |
|
$config['file_repository']['path'] |
|
150
|
1 |
|
)); |
|
151
|
1 |
|
} else { |
|
152
|
1 |
|
throw new InvalidConfigurationException('You must configure location to the file where file repository will store exchange rates.'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
} elseif ($config['repository'] === 'run_open_code.exchange_rate.repository.file_repository') { |
|
156
|
|
|
throw new InvalidConfigurationException('File repository is used to store exchange rates, but it is not available in container.'); |
|
157
|
|
|
} else { |
|
158
|
|
|
$container->removeDefinition('run_open_code.exchange_rate.repository.file_repository'); |
|
159
|
|
|
} |
|
160
|
2 |
|
} |
|
161
|
|
|
|
|
162
|
2 |
View Code Duplication |
public function configureController(array $config, ContainerBuilder $container) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
2 |
|
if ($container->has('run_open_code.exchange_rate.controller')) { |
|
165
|
2 |
|
$definition = $container->getDefinition('run_open_code.exchange_rate.controller'); |
|
166
|
2 |
|
$definition->setArguments(array( |
|
167
|
2 |
|
new Reference('security.csrf.token_manager'), |
|
168
|
2 |
|
new Reference('translator'), |
|
169
|
2 |
|
new Reference($config['repository']), |
|
170
|
2 |
|
$config['base_currency'], |
|
171
|
2 |
|
$config['view'] |
|
172
|
1 |
|
)); |
|
173
|
1 |
|
} |
|
174
|
2 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* {@inheritdoc} |
|
178
|
|
|
*/ |
|
179
|
4 |
|
public function getAlias() |
|
180
|
|
|
{ |
|
181
|
4 |
|
return "run_open_code_exchange_rate"; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
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.