RpcServerPass::buildDefinition()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
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
        $serviceDefinition = $container->getDefinition($this->serviceName);
67
        $queueBagDefinition = new Definition(
68
            RpcQueueBag::class,
69
            [
70
                'queueName' => $this->queueName,
71
                'basicQos' => $this->basicQos,
72
                'durable' => $this->durable,
73
                'autoDelete' => $this->autoDelete,
74
                'arguments' => $this->arguments
75
            ]
76
        );
77
        $queueCallbackDefinition = new Definition(
78
            RpcQueueCallback::class,
79
            [
80
                'queueService' => $serviceDefinition
81
            ]
82
        );
83
        $definition = new Definition(
84
            Queue::class,
85
            [
86
                'connectionManager' => $connection,
87
                'queueBag' => $queueBagDefinition,
88
                'connectionName' => $this->connectionName,
89
                'callback' => $queueCallbackDefinition
90
            ]
91
        );
92
93
        return $definition;
94
    }
95
}