NexyPayboxDirectExtension::processClient()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Nexylan packages.
5
 *
6
 * (c) Nexylan SAS <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Nexy\PayboxDirect\Bridge\Symfony\DependencyInjection;
13
14
use Nexy\PayboxDirect\Enum\Activity;
15
use Nexy\PayboxDirect\Enum\Currency;
16
use Nexy\PayboxDirect\Enum\Version;
17
use Symfony\Component\Config\FileLocator;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21
22
/**
23
 * @author Sullivan Senechal <[email protected]>
24
 */
25
final class NexyPayboxDirectExtension extends Extension
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function load(array $configs, ContainerBuilder $container)
31
    {
32
        $configuration = new Configuration();
33
        $config = $this->processConfiguration($configuration, $configs);
34
35
        $this->processOptions($config, $container);
36
37
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
38
        $loader->load('sdk.xml');
39
        $loader->load('http_clients.xml');
40
41
        $this->processClient($config, $container);
42
    }
43
44
    /**
45
     * @param array            $config
46
     * @param ContainerBuilder $container
47
     */
48
    private function processOptions(array $config, ContainerBuilder $container)
49
    {
50
        // Start with client option
51
        $options = $config['options'];
52
53
        // Paybox version and default_currency special hack: Get the number.
54
        $options['paybox_version'] = constant(Version::class.'::'.strtoupper($config['paybox']['version']));
55
        unset($config['paybox']['version']);
56 View Code Duplication
        if (array_key_exists('default_currency', $config['paybox'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57
            $options['paybox_default_currency'] = constant(Currency::class.'::'.strtoupper($config['paybox']['default_currency']));
58
            unset($config['paybox']['default_currency']);
59
        }
60 View Code Duplication
        if (array_key_exists('default_activity', $config['paybox'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61
            $options['paybox_default_activity'] = constant(Activity::class.'::'.strtoupper($config['paybox']['default_activity']));
62
            unset($config['paybox']['default_activity']);
63
        }
64
65
        // Convert paybox option format
66
        foreach ($config['paybox'] as $key => $value) {
67
            $options['paybox_'.$key] = (string) $value;
68
        }
69
70
        $container->setParameter('nexy_paybox_direct.options', $options);
71
    }
72
73
    /**
74
     * @param array            $config
75
     * @param ContainerBuilder $container
76
     */
77
    private function processClient(array $config, ContainerBuilder $container)
78
    {
79
        if (null === $config['client']) {
80
            return;
81
        }
82
83
        $container->findDefinition('nexy_paybox_direct.sdk')->addArgument(
84
            $container->findDefinition('nexy_paybox_direct.http_client.'.$config['client'])
85
        );
86
    }
87
}
88