WorkerPass::buildDefinition()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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