Completed
Pull Request — master (#30)
by Matthew
07:24
created

RedisCompilerPass::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
crap 20
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
    public function process(ContainerBuilder $container)
13
    {
14
        if ($container->hasParameter('dtc_queue.redis.snc_redis.type')) {
15
            $this->processSncRedis($container);
16
17
            return;
18
        }
19
        if ($container->hasParameter('dtc_queue.redis.predis.dsn')) {
20
            $this->processPredisDsn($container);
21
22
            return;
23
        }
24
        if ($container->hasParameter('dtc_queue.redis.predis.connection_parameters')) {
25
            $this->processPredisConnectionParameters($container);
26
27
            return;
28
        }
29
    }
30
31
    protected function processSncRedis(ContainerBuilder $container)
32
    {
33
        $type = $container->getParameter('dtc_queue.redis.snc_redis.type');
34
        $alias = $container->getParameter('dtc_queue.redis.snc_redis.alias');
35
        $class = 'PhpRedis';
36
        if ('predis' == $type) {
37
            $class = 'Predis';
38
        }
39
40
        $this->setRedis($container, $class, 'snc_redis'.$alias, $type);
41
    }
42
43
    protected function setRedis(ContainerBuilder $container, $class, $reference, $type)
44
    {
45
        $definition = new Definition(
46
            'Dtc\\QueueBundle\\Redis\\'.$class,
47
            [new Reference($reference)]
48
        );
49
        $container->setDefinition('dtc_queue.'.$type, $definition);
50
51
        $definition = $container->getDefinition('dtc_queue.job_manager.redis');
52
        $definition->addMethodCall('setRedis', [new Reference('dtc_queue.'.$type)]);
53
    }
54
55 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...
56
    {
57
        $definition = new Definition(
58
            'Predis\\Client',
59
            [$container->getParameter('dtc_queue.redis.predis.dsn')]
60
        );
61
        $container->setDefinition('dtc_queue.predis.client', $definition);
62
63
        $this->setRedis($container, 'Predis', 'dtc_queue.predis.client', 'predis');
64
    }
65
66
    protected function processPhpRedis(ContainerBuilder $container)
67
    {
68
        $definition = new Definition(
69
            'Redis'
70
        );
71
72
        $arguments = [$container->getParameter('dtc_queue.redis.phpredis.host'),
73
            $container->getParameter('dtc_queue.redis.phpredis.port'),
74
            $container->getParameter('dtc_queue.redis.phpredis.timeout'),
75
            null,
76
            $container->getParameter('dtc_queue.redis.phpredis.retry_interval'),
77
            $container->getParameter('dtc_queue.redis.phpredis.read_timeout'), ];
78
        $definition->addMethodCall('connect', $arguments);
79
        if ($container->hasParameter('dtc_queue.redis.phpredis.auth') && null !== ($auth = $container->getParameter('dtc_queue.redis.phpredis.auth'))) {
80
            $definition->addMethodCall('auth', $auth);
81
        }
82
        $container->setDefinition('dtc_queue.phpredis.connection', $definition);
83
84
        $this->setRedis($container, 'PhpRedis', 'dtc_queue.phpredis.connection', 'phpredis');
85
    }
86
87 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...
88
    {
89
        $definition = new Definition(
90
            'Predis\\Client',
91
            $container->getParameter('dtc_queue.redis.predis.connection_parameters')
92
        );
93
        $container->setDefinition('dtc_queue.predis.client', $definition);
94
95
        $this->setRedis($container, 'Predis', 'dtc_queue.predis.client', 'predis');
96
    }
97
}
98