RegisterConsumers::replaceArgumentWithReference()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 5
cts 8
cp 0.625
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 3
crap 3.4746
1
<?php
2
3
namespace Happyr\Mq2phpBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
/**
10
 * Register the message bus argument to our consumer.
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class RegisterConsumers implements CompilerPassInterface
15
{
16
    /**
17
     * @param ContainerBuilder $container
18
     */
19 1
    public function process(ContainerBuilder $container)
20
    {
21 1
        $this->replaceArgumentWithReference($container, 'happyr.mq2php.extendable_command_envelope_consumer', 'simple_bus.asynchronous.command_bus');
22 1
        $this->replaceArgumentWithReference($container, 'happyr.mq2php.extendable_event_envelope_consumer', 'simple_bus.asynchronous.event_bus');
23 1
    }
24
25
    /**
26
     * @param ContainerBuilder $container
27
     * @param string           $serviceId
28
     * @param string           $referenceId
29
     */
30 1
    private function replaceArgumentWithReference(ContainerBuilder $container, $serviceId, $referenceId)
31
    {
32 1
        if (!$container->hasDefinition($serviceId)) {
33
            return;
34
        }
35
36
        // If there is not $referenceId the $service has no use
37 1
        if (!$container->hasDefinition($referenceId)) {
38
            $container->removeDefinition($serviceId);
39
40
            return;
41
        }
42
43 1
        $container->getDefinition($serviceId)->replaceArgument(1, new Reference($referenceId));
44 1
    }
45
}
46