FacileCrossbarHTTPPublisherExtension::load()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Facile\CrossbarHTTPPublisherBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Definition;
7
use Symfony\Component\DependencyInjection\Reference;
8
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9
use Facile\CrossbarHTTPPublisherBundle\Publisher\Publisher;
10
use Facile\CrossbarHTTPPublisherBundle\Publisher\Factory;
11
12
/**
13
 * Class FacileCrossbarHTTPPublisherExtension
14
 * @package Facile\CrossbarHTTPPublisherBundle\DependencyInjection
15
 * @see \Facile\CrossbarHTTPPublisherBundle\Tests\DependencyInjection\FacileCrossbarHTTPPublisherExtensionTest
16
 */
17
class FacileCrossbarHTTPPublisherExtension extends Extension
18
{
19
    /**
20
     * {@inheritDoc}
21 1
     */
22
    public function load(array $config, ContainerBuilder $container): void
23 1
    {
24 1
        $configuration = new Configuration();
25
        $config = $this->processConfiguration($configuration, $config);
26 1
27 1
        $this->loadConnections($container, $config);
28
    }
29
30
    /**
31
     * Registers publisher services in container
32
     * @param ContainerBuilder $container
33
     * @param array $config
34 1
     */
35
    private function loadConnections(ContainerBuilder $container, array $config): void
36 1
    {
37 1
        $factoryName = $this->addFactoryToContainer($container);
38 1
        $genericDefinition = new Definition(Publisher::class);
39
        $genericDefinition->setFactory([new Reference($factoryName), 'createPublisher']);
40 1
41 1
        foreach ($config['connections'] as $key => $connection) {
42 1
            $protocol = $connection['protocol'];
43 1
            $host = $connection['host'];
44 1
            $port = $connection['port'];
45 1
            $path = $connection['path'];
46 1
            $auth_key = $connection['auth_key'];
47 1
            $auth_secret = $connection['auth_secret'];
48 1
            $hostname = $connection['hostname'];
49
            $ignoreSsl = $connection['ssl_ignore'];
50 1
51
            if ($path[0] !== '/') {
52
                $path = '/' . $path;
53
            }
54 1
55 1
            $definition = clone $genericDefinition;
56 1
            $definition->setArguments([$protocol, $host, $port, $path, $auth_key, $auth_secret, $hostname, $ignoreSsl]);
57 1
            $definition->setPublic(true);
58
            $container->setDefinition(sprintf('facile.crossbar.publisher.%s', $key), $definition);
59 1
        }
60
    }
61
62
    /**
63
     * @param ContainerBuilder $container
64
     * @return string The factory service name inside the container
65 1
     */
66
    private function addFactoryToContainer(ContainerBuilder $container): string
67 1
    {
68 1
        $factoryDefinition = new Definition(Factory::class);
69 1
        $factoryDefinition->setPublic(false);
70 1
        $factoryName = 'facile.crossbar.publisher.factory';
71
        $container->setDefinition($factoryName, $factoryDefinition);
72 1
73
        return $factoryName;
74
    }
75
}
76