Completed
Push — master ( 1e2055...d445f7 )
by Christian
03:00
created

AddTaskFactoryPass::process()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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