Completed
Push — master ( d8db80...b75b86 )
by Daniel
03:57
created

RpcServerPass::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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