Completed
Branch master (fce9f5)
by Edwin
24:34
created

BroadcasterPass::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
ccs 9
cts 9
cp 1
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
3
namespace EdwinLuijten\Ekko\BroadcastBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class BroadcasterPass implements CompilerPassInterface
11
{
12
13
    /**
14
     * @param ContainerBuilder $container
15
     */
16 4
    public function process(ContainerBuilder $container)
17
    {
18
19 4
        if (!$container->hasDefinition('ekko.broadcast.manager')) {
20 2
            return;
21
        }
22
23 2
        $definition     = $container->getDefinition('ekko.broadcast.manager');
24 2
        $taggedServices = $container->findTaggedServiceIds('ekko.broadcaster');
25
26 2
        foreach ($taggedServices as $id => $tags) {
27 2
            $this->createAliasses($id, $tags, $definition, $container);
28 2
        }
29 2
    }
30
31 2
    private function createAliasses($id, $tags, Definition $definition, ContainerBuilder $container)
32
    {
33 2
        foreach ($tags as $attributes) {
34 2
            $reference   = new Reference($id);
35 2
            $broadcaster = $container->getDefinition($reference->__toString());
36
37
            // Set default broadcaster if defined
38 2
            if (isset($attributes['default'])) {
39 1
                $this->createDefault($broadcaster, $reference, $definition, $container);
40 1
            }
41
42 2
            $this->addBroadcaster($broadcaster, $attributes, $reference, $definition, $container);
43 2
        }
44 2
    }
45
46 1
    private function createDefault(
47
        $broadcaster,
48
        Reference $reference,
49
        Definition $definition,
50
        ContainerBuilder $container
51
    ) {
52 1
        if (!$container->hasDefinition('ekko.broadcaster.default')) {
53 1
            $definition->addMethodCall('setDefaultBroadcaster', [$broadcaster]);
54 1
            $container->setAlias('ekko.broadcaster.default', $reference->__toString());
55 1
        }
56 1
    }
57
58 2
    private function addBroadcaster(
59
        $broadcaster,
60
        array $attributes,
61
        Reference $reference,
62
        Definition $definition,
63
        ContainerBuilder $container
64
    ) {
65 2
        $definition->addMethodCall(
66 2
            'add',
67
            [
68 2
                $attributes['alias'],
69
                $broadcaster
70 2
            ]
71 2
        );
72
73
        // Register an alias for each broadcaster
74 2
        if (!$container->hasDefinition('ekko.broadcaster.' . $attributes['alias'])) {
75 2
            $container->setAlias('ekko.broadcaster.' . $attributes['alias'], $reference->__toString());
76 2
        }
77 2
    }
78
}
79