Test Failed
Pull Request — master (#39)
by Aleksandr
05:36
created

ExtraContextLoggerCompilerPass::process()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
c 0
b 0
f 0
nc 9
nop 1
dl 0
loc 26
rs 9.3554
1
<?php
2
3
namespace OldSound\RabbitMqBundle\DependencyInjection\Compiler;
4
5
use OldSound\RabbitMqBundle\Logger\ExtraContextLogger;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
class ExtraContextLoggerCompilerPass implements CompilerPassInterface
12
{
13
    private $tags = [
0 ignored issues
show
introduced by
The private property $tags is not used, and could be removed.
Loading history...
14
        'old_sound_rabbit_mq.producer',
15
        'old_sound_rabbit_mq.consumer',
16
    ];
17
18
    public function process(ContainerBuilder $container)
19
    {
20
        $taggedProducers = $container->findTaggedServiceIds('old_sound_rabbit_mq.producer');
21
        $taggedConsumers = $container->findTaggedServiceIds('old_sound_rabbit_mq.consumer');
22
23
        foreach ($taggedProducers as $id => $tags) {
24
            $producerName = $tags[0]['producer'];
25
            $originLogger = $id . '.logger';
26
27
            if ($container->hasDefinition($originLogger)) {
28
                $consumerDef = $container->getDefinition($id);
29
                $consumerDef->removeMethodCall('setLogger');
30
                $loggerDef = new Definition(ExtraContextLogger::class, [new Reference($originLogger), ['producer' => $producerName]]);
31
                $consumerDef->addMethodCall('setLogger', [$loggerDef]);
32
            }
33
        }
34
35
        foreach ($taggedConsumers as $id => $tags) {
36
            $consumerName = $tags[0]['consumer'];
37
            $originLogger = $id . '.logger';
38
39
            if ($container->hasDefinition($originLogger)) {
40
                $consumerDef = $container->getDefinition($id);
41
                $consumerDef->removeMethodCall('setLogger');
42
                $loggerDef = new Definition(ExtraContextLogger::class, [new Reference($originLogger), ['consumer' => $consumerName]]);
43
                $consumerDef->addMethodCall('setLogger', [$loggerDef]);
44
            }
45
        }
46
    }
47
}
48