Completed
Branch query_logging_to_events (6a211d)
by Alessandro
02:15
created

MongoDbBundleExtension::defineListeners()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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