Completed
Push — dev ( 5c06f5...dcd39b )
by Arnaud
09:19
created

FieldCompilerPass::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 29
ccs 0
cts 24
cp 0
rs 8.5806
cc 4
eloc 15
nc 4
nop 1
crap 20
1
<?php
2
3
namespace LAG\AdminBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
class FieldCompilerPass implements CompilerPassInterface
11
{
12
    public function process(ContainerBuilder $container)
13
    {
14
        if (!$container->has('lag.admin.field_factory')) {
15
            return;
16
        }
17
        // get field factory definition
18
        $definition = $container->findDefinition(
19
            'lag.admin.field_factory'
20
        );
21
        // find field tagged services
22
        $taggedServices = $container->findTaggedServiceIds(
23
            'lag.field'
24
        );
25
        // foreach tagged, with add this field type to the field factory
26
        foreach ($taggedServices as $id => $tags) {
27
            if (empty($tags[0]['type'])) {
28
                throw new InvalidConfigurationException('You should defined a "type" attribute for field tag for service '.$id);
29
            }
30
            // add allowed field type to the field factory
31
            $definition->addMethodCall('addFieldMapping', [
32
                $tags[0]['type'], $id,
33
            ]);
34
            // add application configuration for field
35
            $tagDefinition = $container->findDefinition($id);
36
            $tagDefinition->addMethodCall('setConfiguration', [
37
                new Reference('lag.admin.application'),
38
            ]);
39
        }
40
    }
41
}
42