Passed
Push — master ( e6a56f...0418ee )
by Matthew
03:57
created

GridSourceCompilerPass::addGridSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
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
            self::addGridSource($container, $id);
26
        }
27
    }
28
29
    public static function addGridSource(ContainerBuilder $container, $id) {
30
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
31
        $gridSourceDefinition = $container->getDefinition($id);
32
        $class = $gridSourceDefinition->getClass();
33
34
        $refClass = new \ReflectionClass($class);
35
        $interface = 'Dtc\GridBundle\Grid\Source\GridSourceInterface';
36
37
        if (!$refClass->implementsInterface($interface)) {
38
            throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
39
        }
40
41
        $gridSourceDefinition->addMethodCall('setId', array($id));
42
        $sourceManager->addMethodCall('add', [$id, new Reference($id)]);
43
    }
44
}
45