Completed
Push — master ( f5aed6...aebd89 )
by Nikola
05:02
created

Extension::configureNotifications()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 3
eloc 4
nc 2
nop 2
crap 3.1406
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\Bundle\ExchangeRate\Security\AccessVoter;
14
use RunOpenCode\ExchangeRate\Configuration;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, RunOpenCode\Bundle\Excha...Injection\Configuration.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
20
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * Class Extension
25
 *
26
 * Bundle extension.
27
 *
28
 * @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection
29
 */
30
class Extension extends BaseExtension
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35 11
    public function getAlias()
36
    {
37 11
        return "runopencode_exchange_rate";
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 11
    public function getNamespace()
44
    {
45 11
        return 'http://www.runopencode.com/xsd-schema/exchange-rate-bundle';
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 2
    public function getXsdValidationBasePath()
52
    {
53 2
        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...
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 9
    public function load(array $config, ContainerBuilder $container)
60
    {
61
62 9
        $configuration = new TreeConfiguration();
63 9
        $config = $this->processConfiguration($configuration, $config);
64
65 9
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services'));
66 9
        $loader->load('repository.xml');
67 9
        $loader->load('command.xml');
68 9
        $loader->load('form_type.xml');
69 9
        $loader->load('manager.xml');
70 9
        $loader->load('processor.xml');
71 9
        $loader->load('security.xml');
72 9
        $loader->load('source.xml');
73 9
        $loader->load('validator.xml');
74 9
        $loader->load('notifications.xml');
75
76
        $this
77 9
            ->configureBaseCurrency($config, $container)
78 9
            ->configureRepository($config, $container)
79 9
            ->configureFileRepository($config, $container)
80 9
            ->configureDoctrineDbalRepository($config, $container)
81 9
            ->configureAccessVoter($config, $container)
82 9
            ->configureRates($config, $container)
83 9
            ->configureSources($config, $container)
84 9
            ->configureSourceType($config, $container)
85 9
            ->configureRateTypeType($config, $container)
86 9
            ->configureCurrencyCodeType($config, $container)
87 9
            ->configureForeignCurrencyCodeType($config, $container)
88 9
            ->configureRateType($config, $container)
89 9
            ->configureNotifications($config, $container)
90
        ;
91 9
    }
92
93
    /**
94
     * Configure base currency.
95
     *
96
     * @param array $config Configuration parameters.
97
     * @param ContainerBuilder $container Service container.
98
     *
99
     * @return Extension $this Fluent interface.
100
     * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException
101
     */
102 9
    protected function configureBaseCurrency(array $config, ContainerBuilder $container)
103
    {
104 9
        $baseCurrency = CurrencyCodeUtil::clean($config['base_currency']);
105 9
        $container->setParameter('runopencode.exchange_rate.base_currency', $baseCurrency);
106
107 9
        return $this;
108
    }
109
110
    /**
111
     * Configure rates.
112
     *
113
     * @param array $config Configuration parameters.
114
     * @param ContainerBuilder $container Service container.
115
     *
116
     * @return Extension $this Fluent interface.
117
     */
118 9
    protected function configureRates(array $config, ContainerBuilder $container)
119
    {
120 9
        foreach ($config['rates'] as $rate) {
121 9
            $definition = new Definition(Configuration::class);
122
123
            $arguments = [
124 9
                $rate['currency_code'],
125 9
                $rate['rate_type'],
126 9
                $rate['source'],
127 9
                (isset($rate['extra']) && $rate['extra']) ? $rate['extra'] : []
128
            ];
129
130
            $definition
131 9
                ->setArguments($arguments)
132 9
                ->setPublic(false)
133 9
                ->addTag('runopencode.exchange_rate.rate_configuration')
134
                ;
135
136 9
            $container->setDefinition(sprintf('runopencode.exchange_rate.rate_configuration.%s.%s.%s', $rate['currency_code'], $rate['rate_type'], $rate['source']), $definition);
137
        }
138
139 9
        return $this;
140
    }
141
142
143
    /**
144
     * Configure sources which does not have to be explicitly added to service container.
145
     *
146
     * @param array $config Configuration parameters.
147
     * @param ContainerBuilder $container Service container.
148
     *
149
     * @return Extension $this Fluent interface.
150
     */
151 9
    protected function configureSources(array $config, ContainerBuilder $container)
152
    {
153 9
        if (!empty($config['sources'])) {
154
155 1
            foreach ($config['sources'] as $name => $class) {
156
157 1
                $definition = new Definition($class);
158
                $definition
159 1
                    ->addTag('runopencode.exchange_rate.source', ['name' => $name]);
160
161 1
                $container->setDefinition(sprintf('runopencode.exchange_rate.source.%s', $name), $definition);
162
            }
163
        }
164
165 9
        return $this;
166
    }
167
168
    /**
169
     * Configure required processors.
170
     *
171
     * @param array $config Configuration parameters.
172
     * @param ContainerBuilder $container Service container.
173
     *
174
     * @return Extension $this Fluent interface.
175
     */
176 9
    protected function configureRepository(array $config, ContainerBuilder $container)
177
    {
178 9
        $container->setParameter('runopencode.exchange_rate.repository', $config['repository']);
179 9
        return $this;
180
    }
181
182
    /**
183
     * Configure file repository.
184
     *
185
     * @param array $config Configuration parameters.
186
     * @param ContainerBuilder $container Service container.
187
     *
188
     * @return Extension $this Fluent interface.
189
     */
190 9
    protected function configureFileRepository(array $config, ContainerBuilder $container)
191
    {
192 9
        $defintion = $container->getDefinition('runopencode.exchange_rate.repository.file_repository');
193
194
        $defintion
195 9
            ->setArguments([
196 9
                $config['file_repository']['path'],
197
            ]);
198
199 9
        return $this;
200
    }
201
202
    /**
203
     * Configure Doctrine Dbal repository.
204
     *
205
     * @param array $config Configuration parameters.
206
     * @param ContainerBuilder $container Service container.
207
     *
208
     * @return Extension $this Fluent interface.
209
     */
210 9
    protected function configureDoctrineDbalRepository(array $config, ContainerBuilder $container)
211
    {
212 9
        $defintion = $container->getDefinition('runopencode.exchange_rate.repository.doctrine_dbal_repository');
213
214
        $defintion
215 9
            ->setArguments([
216 9
                new Reference($config['doctrine_dbal_repository']['connection']),
217 9
                $config['doctrine_dbal_repository']['table_name'],
218
            ]);
219
220 9
        return $this;
221
    }
222
223
    /**
224
     * Configure access voter.
225
     *
226
     * @param array $config Configuration parameters.
227
     * @param ContainerBuilder $container Service container.
228
     *
229
     * @return Extension $this Fluent interface.
230
     */
231 9
    protected function configureAccessVoter(array $config, ContainerBuilder $container)
232
    {
233 9
        if ($config['security']['enabled']) {
234
235
            $container
236 8
                ->getDefinition('runopencode.exchange_rate.security.access_voter')
237 8
                ->setArguments([
238
                    [
239 8
                        AccessVoter::VIEW => $config['security'][AccessVoter::VIEW],
240 8
                        AccessVoter::CREATE => $config['security'][AccessVoter::CREATE],
241 8
                        AccessVoter::EDIT => $config['security'][AccessVoter::EDIT],
242 8
                        AccessVoter::DELETE => $config['security'][AccessVoter::DELETE],
243
                    ]
244
                ]);
245
246
        } else {
247 1
            $container->removeDefinition('runopencode.exchange_rate.security.access_voter');
248
        }
249
250 9
        return $this;
251
    }
252
253
    /**
254
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings.
255
     *
256
     * @param array $config Configuration parameters.
257
     * @param ContainerBuilder $container Service container.
258
     *
259
     * @return Extension $this Fluent interface.
260
     */
261 9 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...
262
    {
263 9
        $definition = $container->getDefinition('runopencode.exchange_rate.form_type.source_type');
264
265 9
        $arguments = $definition->getArguments();
266
267 9
        $arguments[1] = $config['form_types']['source_type'];
268
269 9
        $definition->setArguments($arguments);
270
271 9
        return $this;
272
    }
273
274
    /**
275
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings.
276
     *
277
     * @param array $config Configuration parameters.
278
     * @param ContainerBuilder $container Service container.
279
     *
280
     * @return Extension $this Fluent interface.
281
     */
282 9 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...
283
    {
284 9
        $definition = $container->getDefinition('runopencode.exchange_rate.form_type.rate_type_type');
285
286 9
        $arguments = $definition->getArguments();
287
288 9
        $arguments[1] = $config['form_types']['rate_type_type'];
289
290 9
        $definition->setArguments($arguments);
291
292 9
        return $this;
293
    }
294
295
    /**
296
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings.
297
     *
298
     * @param array $config Configuration parameters.
299
     * @param ContainerBuilder $container Service container.
300
     * @return Extension $this Fluent interface.
301
     */
302 9
    protected function configureCurrencyCodeType(array $config, ContainerBuilder $container)
303
    {
304 9
        $definition = $container->getDefinition('runopencode.exchange_rate.form_type.currency_code_type');
305
306 9
        $arguments = $definition->getArguments();
307
308 9
        $arguments[1] = CurrencyCodeUtil::clean($config['base_currency']);
309 9
        $arguments[2] = $config['form_types']['currency_code_type'];
310
311 9
        $definition->setArguments($arguments);
312
313 9
        return $this;
314
    }
315
316
    /**
317
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings.
318
     *
319
     * @param array $config Configuration parameters.
320
     * @param ContainerBuilder $container Service container.
321
     *
322
     * @return Extension $this Fluent interface.
323
     */
324 9 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...
325
    {
326
327 9
        $definition = $container->getDefinition('runopencode.exchange_rate.form_type.foreign_currency_code_type');
328
329 9
        $arguments = $definition->getArguments();
330
331 9
        $arguments[1] = $config['form_types']['foreign_currency_code_type'];
332
333 9
        $definition->setArguments($arguments);
334
335 9
        return $this;
336
    }
337
338
    /**
339
     * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings.
340
     *
341
     * @param array $config Configuration parameters.
342
     * @param ContainerBuilder $container Service container.
343
     *
344
     * @return Extension $this Fluent interface.
345
     */
346 9 View Code Duplication
    protected function configureRateType(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...
347
    {
348 9
        $definition = $container->getDefinition('runopencode.exchange_rate.form_type.rate_type');
349
350 9
        $arguments = $definition->getArguments();
351
352 9
        $arguments[1] = $config['form_types']['rate_type'];
353
354 9
        $definition->setArguments($arguments);
355
356 9
        return $this;
357
    }
358
359
    /**
360
     * Configure notifications
361
     *
362
     * @param array $config Configuration parameters.
363
     * @param ContainerBuilder $container Service container.
364
     *
365
     * @return Extension $this Fluent interface.
366
     */
367 9
    protected function configureNotifications(array $config, ContainerBuilder $container)
368
    {
369 9
        if (isset($config['notifications']['e_mail']['enabled']) && $config['notifications']['e_mail']['enabled']) {
370
            $container->setParameter('runopencode.exchange_rate.notifications.e_mail', $config['notifications']['e_mail']['recipients']);
371
        }
372
373 9
        return $this;
374
    }
375
}
376