Completed
Pull Request — master (#37)
by Tobias
72:36 queued 66:40
created

HttplugExtension::configureClients()   C

Complexity

Conditions 8
Paths 54

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 4
Metric Value
c 5
b 1
f 4
dl 0
loc 37
rs 5.3846
cc 8
eloc 24
nc 54
nop 2
1
<?php
2
3
namespace Http\HttplugBundle\DependencyInjection;
4
5
use Http\Client\Plugin\PluginClient;
6
use Http\HttplugBundle\ClientFactory\DummyClient;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
11
use Symfony\Component\DependencyInjection\Reference;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
14
/**
15
 * @author David Buchmann <[email protected]>
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
class HttplugExtension extends Extension
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function load(array $configs, ContainerBuilder $container)
24
    {
25
        $configuration = $this->getConfiguration($configs, $container);
26
        $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...
27
28
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
29
30
        $loader->load('services.xml');
31
        $loader->load('plugins.xml');
32
        $loader->load('discovery.xml');
33
34
        $enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug');
35
        if ($enabled) {
36
            $loader->load('data-collector.xml');
37
            $config['_inject_collector_plugin'] = true;
38
39
            if (!empty($config['toolbar']['formatter'])) {
40
                $container->getDefinition('httplug.collector.message_journal')
41
                    ->replaceArgument(0, new Reference($config['toolbar']['formatter']));
42
            }
43
        }
44
45
        foreach ($config['classes'] as $service => $class) {
46
            if (!empty($class)) {
47
                $container->removeDefinition(sprintf('httplug.%s.default', $service));
48
                $container->register(sprintf('httplug.%s.default', $service), $class);
49
            }
50
        }
51
52
        foreach ($config['main_alias'] as $type => $id) {
53
            $container->setAlias(sprintf('httplug.%s', $type), $id);
54
        }
55
        $this->configurePlugins($container, $config['plugins']);
56
        $this->configureClients($container, $config);
57
    }
58
59
    /**
60
     * Configure client services.
61
     *
62
     * @param ContainerBuilder $container
63
     * @param array            $config
64
     */
65
    private function configureClients(ContainerBuilder $container, array $config)
66
    {
67
        $first = isset($config['clients']['default']) ? 'default' : null;
68
        foreach ($config['clients'] as $name => $arguments) {
69
            if ($first === null) {
70
                $first = $name;
71
            }
72
73
            if (isset($config['_inject_collector_plugin'])) {
74
                array_unshift($arguments['plugins'], 'httplug.collector.history_plugin');
75
            }
76
77
            $def = $container->register('httplug.client.'.$name, DummyClient::class);
78
79
            if (empty($arguments['plugins'])) {
80
                $def->setFactory([new Reference($arguments['factory']), 'createClient'])
81
                    ->addArgument($arguments['config']);
82
            } else {
83
                $def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
84
                    ->addArgument(array_map(function ($id) {
85
                        return new Reference($id);
86
                    }, $arguments['plugins']))
87
                    ->addArgument(new Reference($arguments['factory']))
88
                    ->addArgument($arguments['config']);
89
            }
90
        }
91
92
        // Alias the first client to httplug.client.default
93
        if ($first !== null) {
94
            $container->setAlias('httplug.client.default', 'httplug.client.'.$first);
95
        } elseif (isset($config['_inject_collector_plugin'])) {
96
            // No client was configured. Make sure to inject history plugin to the auto discovery client.
97
            $container->register('httplug.client', PluginClient::class)
98
                ->addArgument(new Reference('httplug.client.default'))
99
                ->addArgument([new Reference('httplug.collector.history_plugin')]);
100
        }
101
    }
102
103
    /**
104
     * @param ContainerBuilder $container
105
     * @param array            $config
106
     */
107
    private function configurePlugins(ContainerBuilder $container, array $config)
108
    {
109
        foreach ($config as $name => $pluginConfig) {
110
            $pluginId = 'httplug.plugin.'.$name;
111
            if ($pluginConfig['enabled']) {
112
                $def = $container->getDefinition($pluginId);
113
                $this->configurePluginByName($name, $def, $pluginConfig);
114
            } else {
115
                $container->removeDefinition($pluginId);
116
            }
117
        }
118
    }
119
120
    /**
121
     * @param string     $name
122
     * @param Definition $definition
123
     * @param array      $config
124
     */
125
    private function configurePluginByName($name, Definition $definition, array $config)
126
    {
127
        switch ($name) {
128
            case 'authentication':
129
                $definition->replaceArgument(0, new Reference($config['authentication']));
130
                break;
131
            case 'cache':
132
                $definition
133
                    ->replaceArgument(0, new Reference($config['cache_pool']))
134
                    ->replaceArgument(1, new Reference($config['stream_factory']))
135
                    ->replaceArgument(2, $config['config']);
136
                break;
137
            case 'cookie':
138
                $definition->replaceArgument(0, new Reference($config['cookie_jar']));
139
                break;
140
            case 'decoder':
141
                $definition->addArgument($config['use_content_encoding']);
142
                break;
143
            case 'history':
144
                $definition->replaceArgument(0, new Reference($config['journal']));
145
                break;
146
            case 'logger':
147
                $definition->replaceArgument(0, new Reference($config['logger']));
148
                if (!empty($config['formatter'])) {
149
                    $definition->replaceArgument(1, new Reference($config['formatter']));
150
                }
151
                break;
152
            case 'redirect':
153
                $definition
154
                    ->addArgument($config['preserve_header'])
155
                    ->addArgument($config['use_default_for_multiple']);
156
                break;
157
            case 'retry':
158
                $definition->addArgument($config['retry']);
159
                break;
160
            case 'stopwatch':
161
                $definition->replaceArgument(0, new Reference($config['stopwatch']));
162
                break;
163
        }
164
    }
165
}
166