Completed
Pull Request — master (#30)
by Matthew
23:29 queued 08:10
created

RedisCompilerPass   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 20.83 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.28%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 20
loc 96
ccs 57
cts 58
cp 0.9828
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B process() 0 26 5
A processSncRedis() 0 11 2
A setRedis() 0 11 1
A processPredisDsn() 10 10 1
A processPhpRedis() 0 20 3
A processPredisConnectionParameters() 10 10 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 Dtc\QueueBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class RedisCompilerPass implements CompilerPassInterface
11
{
12 4
    public function process(ContainerBuilder $container)
13
    {
14 4
        if ($container->hasParameter('dtc_queue.redis.snc_redis.type')) {
15 1
            $this->processSncRedis($container);
16
17 1
            return;
18
        }
19
20 3
        if ($container->hasParameter('dtc_queue.redis.predis.dsn')) {
21 1
            $this->processPredisDsn($container);
22
23 1
            return;
24
        }
25
26 3
        if ($container->hasParameter('dtc_queue.redis.predis.connection_parameters')) {
27 1
            $this->processPredisConnectionParameters($container);
28
29 1
            return;
30
        }
31
32 2
        if ($container->hasParameter('dtc_queue.redis.phpredis.host')) {
33 1
            $this->processPhpRedis($container);
34
35 1
            return;
36
        }
37 1
    }
38
39 1
    protected function processSncRedis(ContainerBuilder $container)
40
    {
41 1
        $type = $container->getParameter('dtc_queue.redis.snc_redis.type');
42 1
        $alias = $container->getParameter('dtc_queue.redis.snc_redis.alias');
43 1
        $class = 'PhpRedis';
44 1
        if ('predis' == $type) {
45 1
            $class = 'Predis';
46
        }
47
48 1
        $this->setRedis($container, $class, 'snc_redis'.$alias, $type);
49 1
    }
50
51 3
    protected function setRedis(ContainerBuilder $container, $class, $reference, $type)
52
    {
53 3
        $definition = new Definition(
54 3
            'Dtc\\QueueBundle\\Redis\\'.$class,
55 3
            [new Reference($reference)]
56
        );
57 3
        $container->setDefinition('dtc_queue.'.$type, $definition);
58
59 3
        $definition = $container->getDefinition('dtc_queue.job_manager.redis');
60 3
        $definition->addMethodCall('setRedis', [new Reference('dtc_queue.'.$type)]);
61 3
    }
62
63 1 View Code Duplication
    protected function processPredisDsn(ContainerBuilder $container)
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...
64
    {
65 1
        $definition = new Definition(
66 1
            'Predis\\Client',
67 1
            [$container->getParameter('dtc_queue.redis.predis.dsn')]
68
        );
69 1
        $container->setDefinition('dtc_queue.predis.client', $definition);
70
71 1
        $this->setRedis($container, 'Predis', 'dtc_queue.predis.client', 'predis');
72 1
    }
73
74 1
    protected function processPhpRedis(ContainerBuilder $container)
75
    {
76 1
        $definition = new Definition(
77 1
            'Redis'
78
        );
79
80 1
        $arguments = [$container->getParameter('dtc_queue.redis.phpredis.host'),
81 1
            $container->getParameter('dtc_queue.redis.phpredis.port'),
82 1
            $container->getParameter('dtc_queue.redis.phpredis.timeout'),
83
            null,
84 1
            $container->getParameter('dtc_queue.redis.phpredis.retry_interval'),
85 1
            $container->getParameter('dtc_queue.redis.phpredis.read_timeout'), ];
86 1
        $definition->addMethodCall('connect', $arguments);
87 1
        if ($container->hasParameter('dtc_queue.redis.phpredis.auth') && null !== ($auth = $container->getParameter('dtc_queue.redis.phpredis.auth'))) {
88
            $definition->addMethodCall('auth', $auth);
89
        }
90 1
        $container->setDefinition('dtc_queue.phpredis.connection', $definition);
91
92 1
        $this->setRedis($container, 'PhpRedis', 'dtc_queue.phpredis.connection', 'phpredis');
93 1
    }
94
95 1 View Code Duplication
    protected function processPredisConnectionParameters(ContainerBuilder $container)
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...
96
    {
97 1
        $definition = new Definition(
98 1
            'Predis\\Client',
99 1
            $container->getParameter('dtc_queue.redis.predis.connection_parameters')
100
        );
101 1
        $container->setDefinition('dtc_queue.predis.client', $definition);
102
103 1
        $this->setRedis($container, 'Predis', 'dtc_queue.predis.client', 'predis');
104 1
    }
105
}
106