Completed
Push — master ( 8a5d0f...f400e4 )
by Wachter
23:12
created

HandlerCompilerPass::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 18
ccs 13
cts 14
cp 0.9286
crap 4.0058
rs 9.2
1
<?php
2
3
namespace Task\TaskBundle\DependencyInjection;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
/**
10
 * Compiler pass which collects worker services.
11
 *
12
 * @author @wachterjohannes <[email protected]>
13
 */
14
class HandlerCompilerPass implements CompilerPassInterface
15
{
16
    const REGISTRY_ID = 'task.handler_registry';
17
    const HANDLER_TAG = 'task.handler';
18
    const ADD_FUNCTION_NAME = 'add';
19
    const HANDLER_NAME_ATTRIBUTE = 'handler-name';
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 36
    public function process(ContainerBuilder $container)
25
    {
26 36
        if (!$container->has(self::REGISTRY_ID)) {
27
            return;
28
        }
29
30 36
        $definition = $container->findDefinition(self::REGISTRY_ID);
31
32 36
        $taggedServices = $container->findTaggedServiceIds(self::HANDLER_TAG);
33 36
        foreach ($taggedServices as $id => $tags) {
34 36
            foreach ($tags as $attributes) {
35 36
                $definition->addMethodCall(
36 36
                    self::ADD_FUNCTION_NAME,
37 36
                    [$attributes[self::HANDLER_NAME_ATTRIBUTE], new Reference($id)]
38 24
                );
39 24
            }
40 24
        }
41 36
    }
42
}
43