NginxPushStreamExtension::getConnectionServiceId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Alawar\NginxPushStreamBundle\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 NginxPushStreamExtension extends Extension
18
{
19
    /**
20
     * {@inheritDoc}
21
     */
22
    public function load(array $configs, ContainerBuilder $container)
23
    {
24
        $configuration = new Configuration();
25
        $config = $this->processConfiguration($configuration, $configs);
26
27
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
28
        $loader->load('services.yml');
29
30
        foreach ($config['connections'] as $name => $connection) {
31
            $this->buildConnection($container, $name, $connection);
32
        }
33
    }
34
35
    protected function buildConnection(ContainerBuilder $container, $name, $connection)
36
    {
37
        $serviceId = $this->getConnectionServiceId($name);
38
        $definition = $container->setDefinition(
39
            $serviceId,
40
            new Definition('%nginx_push_stream.connection.class%')
41
        );
42
        $definition->setArguments(array($connection['pub_url'], $connection['sub_urls']));
43
44
        // add id generator reference
45 View Code Duplication
        if ($connection['id_generator'] === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
46
            $definition->addMethodCall('setIdGenerator', array(new Reference('nginx_push_stream.id_generator')));
47
        } elseif (is_string($connection['id_generator'])) {
48
            $definition->addMethodCall('setIdGenerator', array(new Reference($connection['id_generator'])));
49
        }
50
51
        // add sender reference
52 View Code Duplication
        if ($connection['sender'] === true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            $definition->addMethodCall('setSender', array(new Reference('nginx_push_stream.sender')));
54
        } elseif (is_string($connection['sender'])) {
55
            $definition->addMethodCall('setSender', array(new Reference($connection['sender'])));
56
        }
57
58
        // add filter references
59
        foreach ($connection['filters'] as $id => $filter) {
60
            $filterServiceId = $this->buildFilter($container, $id, $filter);
61
            $definition->addMethodCall('addFilter', array(new Reference($filterServiceId)));
62
        }
63
    }
64
65
    protected function buildFilter(ContainerBuilder $container, $id, $filter)
66
    {
67
        $serviceId = $this->getFilterServiceId($id);
68
        $container->setDefinition(
69
            $serviceId,
70
            new Definition(sprintf('%%nginx_push_stream.filter.%s.class%%', $filter['class']), array($filter))
71
        );
72
        return $serviceId;
73
    }
74
75
    protected function getConnectionServiceId($id)
76
    {
77
        return sprintf('nginx_push_stream.%s_connection', $id);
78
    }
79
80
    protected function getFilterServiceId($id)
81
    {
82
        return sprintf('nginx_push_stream.%s_filter', $id);
83
    }
84
}
85