AMFWebServicesClientExtension   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 11.54 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 12
loc 104
ccs 0
cts 60
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 10 1
A registerSoapConfiguration() 4 15 4
B registerRestConfiguration() 8 25 5
B remapParametersNamespaces() 0 17 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AMF\WebServicesClientBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8
use Symfony\Component\DependencyInjection\Loader;
9
use Symfony\Component\Config\Loader\LoaderInterface;
10
11
/**
12
 * This is the class that loads and manages your bundle configuration
13
 *
14
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
15
 */
16
class AMFWebServicesClientExtension extends Extension
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    public function load(array $configs, ContainerBuilder $container)
22
    {
23
        $configuration = new Configuration();
24
        $config = $this->processConfiguration($configuration, $configs);
25
26
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27
28
        $this->registerRestConfiguration($loader, $config, $container);
29
        $this->registerSoapConfiguration($loader, $config, $container);
30
    }
31
32
    /**
33
     * Loads Soap config.
34
     *
35
     * @param LoaderInterface  $loader    The loader of file.
36
     * @param array            $config    The gloabl config of this bundle.
37
     * @param ContainerBuilder $container The container for dependency injection.
38
     *
39
     * @return void
40
     */
41
    protected function registerSoapConfiguration(LoaderInterface $loader, array $config, ContainerBuilder $container)
42
    {
43
        if (!empty($config['soap'])) {
44
            $loader->load('soap.yml');
45
            $container->setParameter('amf_web_services_client.soap.endpoints', $config['soap']['endpoints']);
46
            foreach ($config['soap']['endpoints'] as $key => $value) {
47 View Code Duplication
                if ($value['wsse']['enabled'] === true) {
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...
48
                    $this->remapParametersNamespaces($value, $container, array('wsse' => "amf_web_services_client.soap.$key.wsse.%s"));
49
                    unset($value['wsse']);
50
                }
51
52
                $this->remapParametersNamespaces($value, $container, array('' => "amf_web_services_client.soap.$key.%s"));
53
            }
54
        }
55
    }
56
57
    /**
58
     * Loads ReST config.
59
     *
60
     * @param LoaderInterface  $loader    The loader of file.
61
     * @param array            $config    The gloabl config of this bundle.
62
     * @param ContainerBuilder $container The container for dependency injection.
63
     *
64
     * @return void
65
     */
66
    protected function registerRestConfiguration(LoaderInterface $loader, array $config, ContainerBuilder $container)
67
    {
68
        if (!empty($config['rest'])) {
69
            $loader->load('listeners.yml');
70
            $loader->load('rest.yml');
71
            $container->setParameter('amf_web_services_client.rest.endpoints', $config['rest']['endpoints']);
72
            foreach ($config['rest']['endpoints'] as $key => $value) {
73 View Code Duplication
                if ($value['wsse']['enabled'] === true) {
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...
74
                    $this->remapParametersNamespaces($value, $container, array('wsse' => "amf_web_services_client.rest.$key.wsse.%s"));
75
                    unset($value['wsse']);
76
                }
77
78 View Code Duplication
                if ($value['url']['enabled'] === true) {
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...
79
                    $this->remapParametersNamespaces($value, $container, array('url' => "amf_web_services_client.rest.$key.url.%s"));
80
                    unset($value['url']);
81
                }
82
83
                $this->remapParametersNamespaces($value, $container, array('' => "amf_web_services_client.rest.$key.%s"));
84
            }
85
86
            $container->setParameter('amf_web_services_client.rest.decoders', $config['rest']['decoders']);
87
            $container->setParameter('amf_web_services_client.rest.encoders', $config['rest']['encoders']);
88
            $container->setParameter('amf_web_services_client.rest.curl_options', $config['rest']['curl_options']);
89
        }
90
    }
91
92
93
    /**
94
     * Maps parameters to add them in container.
95
     *
96
     * @param array            $config     The gloabl config of this bundle.
97
     * @param ContainerBuilder $container  The container for dependency injection.
98
     * @param array            $namespaces Config namespaces to add as parameters in the container.
99
     *
100
     * @return void
101
     */
102
    protected function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces)
103
    {
104
        foreach ($namespaces as $namespace => $map) {
105
            if (isset($namespace) && strlen($namespace) > 0) {
106
                if (!array_key_exists($namespace, $config)) {
107
                    continue;
108
                }
109
                $namespaceConfig = $config[$namespace];
110
            } else {
111
                $namespaceConfig = $config;
112
            }
113
114
            foreach ($namespaceConfig as $name => $value) {
115
                $container->setParameter(sprintf($map, $name), $value);
116
            }
117
        }
118
    }
119
}
120