Completed
Pull Request — master (#97)
by Tobias
09:36
created

ProfilerExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 7
c 2
b 1
f 0
lcom 0
cbo 5
dl 0
loc 68
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 22 3
A registerDebugPlugin() 0 10 1
A mergeDebugPluginArguments() 0 12 3
1
<?php
2
3
namespace Http\HttplugBundle\DependencyInjection;
4
5
use Http\HttplugBundle\Collector\DebugPlugin;
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
11
/**
12
 * This class is responsible for loading profiler tools and the toolbar. This extension should generally not be used
13
 * in production.
14
 *
15
 * @author David Buchmann <[email protected]>
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
class ProfilerExtension
19
{
20
    /**
21
     * @param array            $config           Processed configuration
22
     * @param ContainerBuilder $container
23
     * @param array            $clientServiceIds ids of all clients
24
     */
25
    public function load(array $config, ContainerBuilder $container, array $clientServiceIds)
26
    {
27
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
28
        $loader->load('data-collector.xml');
29
30
        if (!empty($config['toolbar']['formatter'])) {
31
            // Add custom formatter
32
            $container->getDefinition('httplug.collector.debug_collector')
33
                ->replaceArgument(0, new Reference($config['toolbar']['formatter']));
34
        }
35
36
        $container->getDefinition('httplug.formatter.full_http_message')
37
            ->addArgument($config['toolbar']['captured_body_length']);
38
39
        foreach ($clientServiceIds as $clientId) {
40
            $pluginClientDefinition = $container->getDefinition($clientId);
41
            $serviceIdDebugPlugin = $this->registerDebugPlugin($container, $clientId);
42
43
            $argument = $this->mergeDebugPluginArguments([new Reference($serviceIdDebugPlugin)], $pluginClientDefinition->getArgument(3));
44
            $pluginClientDefinition->replaceArgument(3, $argument);
45
        }
46
    }
47
48
    /**
49
     * Create a new plugin service for this client.
50
     *
51
     * @param ContainerBuilder $container
52
     * @param string           $name
53
     *
54
     * @return string
55
     */
56
    private function registerDebugPlugin(ContainerBuilder $container, $name)
57
    {
58
        $serviceIdDebugPlugin = $name.'.debug_plugin';
59
        $container->register($serviceIdDebugPlugin, DebugPlugin::class)
60
            ->addArgument(new Reference('httplug.collector.debug_collector'))
61
            ->addArgument(substr($name, strrpos($name, '.') + 1))
62
            ->setPublic(false);
63
64
        return $serviceIdDebugPlugin;
65
    }
66
67
    /**
68
     * @param array $newArgument
69
     * @param array $existing
70
     *
71
     * @return array
72
     */
73
    private function mergeDebugPluginArguments(array $newArgument, array $existing = [])
74
    {
75
        if (empty($existing)) {
76
            $mergedArgument = ['debug_plugins' => $newArgument];
77
        } elseif (empty($existing['debug_plugins'])) {
78
            $mergedArgument['debug_plugins'] = $newArgument;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$mergedArgument was never initialized. Although not strictly required by PHP, it is generally a good practice to add $mergedArgument = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
79
        } else {
80
            $mergedArgument['debug_plugins'] = array_merge($existing['debug_plugins'], $newArgument);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$mergedArgument was never initialized. Although not strictly required by PHP, it is generally a good practice to add $mergedArgument = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
81
        }
82
83
        return $mergedArgument;
84
    }
85
}
86