Completed
Branch master (b7eb35)
by Arnaud
02:59
created

FieldCompilerPass::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 16
cts 16
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 1
crap 4
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 4
    public function process(ContainerBuilder $container)
13
    {
14 4
        if (!$container->has('lag.admin.field_factory')) {
15 1
            return;
16
        }
17
        // get field factory definition
18 3
        $definition = $container->findDefinition(
19 3
            'lag.admin.field_factory'
20
        );
21
        // find field tagged services
22 3
        $taggedServices = $container->findTaggedServiceIds(
23 3
            'lag.field'
24
        );
25
        // foreach tagged, with add this field type to the field factory
26 3
        foreach ($taggedServices as $id => $tags) {
27 2
            if (empty($tags[0]['type'])) {
28 1
                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 1
            $definition->addMethodCall('addFieldMapping', [
32 1
                $tags[0]['type'], $id,
33
            ]);
34
            // add application configuration for field
35 1
            $tagDefinition = $container->findDefinition($id);
36 1
            $tagDefinition->addMethodCall('setConfiguration', [
37 1
                new Reference('lag.admin.application'),
38
            ]);
39
        }
40 2
    }
41
}
42