Completed
Push — master ( 949da1...594dbe )
by Olivier
08:49 queued 06:01
created

OlaRabbitMqAdminToolkitExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Ola\RabbitMqAdminToolkitBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
10
use Symfony\Component\DependencyInjection\Loader;
11
12
/**
13
 * This is the class that loads and manages your bundle configuration
14
 *
15
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
16
 */
17
class OlaRabbitMqAdminToolkitExtension extends Extension
18
{
19
    const PARAMETER_TEMPLATE = 'ola_rabbit_mq_admin_toolkit.%s';
20
    const CONNECTION_SERVICE_TEMPLATE = 'ola_rabbit_mq_admin_toolkit.connection.%s';
21
    const VHOST_MANAGER_SERVICE_TEMPLATE = 'ola_rabbit_mq_admin_toolkit.configuration.%s';
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 1
    public function load(array $configs, ContainerBuilder $container)
27
    {
28 1
        $configuration = new Configuration();
29 1
        $config = $this->processConfiguration($configuration, $configs);
30
31 1
        $container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'default_vhost'), $config['default_vhost']);
32
33 1
        $this->loadConnections($config['connections'], $container);
34 1
        $this->loadVhostManagers($config['vhosts'], $container, $config['delete_allowed']);
35
36 1
        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
37 1
        $loader->load('services.xml');
38 1
    }
39
40
    /**
41
     * @param array $connections
42
     * @param ContainerBuilder $container
43
     */
44 1
    private function loadConnections(array $connections, ContainerBuilder $container)
45
    {
46 1
        foreach ($connections as $name => $uri) {
47
48 1
            $parsedUri = parse_url($uri);
49
50 1
            $definition = new Definition('RabbitMq\ManagementApi\Client', array(
51 1
                null,
52 1
                sprintf("%s://%s:%s", $parsedUri['scheme'], $parsedUri['host'], $parsedUri['port']),
53 1
                $parsedUri['user'],
54 1
                $parsedUri['pass']
55 1
            ));
56
57 1
            $container->setDefinition(sprintf(self::CONNECTION_SERVICE_TEMPLATE, $name), $definition);
58 1
        }
59 1
    }
60
61
    /**
62
     * @param array $vhosts
63
     * @param ContainerBuilder $container
64
     * @param $deleteAllowed
65
     */
66 1
    private function loadVhostManagers(array $vhosts, ContainerBuilder $container, $deleteAllowed)
67
    {
68 1
        foreach ($vhosts as $name => $vhost) {
69 1
            $definition = new Definition('Ola\RabbitMqAdminToolkitBundle\VhostConfiguration', array(
70 1
                new Reference(sprintf(self::CONNECTION_SERVICE_TEMPLATE, $vhost['connection'])),
71 1
                !empty($vhost['name']) ? $vhost['name'] : $name,
72 1
                $vhost,
73
                $deleteAllowed
74 1
            ));
75 1
            $container->setDefinition(sprintf(self::VHOST_MANAGER_SERVICE_TEMPLATE, $name), $definition);
76 1
        }
77 1
    }
78
}
79