Completed
Push — master ( e9401f...2161bf )
by Daniel
03:27
created

CmobiRabbitmqExtension   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 97
Duplicated Lines 29.9 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 29
loc 97
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 16 1
A loadConnections() 0 23 3
A loadRpcServers() 15 15 2
A loadWorkers() 0 12 2
A loadSubscribers() 14 14 2
A registerLogger() 0 5 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 Cmobi\RabbitmqBundle\DependencyInjection;
4
5
use Cmobi\RabbitmqBundle\DependencyInjection\Compiler\LogDispatcherPass;
6
use Cmobi\RabbitmqBundle\DependencyInjection\Compiler\RpcServerPass;
7
use Cmobi\RabbitmqBundle\DependencyInjection\Compiler\SubscriberPass;
8
use Cmobi\RabbitmqBundle\DependencyInjection\Compiler\WorkerPass;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
14
15
class CmobiRabbitmqExtension extends Extension
16
{
17
    public function load(array $configs, ContainerBuilder $container)
18
    {
19
        $fileLocator = new FileLocator(__DIR__ . '/../Resources/config');
20
        $loader = new YamlFileLoader($container, $fileLocator);
21
        $loader->load('rabbitmq.yml');
22
23
        $configuration = $this->getConfiguration($configs, $container);
24
        $config = $this->processConfiguration($configuration, $configs);
0 ignored issues
show
Documentation introduced by
$configuration is of type object|null, but the function expects a object<Symfony\Component...ConfigurationInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
25
        $this->registerLogger($container, $configs[0]['log_path']);
26
        $this->loadConnections($container, $configs[0]);
27
        $this->loadRpcServers($container, $config);
28
        $this->loadWorkers($container, $config);
29
30
        /* Compile and lock container */
31
        $container->compile();
32
    }
33
34
    protected function loadConnections(ContainerBuilder $container, array $configs)
35
    {
36
        $factories = [];
37
38
        foreach ($configs['connections'] as $name => $connection) {
39
            $connectionClass = '%cmobi_rabbitmq.connection.class%';
40
41
            if ($connection['lazy']) {
42
                $connectionClass = '%cmobi_rabbitmq.lazy.connection.class%';
43
            }
44
            $definition = new Definition(
45
                '%cmobi_rabbitmq.connection.factory.class%',
46
                [
47
                    $connectionClass,
48
                    $connection,
49
                ]
50
            );
51
            $factoryName = sprintf('cmobi_rabbitmq.connection.factory.%s', $name);
52
            $container->setDefinition($factoryName, $definition);
53
            $factories[$name] = $factoryName;
54
        }
55
        $container->setParameter('cmobi_rabbitmq.connection.factories', $factories);
56
    }
57
58 View Code Duplication
    public function loadRpcServers(ContainerBuilder $container, array $configs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
    {
60
        foreach ($configs['rpc_servers'] as $server) {
61
62
            $container->addCompilerPass(new RpcServerPass(
63
                $server['queue']['name'],
64
                $server['queue']['connection'],
65
                $server['service'],
66
                $server['queue']['basic_qos'],
67
                $server['queue']['durable'],
68
                $server['queue']['auto_delete'],
69
                $server['queue']['arguments']
70
            ));
71
        }
72
    }
73
74
    public function loadWorkers(ContainerBuilder $container, array $configs)
75
    {
76
        foreach ($configs['workers'] as $worker) {
77
            $container->addCompilerPass(new WorkerPass(
78
                $worker['queue']['name'],
79
                $worker['queue']['connection'],
80
                $worker['service'],
81
                $worker['queue']['basic_qos'],
82
                $worker['queue']['arguments']
83
            ));
84
        }
85
    }
86
87 View Code Duplication
    public function loadSubscribers(ContainerBuilder $container, array $configs)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
    {
89
        foreach ($configs['subscribers'] as $subscriber) {
90
            $container->addCompilerPass(new SubscriberPass(
91
                $subscriber['queue']['exchange'],
92
                $subscriber['queue']['exchange_type'],
93
                $subscriber['queue']['name'],
94
                $subscriber['queue']['connection'],
95
                $subscriber['service'],
96
                $subscriber['queue']['basic_qos'],
97
                $subscriber['queue']['arguments']
98
            ));
99
        }
100
    }
101
102
    /**
103
     * @param ContainerBuilder $container
104
     * @param $path
105
     */
106
    public function registerLogger(ContainerBuilder $container, $path)
107
    {
108
        $logDispatcherPass = new LogDispatcherPass($path);
109
        $container->addCompilerPass($logDispatcherPass);
110
    }
111
}
112