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

GridSourceCompilerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 26 5
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