Completed
Push — master ( 6c3128...7946ff )
by Alessandro
12s
created

MongoDbBundleExtension   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 11.8 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 19
loc 161
ccs 85
cts 85
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A mustCollectData() 0 6 3
A defineLoggers() 0 7 1
A defineDataCollector() 0 16 1
A defineClientRegistry() 0 13 1
A defineConnectionFactory() 7 7 1
A defineConnections() 0 15 2
A defineEventManager() 0 7 1
A defineDataCollectorListeners() 12 12 1
A attachDataCollectionListenerToEventManager() 0 18 1
A load() 0 21 2
A attachTwigExtesion() 0 8 1

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 declare(strict_types = 1);
2
3
namespace Facile\MongoDbBundle\DependencyInjection;
4
5
use Facile\MongoDbBundle\DataCollector\MongoDbDataCollector;
6
use Facile\MongoDbBundle\Event\ConnectionEvent;
7
use Facile\MongoDbBundle\Event\Listener\DataCollectorListener;
8
use Facile\MongoDbBundle\Event\QueryEvent;
9
use Facile\MongoDbBundle\Services\ClientRegistry;
10
use Facile\MongoDbBundle\Services\ConnectionFactory;
11
use Facile\MongoDbBundle\Services\Loggers\MongoQueryLogger;
12
use Facile\MongoDbBundle\Twig\FacileMongoDbBundleExtension;
13
use MongoDB\Database;
14
use Symfony\Bundle\WebProfilerBundle\WebProfilerBundle;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
use Symfony\Component\EventDispatcher\EventDispatcher;
19
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
20
21
/**
22
 * Class MongoDbBundleExtension.
23
 * @internal
24
 */
25
final class MongoDbBundleExtension extends Extension
26
{
27
    /** @var ContainerBuilder */
28
    private $containerBuilder;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 5
    public function load(array $configs, ContainerBuilder $container)
34
    {
35 5
        $this->containerBuilder = $container;
36 5
        $configuration = new Configuration();
37 5
        $config = $this->processConfiguration($configuration, $configs);
38
39 5
        $this->defineEventManager();
40 5
        $this->defineClientRegistry($config['clients'], $container->getParameter("kernel.environment"));
41 5
        $this->defineConnectionFactory();
42 5
        $this->defineConnections($config['connections']);
43
44 5
        if ($this->mustCollectData($config)) {
45 2
            $this->defineLoggers();
46 2
            $this->defineDataCollectorListeners();
47 2
            $this->attachDataCollectionListenerToEventManager();
48 2
            $this->defineDataCollector();
49 2
            $this->attachTwigExtesion();
50
        }
51
52 5
        return $config;
53
    }
54
55
    /**
56
     * @param array $config
57
     *
58
     * @return bool
59
     */
60 5
    private function mustCollectData(array $config): bool
61
    {
62 5
        return in_array($this->containerBuilder->getParameter("kernel.environment"), ["dev"])
63 5
            && class_exists(WebProfilerBundle::class)
64 5
            && $config['data_collection'] === true;
65
    }
66
67 2
    private function defineLoggers()
68
    {
69 2
        $loggerDefinition = new Definition(MongoQueryLogger::class);
70 2
        $loggerDefinition->setPublic(false);
71
72 2
        $this->containerBuilder->setDefinition('facile_mongo_db.logger', $loggerDefinition);
73 2
    }
74
75 2
    private function defineDataCollector()
76
    {
77 2
        $dataCollectorDefinition = new Definition(MongoDbDataCollector::class);
78 2
        $dataCollectorDefinition->addMethodCall('setLogger', [new Reference('facile_mongo_db.logger')]);
79 2
        $dataCollectorDefinition->addTag(
80 2
            'data_collector',
81
            [
82 2
                'template' => 'FacileMongoDbBundle:Collector:mongo.html.twig',
83
                'id' => 'mongodb',
84
                'priority' => 250,
85
            ]
86
        );
87 2
        $dataCollectorDefinition->setPublic(false);
88
89 2
        $this->containerBuilder->setDefinition('facile_mongo_db.data_collector', $dataCollectorDefinition);
90 2
    }
91
92
    /**
93
     * @param array  $clientsConfig
94
     * @param string $environment
95
     */
96 5
    private function defineClientRegistry(array $clientsConfig, string $environment)
97
    {
98 5
        $clientRegistryDefinition = new Definition(
99 5
            ClientRegistry::class,
100
            [
101 5
                new Reference('facile_mongo_db.event_dispatcher'),
102 5
                $environment,
103
            ]
104
        );
105 5
        $clientRegistryDefinition->addMethodCall('addClientsConfigurations', [$clientsConfig]);
106
107 5
        $this->containerBuilder->setDefinition('mongo.client_registry', $clientRegistryDefinition);
108 5
    }
109
110 5 View Code Duplication
    private function defineConnectionFactory()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
111
    {
112 5
        $factoryDefinition = new Definition(ConnectionFactory::class, [new Reference('mongo.client_registry')]);
113 5
        $factoryDefinition->setPublic(false);
114
115 5
        $this->containerBuilder->setDefinition('mongo.connection_factory', $factoryDefinition);
116 5
    }
117
118
    /**
119
     * @param array $connections
120
     */
121 5
    private function defineConnections(array $connections)
122
    {
123 5
        foreach ($connections as $name => $conf) {
124 5
            $connectionDefinition = new Definition(
125 5
                Database::class,
126
                [
127 5
                    $conf['client_name'],
128 5
                    $conf['database_name'],
129
                ]
130
            );
131 5
            $connectionDefinition->setFactory([new Reference('mongo.connection_factory'), 'createConnection']);
132 5
            $this->containerBuilder->setDefinition('mongo.connection.'.$name, $connectionDefinition);
133
        }
134 5
        $this->containerBuilder->setAlias('mongo.connection', 'mongo.connection.'.array_keys($connections)[0]);
135 5
    }
136
137 5
    private function defineEventManager()
138
    {
139 5
        $eventManagerDefinition = new Definition(EventDispatcher::class);
140 5
        $eventManagerDefinition->setPublic(false);
141
142 5
        $this->containerBuilder->setDefinition('facile_mongo_db.event_dispatcher', $eventManagerDefinition);
143 5
    }
144
145 2 View Code Duplication
    private function defineDataCollectorListeners()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
146
    {
147 2
        $dataCollectorListenerDefinition = new Definition(
148 2
            DataCollectorListener::class,
149
            [
150 2
                new Reference('facile_mongo_db.logger')
151
            ]
152
        );
153 2
        $dataCollectorListenerDefinition->setPublic(false);
154
155 2
        $this->containerBuilder->setDefinition('facile_mongo_db.data_collector.listener', $dataCollectorListenerDefinition);
156 2
    }
157
158 2
    private function attachDataCollectionListenerToEventManager()
159
    {
160 2
        $eventManagerDefinition = $this->containerBuilder->getDefinition('facile_mongo_db.event_dispatcher');
161 2
        $eventManagerDefinition->addMethodCall(
162 2
            'addListener',
163
            [
164 2
                ConnectionEvent::CLIENT_CREATED,
165 2
                [new Reference('facile_mongo_db.data_collector.listener'), 'onConnectionClientCreated']
166
            ]
167
        );
168 2
        $eventManagerDefinition->addMethodCall(
169 2
            'addListener',
170
            [
171 2
                QueryEvent::QUERY_EXECUTED,
172 2
                [new Reference('facile_mongo_db.data_collector.listener'), 'onQueryExecuted']
173
            ]
174
        );
175 2
    }
176
177 2
    private function attachTwigExtesion()
178
    {
179 2
        $extesion = new Definition(FacileMongoDbBundleExtension::class);
180 2
        $extesion->setPublic(false);
181 2
        $extesion->addTag('twig.extension');
182
183 2
        $this->containerBuilder->setDefinition('facile_mongo_db.twig_extesion', $extesion);
184 2
    }
185
}
186