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

GridSourceCompilerPass   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 0
cbo 3
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 13 3
A addDoctrine() 0 12 3
A addGridSource() 0 16 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
        $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