Completed
Push — master ( aa0624...f17611 )
by Nikola
03:22
created

Extension::configureCurrencyCodeType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
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) 2017 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 1
    public function getAlias()
32
    {
33 1
        return "run_open_code_exchange_rate";
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function getNamespace()
40
    {
41 1
        return 'http://www.runopencode.com/xsd-schema/exchange-rate-bundle';
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getXsdValidationBasePath()
48
    {
49
        return __DIR__.'/../Resources/config/schema';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return __DIR__ . '/../Resources/config/schema'; (string) is incompatible with the return type of the parent method Symfony\Component\Depend...etXsdValidationBasePath of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function load(array $config, ContainerBuilder $container)
56
    {
57
58
        $configuration = new TreeConfiguration();
59
        $config = $this->processConfiguration($configuration, $config);
60
61
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services'));
62
        $loader->load('repository.xml');
63
        $loader->load('command.xml');
64
        $loader->load('controller.xml');
65
        $loader->load('form_type.xml');
66
        $loader->load('manager.xml');
67
        $loader->load('processor.xml');
68
        $loader->load('source.xml');
69
        $loader->load('validator.xml');
70
71
        $this
72
            ->configureBaseCurrency($config, $container)
73
            ->configureRepository($config, $container)
74
            ->configureFileRepository($config, $container)
75
            ->configureController($config, $container)
76
            ->configureRates($config, $container)
77
            ->configureSimpleSources($config, $container)
78
            ->configureSourceType($config, $container)
79
            ->configureRateTypeType($config, $container)
80
            ->configureCurrencyCodeType($config, $container)
81
            ->configureForeignCurrencyCodeType($config, $container)
82
            ->configureRateType($config, $container)
83
            ->configureNotifications($config, $container)
84
        ;
85
    }
86
87
    /**
88
     * Configure base currency.
89
     *
90
     * @param array $config Configuration parameters.
91
     * @param ContainerBuilder $container Service container.
92
     * @return Extension $this Fluent interface.
93
     */
94
    protected function configureBaseCurrency(array $config, ContainerBuilder $container)
95
    {
96
        $baseCurrency = CurrencyCodeUtil::clean($config['base_currency']);
97
        $container->setParameter('run_open_code.exchange_rate.base_currency', $baseCurrency);
98
99
        return $this;
100
    }
101
102
    /**
103
     * Configure rates.
104
     *
105
     * @param array $config Configuration parameters.
106
     * @param ContainerBuilder $container Service container.
107
     * @return Extension $this Fluent interface.
108
     */
109
    protected function configureRates(array $config, ContainerBuilder $container)
110
    {
111
        if (count($config['rates']) === 0) {
112
            throw new \RuntimeException('You have to configure which rates you would like to use in your project.');
113
        }
114
115
        $container->setParameter('run_open_code.exchange_rate.registered_rates', $config['rates']);
116
        return $this;
117
    }
118
119
    /**
120
     * Configure required processors.
121
     *
122
     * @param array $config Configuration parameters.
123
     * @param ContainerBuilder $container Service container.
124
     * @return Extension $this Fluent interface.
125
     */
126
    protected function configureRepository(array $config, ContainerBuilder $container)
127
    {
128
        $container->setParameter('run_open_code.exchange_rate.repository', $config['repository']);
129
        return $this;
130
    }
131
132
    /**
133
     * Configure file repository, if used.
134
     *
135
     * @param array $config Configuration parameters.
136
     * @param ContainerBuilder $container Service container.
137
     * @return Extension $this Fluent interface.
138
     */
139
    protected function configureFileRepository(array $config, ContainerBuilder $container)
140
    {
141
        $container->setParameter('run_open_code.exchange_rate.repository.file_repository.path', $config['file_repository']['path']);
142
        return $this;
143
    }
144
145
    /**
146
     * Configure controller.
147
     *
148
     * @param array $config Configuration parameters.
149
     * @param ContainerBuilder $container Service container.
150
     * @return Extension $this Fluent interface.
151
     */
152
    protected function configureController(array $config, ContainerBuilder $container)
153
    {
154
        $container->setParameter(
155
            'run_open_code.exchange_rate.controller.view_configuration',
156
            $config['view']
157
        );
158
159
        $container->setParameter(
160
            'run_open_code.exchange_rate.controller.access_roles_configuration',
161
            $config['access_roles']
162
        );
163
164
        return $this;
165
    }
166
167
    /**
168
     * Configure simple sources which does not have to be explicitly added to service container.
169
     *
170
     * @param array $config Configuration parameters.
171
     * @param ContainerBuilder $container Service container.
172
     * @return Extension $this Fluent interface.
173
     */
174
    protected function configureSimpleSources(array $config, ContainerBuilder $container)
175
    {
176
        if (!empty($config['sources']) && count($config['sources']) > 0) {
177
            $container->setParameter('run_open_code.exchange_rate.source.registered_simple_sources', $config['sources']);
178
        }
179
180
        return $this;
181
    }
182
183
    /**
184
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings.
185
     *
186
     * @param array $config Configuration parameters.
187
     * @param ContainerBuilder $container Service container.
188
     * @return Extension $this Fluent interface.
189
     */
190 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...
191
    {
192
        $defaults = array_merge($config['form_types']['source_type'], array('choices' => array()));
193
194
        foreach ($config['rates'] as $rate) {
195
            $defaults['choices'][sprintf('exchange_rate.source.%s', $rate['source'])] = $rate['source'];
196
        }
197
198
        $container->setParameter('run_open_code.exchange_rate.form_type.source_type', $defaults);
199
200
        return $this;
201
    }
202
203
    /**
204
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings.
205
     *
206
     * @param array $config Configuration parameters.
207
     * @param ContainerBuilder $container Service container.
208
     * @return Extension $this Fluent interface.
209
     */
210
    protected function configureRateTypeType(array $config, ContainerBuilder $container)
211
    {
212
        $defaults = array_merge($config['form_types']['rate_type_type'], array('choices' => array()));
213
214
        foreach ($config['rates'] as $rate) {
215
            $defaults['choices'][$rate['rate_type']] = sprintf('exchange_rate.rate_type.%s.%s', $rate['source'], $rate['rate_type']);
216
        }
217
218
        $defaults['choices'] = array_flip($defaults['choices']);
219
220
        $container->setParameter('run_open_code.exchange_rate.form_type.rate_type_type', $defaults);
221
222
        return $this;
223
    }
224
225
    /**
226
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" 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 configureCurrencyCodeType(array $config, ContainerBuilder $container)
233
    {
234
        $defaults = array_merge($config['form_types']['currency_code_type'], array('choices' => array()));
235
236
        foreach ($config['rates'] as $rate) {
237
            $defaults['choices'][$rate['currency_code']] = $rate['currency_code'];
238
        }
239
240
        asort($defaults['choices']);
241
242
        $defaults['choices'] = array_merge(array($config['base_currency'] => $config['base_currency']), $defaults['choices']);
243
244
        $container->setParameter('run_open_code.exchange_rate.form_type.currency_code_type', $defaults);
245
246
        return $this;
247
    }
248
249
    /**
250
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings.
251
     *
252
     * @param array $config Configuration parameters.
253
     * @param ContainerBuilder $container Service container.
254
     * @return Extension $this Fluent interface.
255
     */
256 View Code Duplication
    protected function configureForeignCurrencyCodeType(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...
257
    {
258
        $defaults = array_merge($config['form_types']['currency_code_type'], array('choices' => array()));
259
260
        foreach ($config['rates'] as $rate) {
261
            $defaults['choices'][$rate['currency_code']] = $rate['currency_code'];
262
        }
263
264
        asort($defaults['choices']);
265
266
        $container->setParameter('run_open_code.exchange_rate.form_type.foreign_currency_code_type', $defaults);
267
268
        return $this;
269
    }
270
271
    /**
272
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings.
273
     *
274
     * @param array $config Configuration parameters.
275
     * @param ContainerBuilder $container Service container.
276
     * @return Extension $this Fluent interface.
277
     */
278
    protected function configureRateType(array $config, ContainerBuilder $container)
279
    {
280
        $container->setParameter('run_open_code.exchange_rate.form_type.rate_type', $config['form_types']['rate_type']);
281
282
        return $this;
283
    }
284
285
    /**
286
     * Configure internal notifications.
287
     *
288
     * @param array $config Configuration parameters.
289
     * @param ContainerBuilder $container Service container.
290
     * @return Extension $this Fluent interface.
291
     */
292
    protected function configureNotifications(array $config, ContainerBuilder $container)
293
    {
294
        if (!empty($config['notifications']['fetch']) && !empty($config['notifications']['fetch']['enabled'])) {
295
            $container->setParameter('run_open_code.exchange_rate.notifications.fetch', $config['notifications']['fetch']);
296
        }
297
298
        return $this;
299
    }
300
}
301