AddTaskFactoryPass::process()   B
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
rs 8.439
c 1
b 0
f 0
cc 6
eloc 15
nc 10
nop 1
1
<?php
2
3
/**
4
 * This file is part of tenside/core-bundle.
5
 *
6
 * (c) Christian Schiffler <[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
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core-bundle
14
 * @author     Christian Schiffler <[email protected]>
15
 * @author     Yanick Witschi <[email protected]>
16
 * @copyright  2015 Christian Schiffler <[email protected]>
17
 * @license    https://github.com/tenside/core-bundle/blob/master/LICENSE MIT
18
 * @link       https://github.com/tenside/core-bundle
19
 * @filesource
20
 */
21
22
namespace Tenside\CoreBundle\DependencyInjection\Compiler;
23
24
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\Reference;
27
28
/**
29
 * Registers the task factories
30
 */
31
class AddTaskFactoryPass implements CompilerPassInterface
32
{
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function process(ContainerBuilder $container)
37
    {
38
        if (!$container->hasDefinition('tenside.taskfactory')) {
39
            return;
40
        }
41
42
        $factories = [];
43
        foreach ($container->findTaggedServiceIds('tenside.taskfactory') as $id => $attributes) {
44
            $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
45
46
            $factories[$priority][] = new Reference($id);
47
        }
48
49
        if (0 === count($factories)) {
50
            return;
51
        }
52
53
        // sort by priority and flatten
54
        krsort($factories);
55
        $factories = call_user_func_array('array_merge', $factories);
56
57
        foreach ($factories as $factory) {
58
            $container->getDefinition('tenside.taskfactory')->addMethodCall(
59
                'add',
60
                [$factory]
61
            );
62
        }
63
    }
64
}
65