Completed
Pull Request — master (#33)
by Márk
39:13 queued 29:54
created

HttplugExtension::load()   C

Complexity

Conditions 8
Paths 72

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 2
Metric Value
c 5
b 1
f 2
dl 0
loc 34
rs 5.3846
cc 8
eloc 21
nc 72
nop 2
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\Loader\XmlFileLoader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
12
/**
13
 * @author David Buchmann <[email protected]>
14
 */
15
class HttplugExtension extends Extension
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function load(array $configs, ContainerBuilder $container)
21
    {
22
        $configuration = $this->getConfiguration($configs, $container);
23
        $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...
24
25
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26
27
        $loader->load('services.xml');
28
        $loader->load('plugins.xml');
29
        $loader->load('discovery.xml');
30
31
        $enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug');
32
        if ($enabled) {
33
            $loader->load('data-collector.xml');
34
            $config['_inject_collector_plugin'] = true;
35
36
            if (!empty($config['toolbar']['formatter'])) {
37
                $container->getDefinition('httplug.collector.message_journal')
38
                    ->replaceArgument(0, new Reference($config['toolbar']['formatter']));
39
            }
40
        }
41
42
        foreach ($config['classes'] as $service => $class) {
43
            if (!empty($class)) {
44
                $container->removeDefinition(sprintf('httplug.%s.default', $service));
45
                $container->register(sprintf('httplug.%s.default', $service), $class);
46
            }
47
        }
48
49
        foreach ($config['main_alias'] as $type => $id) {
50
            $container->setAlias(sprintf('httplug.%s', $type), $id);
51
        }
52
        $this->configureClients($container, $config);
53
    }
54
55
    /**
56
     * Configure client services.
57
     *
58
     * @param ContainerBuilder $container
59
     * @param array            $config
60
     */
61
    protected function configureClients(ContainerBuilder $container, array $config)
62
    {
63
        $first = isset($config['clients']['default']) ? 'default' : null;
64
        foreach ($config['clients'] as $name => $arguments) {
65
            if ($first === null) {
66
                $first = $name;
67
            }
68
69
            if (isset($config['_inject_collector_plugin'])) {
70
                array_unshift($arguments['plugins'], 'httplug.collector.history_plugin');
71
            }
72
73
            $def = $container->register('httplug.client.'.$name, DummyClient::class);
74
75
            if (empty($arguments['plugins'])) {
76
                $def->setFactory([new Reference($arguments['factory']), 'createClient'])
77
                    ->addArgument($arguments['config']);
78
            } else {
79
                $def->setFactory('Http\HttplugBundle\ClientFactory\PluginClientFactory::createPluginClient')
80
                    ->addArgument(array_map(function ($id) {
81
                        return new Reference($id);
82
                    }, $arguments['plugins']))
83
                    ->addArgument(new Reference($arguments['factory']))
84
                    ->addArgument($arguments['config']);
85
            }
86
        }
87
88
        // Alias the first client to httplug.client.default
89
        if ($first !== null) {
90
            $container->setAlias('httplug.client.default', 'httplug.client.'.$first);
91
        }
92
    }
93
}
94