ContainerCallbackPass::process()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 20
cts 20
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 6
nop 1
crap 8
1
<?php
2
3
namespace Finite\Bundle\FiniteBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
/**
10
 * Replace '@xxx' with the defined service if exists in loader configs.
11
 *
12
 * @author Alexandre Bacco <[email protected]>
13
 */
14
class ContainerCallbackPass implements CompilerPassInterface
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19 5
    public function process(ContainerBuilder $container)
20
    {
21 5
        $loaders = $container->findTaggedServiceIds('finite.loader');
22
23 5
        foreach ($loaders as $id => $loader) {
24 5
            $definition = $container->getDefinition($id);
25 5
            $config = $definition->getArgument(0);
26 5
            if (isset($config['callbacks'])) {
27 5
                foreach (array('before', 'after') as $position) {
28 5
                    foreach ($config['callbacks'][$position] as &$callback) {
29
                        if (
30 5
                            is_array($callback['do'])
31 5
                            && 0 === strpos($callback['do'][0], '@')
32 5
                            && $container->hasDefinition(substr($callback['do'][0], 1))
33 3
                        ) {
34 5
                            $callback['do'][0] = new Reference(substr($callback['do'][0], 1));
35 3
                        }
36 3
                    }
37 3
                }
38
39 5
                $definition->replaceArgument(0, $config);
40 3
            }
41 3
        }
42 5
    }
43
}
44