Completed
Push — master ( 7ca809...e10277 )
by GBProd
02:21
created

ExpressionBuilderPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 25 5
1
<?php
2
3
namespace GBProd\DoctrineSpecificationBundle\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
/**
10
 * Register doctrine expression builder
11
 * 
12
 * @author gbprod 
13
 */
14
class ExpressionBuilderPass implements CompilerPassInterface
15
{
16
    /**
17
     * {inheritdoc}
18
     */
19
    public function process(ContainerBuilder $container)
20
    {
21
        if (!$container->has('gbprod.doctrine_specification_handler')) {
22
            throw new \Exception('Missing gbprod.doctrine_specification_handler definition');
23
        }
24
25
        $handler = $container
26
            ->findDefinition('gbprod.doctrine_specification_handler')
27
        ;
28
        
29
        $builders = $container->findTaggedServiceIds('doctrine.expression_builder');
30
        
31
        foreach ($builders as $id => $tags) {
32
            foreach ($tags as $attributes) {
33
                if (!isset($attributes['specification'])) {
34
                    throw new \Exception('The doctrine.expression_builder tag must always have a "specification" attribute');
35
                }
36
                
37
                $handler->addMethodCall(
38
                    'registerBuilder',
39
                    [$attributes['specification'], new Reference($id)]
40
                );
41
            }
42
        }
43
    }
44
}