Completed
Push — master ( b77c5c...4fbeac )
by Jeroen
10s
created

TaskCompilerPass::process()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 19

Duplication

Lines 34
Ratio 100 %

Code Coverage

Tests 16
CRAP Score 6.0493

Importance

Changes 0
Metric Value
dl 34
loc 34
ccs 16
cts 18
cp 0.8889
rs 8.439
c 0
b 0
f 0
cc 6
eloc 19
nc 6
nop 1
crap 6.0493
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
/**
18
 * Pass to add tagged tasks to the TaskFactory
19
 *
20
 */
21 View Code Duplication
class TaskCompilerPass implements CompilerPassInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
22
{
23 3
    public function process(ContainerBuilder $container)
24
    {
25 3
        if (!$container->hasDefinition('task.factory')) {
26
            return;
27
        }
28
29 3
        $definition = $container->getDefinition('task.factory');
30 3
        $taggedServices = $container->findTaggedServiceIds('task');
31
32 3
        foreach ($taggedServices as $id => $tagsAttributes) {
33 3
            foreach ($tagsAttributes as $attributes) {
34 3
                $taskDefinition = $container->getDefinition($id);
35 3
                $taskDefinition->setScope('prototype');
36
37
                $attributes += array(
38 3
                    'alias'         => $id,
39
                    'configuration' => false,
40
                );
41
42
                // validate configuration attribute
43 3
                if (false !== $attributes['configuration']) {
44 3
                    $ref = new \ReflectionClass($attributes['configuration']);
45 3
                    if (!$ref->implementsInterface('Symfony\Component\Config\Definition\ConfigurationInterface')) {
46
                        throw new \Exception(sprintf('The tag attribute "configuration" for service "%s" must implement "Symfony\Component\Config\Definition\ConfigurationInterface"', $id));
47
                    }
48
                }
49
50 3
                $definition->addMethodCall(
51 3
                    'addTask',
52 3
                    array($id, $attributes['alias'], $attributes['configuration'])
53
                );
54
            }
55
        }
56 3
    }
57
}
58