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) |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.