Completed
Pull Request — master (#37)
by Tobias
15:28
created

HttplugExtension::configurePluginByName()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 39
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 39
rs 4.8196
cc 10
eloc 36
nc 10
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Http\HttplugBundle\DependencyInjection;
4
5
use Http\HttplugBundle\ClientFactory\DummyClient;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
10
use Symfony\Component\DependencyInjection\Reference;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
13
/**
14
 * @author David Buchmann <[email protected]>
15
 */
16
class HttplugExtension extends Extension
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function load(array $configs, ContainerBuilder $container)
22
    {
23
        $configuration = $this->getConfiguration($configs, $container);
24
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Documentation introduced by
$configuration is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
26
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27
28
        $loader->load('services.xml');
29
        $loader->load('plugins.xml');
30
        $loader->load('discovery.xml');
31
32
        $enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug');
33
        if ($enabled) {
34
            $loader->load('data-collector.xml');
35
            $config['_inject_collector_plugin'] = true;
36
37
            if (!empty($config['toolbar']['formatter'])) {
38
                $container->getDefinition('httplug.collector.message_journal')
39
                    ->replaceArgument(0, new Reference($config['toolbar']['formatter']));
40
            }
41
        }
42
43
        foreach ($config['classes'] as $service => $class) {
44
            if (!empty($class)) {
45
                $container->removeDefinition(sprintf('httplug.%s.default', $service));
46
                $container->register(sprintf('httplug.%s.default', $service), $class);
47
            }
48
        }
49
50
        foreach ($config['main_alias'] as $type => $id) {
51
            $container->setAlias(sprintf('httplug.%s', $type), $id);
52
        }
53
        $this->configurePlugins($container, $config['plugins']);
54
        $this->configureClients($container, $config);
55
    }
56
57
    /**
58
     * Configure client services.
59
     *
60
     * @param ContainerBuilder $container
61
     * @param array            $config
62
     */
63
    protected function configureClients(ContainerBuilder $container, array $config)
64
    {
65
        $first = isset($config['clients']['default']) ? 'default' : null;
66
        foreach ($config['clients'] as $name => $arguments) {
67
            if ($first === null) {
68
                $first = $name;
69
            }
70
71
            if (isset($config['_inject_collector_plugin'])) {
72
                array_unshift($arguments['plugins'], 'httplug.collector.history_plugin');
73
            }
74
75
            $def = $container->register('httplug.client.'.$name, DummyClient::class);
76
77
            if (empty($arguments['plugins'])) {
78
                $def->setFactory([new Reference($arguments['factory']), 'createClient'])
79
                    ->addArgument($arguments['config']);
80
            } else {
81
                $def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
82
                    ->addArgument(array_map(function ($id) {
83
                        return new Reference($id);
84
                    }, $arguments['plugins']))
85
                    ->addArgument(new Reference($arguments['factory']))
86
                    ->addArgument($arguments['config']);
87
            }
88
        }
89
90
        // Alias the first client to httplug.client.default
91
        if ($first !== null) {
92
            $container->setAlias('httplug.client.default', 'httplug.client.'.$first);
93
        }
94
    }
95
96
    /**
97
     * @param ContainerBuilder $container
98
     * @param array            $config
99
     */
100
    protected function configurePlugins(ContainerBuilder $container, array $config)
101
    {
102
        foreach ($config as $name => $pluginConfig) {
103
            $pluginId = 'httplug.plugin.'.$name;
104
            if ($pluginConfig['enabled']) {
105
                $def = $container->getDefinition($pluginId);
106
                $this->configurePluginByName($name, $def, $pluginConfig);
107
            } else {
108
                $container->removeDefinition($pluginId);
109
            }
110
        }
111
    }
112
113
    /**
114
     * @param string     $name
115
     * @param Definition $definition
116
     * @param array      $config
117
     */
118
    private function configurePluginByName($name, Definition $definition, array $config)
119
    {
120
        switch ($name) {
121
            case 'authentication':
122
                $definition->replaceArgument(0, new Reference($config['authentication']));
123
                break;
124
            case 'cache':
125
                $definition
126
                    ->replaceArgument(0, new Reference($config['cache_pool']))
127
                    ->replaceArgument(1, new Reference($config['stream_factory']))
128
                    ->replaceArgument(2, $config['config']);
129
                break;
130
            case 'cookie':
131
                $definition->replaceArgument(0, new Reference($config['cookie_jar']));
132
                break;
133
            case 'decoder':
134
                $definition->addArgument($config['use_content_encoding']);
135
                break;
136
            case 'history':
137
                $definition->replaceArgument(0, new Reference($config['journal']));
138
                break;
139
            case 'logger':
140
                $definition
141
                    ->replaceArgument(0, new Reference($config['logger']))
142
                    ->replaceArgument(1, new Reference($config['formatter']));
143
                break;
144
            case 'redirect':
145
                $definition
146
                    ->addArgument($config['preserve_header'])
147
                    ->addArgument($config['use_default_for_multiple']);
148
                break;
149
            case 'retry':
150
                $definition->addArgument($config['retry']);
151
                break;
152
            case 'stopwatch':
153
                $definition->replaceArgument(0, new Reference($config['stopwatch']));
154
                break;
155
        }
156
    }
157
}
158