Completed
Push — master ( b8ee64...bf1b53 )
by Nikola
04:04
created

Extension::configureSourceType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 6
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
        ;
67
    }
68
69
    /**
70
     * Configure base currency.
71
     *
72
     * @param array $config Configuration parameters.
73
     * @param ContainerBuilder $container Service container.
74
     * @return Extension $this Fluent interface.
75
     */
76
    protected function configureBaseCurrency(array $config, ContainerBuilder $container)
77
    {
78
        $baseCurrency = CurrencyCodeUtil::clean($config['base_currency']);
79
        $container->setParameter('run_open_code.exchange_rate.base_currency', $baseCurrency);
80
81
        return $this;
82
    }
83
84
    /**
85
     * Configure rates.
86
     *
87
     * @param array $config Configuration parameters.
88
     * @param ContainerBuilder $container Service container.
89
     * @return Extension $this Fluent interface.
90
     */
91
    protected function configureRates(array $config, ContainerBuilder $container)
92
    {
93
        if (count($config['rates']) === 0) {
94
            throw new \RuntimeException('You have to configure which rates you would like to use in your project.');
95
        }
96
97
        $container->setParameter('run_open_code.exchange_rate.registered_rates', $config['rates']);
98
        return $this;
99
    }
100
101
    /**
102
     * Configure required processors.
103
     *
104
     * @param array $config Configuration parameters.
105
     * @param ContainerBuilder $container Service container.
106
     * @return Extension $this Fluent interface.
107
     */
108
    protected function configureRepository(array $config, ContainerBuilder $container)
109
    {
110
        $container->setParameter('run_open_code.exchange_rate.repository', $config['repository']);
111
        return $this;
112
    }
113
114
    /**
115
     * Configure file repository, if used.
116
     *
117
     * @param array $config Configuration parameters.
118
     * @param ContainerBuilder $container Service container.
119
     * @return Extension $this Fluent interface.
120
     */
121
    protected function configureFileRepository(array $config, ContainerBuilder $container)
122
    {
123
        $container->setParameter('run_open_code.exchange_rate.repository.file_repository.path', $config['file_repository']['path']);
124
        return $this;
125
    }
126
127
    /**
128
     * Configure controller.
129
     *
130
     * @param array $config Configuration parameters.
131
     * @param ContainerBuilder $container Service container.
132
     * @return Extension $this Fluent interface.
133
     */
134
    protected function configureController(array $config, ContainerBuilder $container)
135
    {
136
        $container->setParameter(
137
            'run_open_code.exchange_rate.controller.view_configuration',
138
            $config['view']
139
        );
140
141
        return $this;
142
    }
143
144
    /**
145
     * Configure simple sources which does not have to be explicitly added to service container.
146
     *
147
     * @param array $config Configuration parameters.
148
     * @param ContainerBuilder $container Service container.
149
     * @return Extension $this Fluent interface.
150
     */
151
    protected function configureSimpleSources(array $config, ContainerBuilder $container)
152
    {
153
        if (!empty($config['sources']) && count($config['sources']) > 0) {
154
            $container->setParameter('run_open_code.exchange_rate.source.registered_simple_sources', $config['sources']);
155
        }
156
157
        return $this;
158
    }
159
160
    /**
161
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings.
162
     *
163
     * @param array $config Configuration parameters.
164
     * @param ContainerBuilder $container Service container.
165
     * @return Extension $this Fluent interface.
166
     */
167 View Code Duplication
    protected function configureSourceType(array $config, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
168
    {
169
        $defaults = array_merge($config['form_types']['source_type'], array('choices' => array()));
170
171
        foreach ($config['rates'] as $rate) {
172
            $defaults['choices'][sprintf('exchange_rate.source.%s', $rate['source'])] = $rate['source'];
173
        }
174
175
        $container->setParameter('run_open_code.exchange_rate.form_type.source_type', $defaults);
176
177
        return $this;
178
    }
179
180
    /**
181
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings.
182
     *
183
     * @param array $config Configuration parameters.
184
     * @param ContainerBuilder $container Service container.
185
     * @return Extension $this Fluent interface.
186
     */
187 View Code Duplication
    protected function configureRateTypeType(array $config, ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
188
    {
189
        $defaults = array_merge($config['form_types']['rate_type_type'], array('choices' => array()));
190
191
        foreach ($config['rates'] as $rate) {
192
            $defaults['choices'][sprintf('exchange_rate.rate_type.%s.%s', $rate['source'], $rate['rate_type'])] = $rate['rate_type'];
193
        }
194
195
        $container->setParameter('run_open_code.exchange_rate.form_type.rate_type_type', $defaults);
196
197
        return $this;
198
    }
199
200
    /**
201
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings.
202
     *
203
     * @param array $config Configuration parameters.
204
     * @param ContainerBuilder $container Service container.
205
     * @return Extension $this Fluent interface.
206
     */
207
    protected function configureCurrencyCodeType(array $config, ContainerBuilder $container)
208
    {
209
        $defaults = array_merge($config['form_types']['currency_code_type'], array('choices' => array()));
210
211
        foreach ($config['rates'] as $rate) {
212
            $defaults['choices'][$rate['currency_code']] = $rate['currency_code'];
213
        }
214
215
        asort($defaults['choices']);
216
217
        $defaults['choices'] = array_merge(array($config['base_currency'] => $config['base_currency']), $defaults['choices']);
218
219
        $container->setParameter('run_open_code.exchange_rate.form_type.currency_code_type', $defaults);
220
221
        return $this;
222
    }
223
224
    /**
225
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings.
226
     *
227
     * @param array $config Configuration parameters.
228
     * @param ContainerBuilder $container Service container.
229
     * @return Extension $this Fluent interface.
230
     */
231
    protected function configureRateType(array $config, ContainerBuilder $container)
232
    {
233
        $container->setParameter('run_open_code.exchange_rate.form_type.rate_type', $config['form_types']['rate_type']);
234
235
        return $this;
236
    }
237
}
238