Completed
Push — master ( 209945...41a4bc )
by Matthew
07:41 queued 03:34
created

GridSourceCompilerPass::process()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 8
nop 1
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\GridBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\ContainerBuilder;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
class GridSourceCompilerPass implements CompilerPassInterface
10
{
11
    public function process(ContainerBuilder $container)
12
    {
13
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
14
15
        if ($container->has('doctrine')) {
16
            $sourceManager->addMethodCall('setRegistry', [new Reference('doctrine')]);
17
        }
18
19
        if ($container->has('doctrine_mongodb')) {
20
            $sourceManager->addMethodCall('setMongodbRegistry', [new Reference('doctrine_mongodb')]);
21
        }
22
23
        // Add each worker to workerManager, make sure each worker has instance to work
24
        foreach ($container->findTaggedServiceIds('dtc_grid.source') as $id => $attributes) {
25
            $gridSourceDefinition = $container->getDefinition($id);
26
            $class = $gridSourceDefinition->getClass();
27
28
            $refClass = new \ReflectionClass($class);
29
            $interface = 'Dtc\GridBundle\Grid\Source\GridSourceInterface';
30
31
            if (!$refClass->implementsInterface($interface)) {
32
                throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
33
            }
34
35
            $gridSourceDefinition->addMethodCall('setId', array($id));
36
            $sourceManager->addMethodCall('add', [$id, new Reference($id)]);
37
        }
38
    }
39
}
40