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 RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil; |
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
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Extension |
21
|
|
|
* |
22
|
|
|
* Bundle extension. |
23
|
|
|
* |
24
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
25
|
|
|
*/ |
26
|
|
|
class Extension extends BaseExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
4 |
|
public function getAlias() |
32
|
|
|
{ |
33
|
4 |
|
return "run_open_code_exchange_rate"; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function load(array $config, ContainerBuilder $container) |
40
|
|
|
{ |
41
|
|
|
|
42
|
2 |
|
$configuration = new TreeConfiguration(); |
43
|
2 |
|
$config = $this->processConfiguration($configuration, $config); |
44
|
|
|
|
45
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services')); |
46
|
|
|
$loader->load('repository.xml'); |
47
|
|
|
$loader->load('command.xml'); |
48
|
|
|
$loader->load('controller.xml'); |
49
|
|
|
$loader->load('form_type.xml'); |
50
|
|
|
$loader->load('manager.xml'); |
51
|
|
|
$loader->load('processor.xml'); |
52
|
|
|
$loader->load('source.xml'); |
53
|
|
|
$loader->load('validator.xml'); |
54
|
|
|
|
55
|
|
|
$this |
56
|
|
|
->configureBaseCurrency($config, $container) |
57
|
|
|
->configureRepository($config, $container) |
58
|
|
|
->configureFileRepository($config, $container) |
59
|
|
|
->configureController($config, $container) |
60
|
|
|
->configureRates($config, $container) |
61
|
|
|
->configureSimpleSources($config, $container) |
62
|
|
|
->configureSourceType($config, $container) |
63
|
|
|
->configureRateTypeType($config, $container) |
64
|
|
|
->configureCurrencyCodeType($config, $container) |
65
|
|
|
->configureRateType($config, $container) |
66
|
|
|
->configureNotifications($config, $container) |
67
|
|
|
; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Configure base currency. |
72
|
|
|
* |
73
|
|
|
* @param array $config Configuration parameters. |
74
|
|
|
* @param ContainerBuilder $container Service container. |
75
|
|
|
* @return Extension $this Fluent interface. |
76
|
|
|
*/ |
77
|
|
|
protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
78
|
|
|
{ |
79
|
|
|
$baseCurrency = CurrencyCodeUtil::clean($config['base_currency']); |
80
|
|
|
$container->setParameter('run_open_code.exchange_rate.base_currency', $baseCurrency); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Configure rates. |
87
|
|
|
* |
88
|
|
|
* @param array $config Configuration parameters. |
89
|
|
|
* @param ContainerBuilder $container Service container. |
90
|
|
|
* @return Extension $this Fluent interface. |
91
|
|
|
*/ |
92
|
|
|
protected function configureRates(array $config, ContainerBuilder $container) |
93
|
|
|
{ |
94
|
|
|
if (count($config['rates']) === 0) { |
95
|
|
|
throw new \RuntimeException('You have to configure which rates you would like to use in your project.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$container->setParameter('run_open_code.exchange_rate.registered_rates', $config['rates']); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Configure required processors. |
104
|
|
|
* |
105
|
|
|
* @param array $config Configuration parameters. |
106
|
|
|
* @param ContainerBuilder $container Service container. |
107
|
|
|
* @return Extension $this Fluent interface. |
108
|
|
|
*/ |
109
|
|
|
protected function configureRepository(array $config, ContainerBuilder $container) |
110
|
|
|
{ |
111
|
|
|
$container->setParameter('run_open_code.exchange_rate.repository', $config['repository']); |
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Configure file repository, if used. |
117
|
|
|
* |
118
|
|
|
* @param array $config Configuration parameters. |
119
|
|
|
* @param ContainerBuilder $container Service container. |
120
|
|
|
* @return Extension $this Fluent interface. |
121
|
|
|
*/ |
122
|
|
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
123
|
|
|
{ |
124
|
|
|
$container->setParameter('run_open_code.exchange_rate.repository.file_repository.path', $config['file_repository']['path']); |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Configure controller. |
130
|
|
|
* |
131
|
|
|
* @param array $config Configuration parameters. |
132
|
|
|
* @param ContainerBuilder $container Service container. |
133
|
|
|
* @return Extension $this Fluent interface. |
134
|
|
|
*/ |
135
|
|
|
protected function configureController(array $config, ContainerBuilder $container) |
136
|
|
|
{ |
137
|
|
|
$container->setParameter( |
138
|
|
|
'run_open_code.exchange_rate.controller.view_configuration', |
139
|
|
|
$config['view'] |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Configure simple sources which does not have to be explicitly added to service container. |
147
|
|
|
* |
148
|
|
|
* @param array $config Configuration parameters. |
149
|
|
|
* @param ContainerBuilder $container Service container. |
150
|
|
|
* @return Extension $this Fluent interface. |
151
|
|
|
*/ |
152
|
|
|
protected function configureSimpleSources(array $config, ContainerBuilder $container) |
153
|
|
|
{ |
154
|
|
|
if (!empty($config['sources']) && count($config['sources']) > 0) { |
155
|
|
|
$container->setParameter('run_open_code.exchange_rate.source.registered_simple_sources', $config['sources']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
163
|
|
|
* |
164
|
|
|
* @param array $config Configuration parameters. |
165
|
|
|
* @param ContainerBuilder $container Service container. |
166
|
|
|
* @return Extension $this Fluent interface. |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
protected function configureSourceType(array $config, ContainerBuilder $container) |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$defaults = array_merge($config['form_types']['source_type'], array('choices' => array())); |
171
|
|
|
|
172
|
|
|
foreach ($config['rates'] as $rate) { |
173
|
|
|
$defaults['choices'][sprintf('exchange_rate.source.%s', $rate['source'])] = $rate['source']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.source_type', $defaults); |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
183
|
|
|
* |
184
|
|
|
* @param array $config Configuration parameters. |
185
|
|
|
* @param ContainerBuilder $container Service container. |
186
|
|
|
* @return Extension $this Fluent interface. |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
protected function configureRateTypeType(array $config, ContainerBuilder $container) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$defaults = array_merge($config['form_types']['rate_type_type'], array('choices' => array())); |
191
|
|
|
|
192
|
|
|
foreach ($config['rates'] as $rate) { |
193
|
|
|
$defaults['choices'][sprintf('exchange_rate.rate_type.%s.%s', $rate['source'], $rate['rate_type'])] = $rate['rate_type']; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.rate_type_type', $defaults); |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
203
|
|
|
* |
204
|
|
|
* @param array $config Configuration parameters. |
205
|
|
|
* @param ContainerBuilder $container Service container. |
206
|
|
|
* @return Extension $this Fluent interface. |
207
|
|
|
*/ |
208
|
|
|
protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
209
|
|
|
{ |
210
|
|
|
$defaults = array_merge($config['form_types']['currency_code_type'], array('choices' => array())); |
211
|
|
|
|
212
|
|
|
foreach ($config['rates'] as $rate) { |
213
|
|
|
$defaults['choices'][$rate['currency_code']] = $rate['currency_code']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
asort($defaults['choices']); |
217
|
|
|
|
218
|
|
|
$defaults['choices'] = array_merge(array($config['base_currency'] => $config['base_currency']), $defaults['choices']); |
219
|
|
|
|
220
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.currency_code_type', $defaults); |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
227
|
|
|
* |
228
|
|
|
* @param array $config Configuration parameters. |
229
|
|
|
* @param ContainerBuilder $container Service container. |
230
|
|
|
* @return Extension $this Fluent interface. |
231
|
|
|
*/ |
232
|
|
|
protected function configureRateType(array $config, ContainerBuilder $container) |
233
|
|
|
{ |
234
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.rate_type', $config['form_types']['rate_type']); |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Configure internal notifications. |
241
|
|
|
* |
242
|
|
|
* @param array $config Configuration parameters. |
243
|
|
|
* @param ContainerBuilder $container Service container. |
244
|
|
|
* @return Extension $this Fluent interface. |
245
|
|
|
*/ |
246
|
|
|
protected function configureNotifications(array $config, ContainerBuilder $container) |
247
|
|
|
{ |
248
|
|
|
if (!empty($config['notifications']['fetch'])) { |
249
|
|
|
$container->setParameter('run_open_code.exchange_rate.notifications.fetch', $config['notifications']['fetch']); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
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.