Passed
Push — master ( 34419e...bd5a67 )
by Matthew
03:07
created

GridSourceCompilerPass::addDoctrine()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 4
nop 1
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
        $this->addDoctrine($container);
14
15
        if ($container->has('templating.engine.twig')) {
16
            $container->getDefinition('dtc_grid.renderer.factory')->addMethodCall('setTwigEngine', [new Reference('templating.engine.twig')]);
17
        }
18
19
        // Add each worker to workerManager, make sure each worker has instance to work
20
        foreach ($container->findTaggedServiceIds('dtc_grid.source') as $id => $attributes) {
21
            self::addGridSource($container, $id);
22
        }
23
    }
24
25
    public function addDoctrine(ContainerBuilder $container)
26
    {
27
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
28
29
        if ($container->has('doctrine')) {
30
            $sourceManager->addMethodCall('setRegistry', [new Reference('doctrine')]);
31
        }
32
33
        if ($container->has('doctrine_mongodb')) {
34
            $sourceManager->addMethodCall('setMongodbRegistry', [new Reference('doctrine_mongodb')]);
35
        }
36
    }
37
38
    public static function addGridSource(ContainerBuilder $container, $id)
39
    {
40
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
41
        $gridSourceDefinition = $container->getDefinition($id);
42
        $class = $gridSourceDefinition->getClass();
43
44
        $refClass = new \ReflectionClass($class);
45
        $interface = 'Dtc\GridBundle\Grid\Source\GridSourceInterface';
46
47
        if (!$refClass->implementsInterface($interface)) {
48
            throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
49
        }
50
51
        $gridSourceDefinition->addMethodCall('setId', array($id));
52
        $sourceManager->addMethodCall('add', [$id, new Reference($id)]);
53
    }
54
}
55