1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Queue\Queue; |
6
|
|
|
use Cmobi\RabbitmqBundle\Transport\Subscriber\ExchangeType; |
7
|
|
|
use Cmobi\RabbitmqBundle\Transport\Subscriber\SubscriberQueueBag; |
8
|
|
|
use Cmobi\RabbitmqBundle\Transport\Subscriber\SubscriberQueueCallback; |
9
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
|
13
|
|
View Code Duplication |
class SubscriberPass implements CompilerPassInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
private $exchangeName; |
16
|
|
|
private $exchangeType; |
17
|
|
|
private $queueName; |
18
|
|
|
private $connectionName; |
19
|
|
|
private $serviceName; |
20
|
|
|
private $basicQos; |
21
|
|
|
private $arguments; |
22
|
|
|
private $jobs; |
23
|
|
|
|
24
|
|
|
public function __construct( |
25
|
|
|
$exchangeName, |
26
|
|
|
$type = ExchangeType::FANOUT, |
27
|
|
|
$queueName = null, |
28
|
|
|
$connectionName, |
29
|
|
|
$serviceName, |
30
|
|
|
$basicQos, |
31
|
|
|
array $arguments, |
32
|
|
|
$jobs = 1 |
33
|
|
|
) |
34
|
|
|
{ |
35
|
|
|
$this->exchangeName = $exchangeName; |
36
|
|
|
$this->exchangeType = $type; |
37
|
|
|
$this->queueName = $queueName; |
38
|
|
|
$this->connectionName = $connectionName; |
39
|
|
|
$this->serviceName = $serviceName; |
40
|
|
|
$this->basicQos = $basicQos; |
41
|
|
|
$this->arguments = $arguments; |
42
|
|
|
$this->jobs = $jobs; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ContainerBuilder $container |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
|
|
public function process(ContainerBuilder $container) |
50
|
|
|
{ |
51
|
|
|
for ($job = 0; $job < $this->jobs; $job++) { |
52
|
|
|
$definition = $this->buildDefinition($container); |
53
|
|
|
$definition->addTag('cmobi.subscriber'); |
54
|
|
|
$jobName = sprintf('cmobi_rabbitmq.subscriber.%s_%s', $this->queueName, $job); |
55
|
|
|
$container->setDefinition($jobName, $definition); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ContainerBuilder $container |
61
|
|
|
* @return Definition |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
|
|
protected function buildDefinition(ContainerBuilder $container) |
65
|
|
|
{ |
66
|
|
|
$connection = $container->getDefinition('cmobi_rabbitmq.connection.manager'); |
67
|
|
|
$logger = $container->getDefinition('cmobi_rabbitmq.logger'); |
68
|
|
|
$serviceDefinition = $container->getDefinition($this->serviceName); |
69
|
|
|
$queueBagDefinition = new Definition( |
70
|
|
|
SubscriberQueueBag::class, |
71
|
|
|
[ |
72
|
|
|
'exchangeName' => $this->exchangeName, |
73
|
|
|
'type' => $this->exchangeType, |
74
|
|
|
'queueName' => $this->queueName, |
75
|
|
|
'basicQos' => $this->basicQos, |
76
|
|
|
'arguments' => $this->arguments |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
$queueCallbackDefinition = new Definition( |
80
|
|
|
SubscriberQueueCallback::class, |
81
|
|
|
[ |
82
|
|
|
'queueService' => $serviceDefinition |
83
|
|
|
] |
84
|
|
|
); |
85
|
|
|
$definition = new Definition( |
86
|
|
|
Queue::class, |
87
|
|
|
[ |
88
|
|
|
'connectionManager' => $connection, |
89
|
|
|
'queueBag' => $queueBagDefinition, |
90
|
|
|
'logger' => $logger, |
91
|
|
|
'connectionName' => $this->connectionName, |
92
|
|
|
'callback' => $queueCallbackDefinition |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
return $definition; |
97
|
|
|
} |
98
|
|
|
} |
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.