Completed
Push — master ( e9401f...2161bf )
by Daniel
03:27
created

WorkerPass   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 78
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A process() 0 9 2
B buildDefinition() 0 32 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
        $logger = $container->getDefinition('cmobi_rabbitmq.logger');
61
        $serviceDefinition = $container->getDefinition($this->serviceName);
62
        $queueBagDefinition = new Definition(
63
            WorkerQueueBag::class,
64
            [
65
                'queueName' => $this->queueName,
66
                'basicQos' => $this->basicQos,
67
                'arguments' => $this->arguments
68
            ]
69
        );
70
        $queueCallbackDefinition = new Definition(
71
            WorkerQueueCallback::class,
72
            [
73
                'queueService' => $serviceDefinition
74
            ]
75
        );
76
        $definition = new Definition(
77
            Queue::class,
78
            [
79
                'connectionManager' => $connection,
80
                'queueBag' => $queueBagDefinition,
81
                'logger' => $logger,
82
                'connectionName' => $this->connectionName,
83
                'callback' => $queueCallbackDefinition
84
            ]
85
        );
86
87
        return $definition;
88
    }
89
}