NginxPushStreamExtension   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 14.71 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 10
Bugs 2 Features 2
Metric Value
wmc 11
c 10
b 2
f 2
lcom 1
cbo 7
dl 10
loc 68
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 12 2
B buildConnection() 10 29 6
A buildFilter() 0 9 1
A getConnectionServiceId() 0 4 1
A getFilterServiceId() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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