Completed
Push — master ( 61c032...c23418 )
by Alessandro
03:27
created

InfluxDbExtension::createConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Algatux\InfluxDbBundle\DependencyInjection;
4
5
use Algatux\InfluxDbBundle\Events\Listeners\InfluxDbEventListener;
6
use InfluxDB\Database;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Definition;
10
use Symfony\Component\DependencyInjection\Loader;
11
use Symfony\Component\DependencyInjection\Reference;
12
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
13
14
/**
15
 * This is the class that loads and manages your bundle configuration.
16
 *
17
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
18
 */
19
class InfluxDbExtension extends Extension
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24 2
    public function load(array $configs, ContainerBuilder $container)
25
    {
26 2
        $configuration = new Configuration();
27 2
        $config = $this->processConfiguration($configuration, $configs);
28
29 2
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
30 2
        $loader->load('factory.xml');
31
32
        // If the default connection if not defined, get the first one.
33 2
        $defaultConnection = isset($config['default_connection']) ? $config['default_connection'] : key($config['connections']);
34 2
        foreach ($config['connections'] as $connection => $connectionConfig) {
35 2
            $this->createConnection($container, $connection, $connectionConfig, 'http');
36 2
            $this->createConnection($container, $connection, $connectionConfig, 'udp');
37
38 2
            $this->createConnectionListener($container, $connection, $defaultConnection);
39
        }
40
41 2
        $container->setAlias(
42 2
            'algatux_influx_db.connection.http',
43 2
            'algatux_influx_db.connection.'.$defaultConnection.'.http'
44
        );
45 2
        $container->setAlias(
46 2
            'algatux_influx_db.connection.udp',
47 2
            'algatux_influx_db.connection.'.$defaultConnection.'.udp'
48
        );
49
50 2
        return $config;
51
    }
52
53
    /**
54
     * @param ContainerBuilder $container
55
     * @param string           $connection The connection name
56
     * @param array            $config     The connection configuration
57
     * @param string           $protocol   The connection protocol ('http' or 'udp')
58
     */
59 2
    private function createConnection(ContainerBuilder $container, $connection, array $config, $protocol)
60
    {
61
        // Create the connection based from the abstract one.
62 2
        $connectionDefinition = new Definition(Database::class, [
63 2
            $config['database'],
64 2
            $config['host'],
65 2
            $config['http_port'],
66 2
            $config['udp_port'],
67 2
            $config['username'],
68 2
            $config['password'],
69 2
            'udp' === $protocol,
70
        ]);
71 2
        $connectionDefinition->setFactory([new Reference('algatux_influx_db.connection_factory'), 'createConnection']);
72
73
        // E.g.: algatux_influx_db.connection.default.http
74 2
        $container->setDefinition('algatux_influx_db.connection.'.$connection.'.'.$protocol, $connectionDefinition);
75 2
    }
76
77 2
    private function createConnectionListener(ContainerBuilder $container, $connection, $defaultConnection)
78
    {
79 2
        $listenerDefinition = new Definition(InfluxDbEventListener::class, [
80 2
            $connection,
81 2
            $connection === $defaultConnection,
82 2
            new Reference('algatux_influx_db.connection.'.$connection.'.http'),
83 2
            new Reference('algatux_influx_db.connection.'.$connection.'.udp'),
84
        ]);
85 2
        $listenerDefinition->addTag('kernel.event_listener', [
86 2
            'event' => 'influxdb.points_collected',
87
            'method' => 'onPointsCollected',
88
        ]);
89 2
        $listenerDefinition->addTag('kernel.event_listener', [
90 2
            'event' => 'kernel.terminate',
91
            'method' => 'onKernelTerminate',
92
        ]);
93
94 2
        $container->setDefinition('algatux_influx_db.event_listener.'.$connection, $listenerDefinition);
95 2
    }
96
}
97