Completed
Push — master ( bc87d6...419294 )
by Nikola
02:30
created

Extension::configureSourcesRegistry()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.0062

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 28
ccs 17
cts 18
cp 0.9444
rs 8.439
cc 6
eloc 12
nc 9
nop 2
crap 6.0062
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
    }
56
57
    /**
58
     * Configure exchange rate service.
59
     *
60
     * @param array $config
61
     * @param ContainerBuilder $container
62
     */
63 2
    protected function configureExchangeRateService(array $config, ContainerBuilder $container)
64
    {
65 2
        if ($container->hasDefinition('run_open_code.exchange_rate')) {
66
67 2
            $definition = $container->getDefinition('run_open_code.exchange_rate');
68
69 2
            $definition->setArguments(array(
70 2
                $config['base_currency'],
71 2
                new Reference($config['repository']),
72 2
                new Reference('run_open_code.exchange_rate.registry.sources'),
73 2
                new Reference('run_open_code.exchange_rate.registry.processors'),
74 2
                new Reference('run_open_code.exchange_rate.registry.rates')
75 1
            ));
76 1
        }
77 2
    }
78
79
    /**
80
     * Configure sources registry.
81
     *
82
     * @param array $config
83
     * @param ContainerBuilder $container
84
     */
85 2
    protected function configureSourcesRegistry(array $config, ContainerBuilder $container)
86
    {
87 2
        if ($container->hasDefinition('run_open_code.exchange_rate.registry.sources')) {
88
89 2
            $definition = $container->getDefinition('run_open_code.exchange_rate.registry.sources');
90 2
            $requiredSources = $this->getRequiredSources($config);
91
92
93 2
            foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.source') as $id => $tags) {
94
95 2
                foreach ($tags as $attributes) {
96
97 2
                    if (array_key_exists($attributes['alias'], $requiredSources)) {
98
99 2
                        $definition->addMethodCall('add', array(
100 2
                            new Reference($id)
101 1
                        ));
102
103 2
                        unset($requiredSources[$attributes['alias']]);
104 1
                    }
105 1
                }
106 1
            }
107
108 2
            if (count($requiredSources) > 0) {
109
                throw new InvalidConfigurationException(sprintf('Required source(s) "%s" does not exists.', implode(', ', $requiredSources)));
110
            }
111 1
        }
112 2
    }
113
114
    /**
115
     * Configure processors registry.
116
     *
117
     * @param array $config
118
     * @param ContainerBuilder $container
119
     */
120 2
    protected function configureProcessorsRegistry(array $config, ContainerBuilder $container)
121
    {
122 2
        if ($container->hasDefinition('run_open_code.exchange_rate.registry.processors')) {
123
124 2
            $definition = $container->getDefinition('run_open_code.exchange_rate.registry.processors');
125
126 2
            $processors = array();
127
128 2
            foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.processor') as $id => $tags) {
129
130 2
                if (!in_array($id, $config['processors'], true)) {
131 2
                    continue;
132
                }
133
134 2
                foreach ($tags as $attributes) {
135 2
                    $processors[$id] = (isset($attributes['priority'])) ? intval($attributes['priority']) : 0;
136 1
                }
137 1
            }
138
139 2
            asort($processors);
140
141 2
            foreach ($processors as $id => $processor) {
142 2
                $definition->addMethodCall('add', array(
143 2
                    new Reference($id)
144 1
                ));
145 1
            }
146 1
        }
147 2
    }
148
149
    /**
150
     * Configure rates.
151
     *
152
     * @param array $config
153
     * @param ContainerBuilder $container
154
     */
155 2
    protected function configureRatesRegistry(array $config, ContainerBuilder $container)
156
    {
157 2
        if ($container->hasDefinition('run_open_code.exchange_rate.registry.rates')) {
158
159 2
            $definition = $container->getDefinition('run_open_code.exchange_rate.registry.rates');
160
161 2
            $aliases = array();
162
163 2
            foreach ($config['rates'] as $rateConfiguration) {
164
165 2
                if ($rateConfiguration['alias'] === null) {
166 2
                    continue;
167
                }
168
169
                if (array_key_exists($rateConfiguration['alias'], $aliases)) {
170
                    throw new InvalidConfigurationException(sprintf('Rate with alias "%s" is already defined.', $rateConfiguration['alias']));
171
                }
172
173
                $aliases[$rateConfiguration['alias']] = $rateConfiguration;
174 1
            }
175
176 2
            $definition->setArguments(array(array_values($config['rates'])));
177 1
        }
178 2
    }
179
180
    /**
181
     * Configure file registry, if used.
182
     *
183
     * @param array $config
184
     * @param ContainerBuilder $container
185
     */
186 2
    protected function configureFileRepository(array $config, ContainerBuilder $container)
187
    {
188
        if (
189 2
            $config['repository'] === 'run_open_code.exchange_rate.repository.file_repository'
190 1
            &&
191 2
            $container->hasDefinition('run_open_code.exchange_rate.repository.file_repository')
192 1
        ) {
193
194 2
            if (!empty($config['file_repository']) && !empty($config['file_repository']['path'])) {
195 2
                $definition = $container->getDefinition('run_open_code.exchange_rate.repository.file_repository');
196 2
                $definition->setArguments(array(
197 2
                    $config['file_repository']['path']
198 1
                ));
199 1
            } else {
200 1
                throw new InvalidConfigurationException('You must configure location to the file where file repository will store exchange rates.');
201
            }
202
203 1
        } elseif ($config['repository'] === 'run_open_code.exchange_rate.repository.file_repository') {
204
            throw new InvalidConfigurationException('File repository is used to store exchange rates, but it is not available in container.');
205
        } else {
206
            $container->removeDefinition('run_open_code.exchange_rate.repository.file_repository');
207
        }
208 2
    }
209
210
    /**
211
     * Configure controller - view layer.
212
     *
213
     * @param array $config
214
     * @param ContainerBuilder $container
215
     */
216 2
    protected function configureController(array $config, ContainerBuilder $container)
217
    {
218 2
        if ($container->has('run_open_code.exchange_rate.controller')) {
219 2
            $definition = $container->getDefinition('run_open_code.exchange_rate.controller');
220 2
            $definition->setArguments(array(
221 2
                new Reference($config['repository']),
222 2
                $config['base_currency'],
223 2
                $config['view']
224 1
            ));
225 1
        }
226 2
    }
227
228
    /**
229
     * Configure debug command.
230
     *
231
     * @param array $config
232
     * @param ContainerBuilder $container
233
     */
234 2
    protected function configureDebugCommand(array $config, ContainerBuilder $container)
235
    {
236 2
        if ($container->has('run_open_code.exchange_rate.command.configuration_debug')) {
237 2
            $definition = $container->getDefinition('run_open_code.exchange_rate.command.configuration_debug');
238
239 2
            $arguments = $definition->getArguments();
240 2
            $arguments[3] = new Reference($config['repository']);
241
242 2
            $definition->setArguments($arguments);
243 1
        }
244 2
    }
245
246
    /**
247
     * Extract name of only required sources from configuration.
248
     *
249
     * @param array $config
250
     * @return array
251
     */
252 2
    protected function getRequiredSources(array $config)
253
    {
254 2
        $requiredSources = array();
255
256 2
        foreach ($config['rates'] as $rate) {
257 2
            $requiredSources[$rate['source']] = $rate['source'];
258 1
        }
259
260 2
        return $requiredSources;
261
    }
262
}
263