Completed
Pull Request — master (#28)
by Jamal
26:57
created

LoaderBridgeFormCompilerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
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 33
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 25 5
1
<?php
2
3
namespace Majora\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler;
4
5
use Majora\Framework\Loader\Bridge\LoaderBridgeInterface;
6
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Reference;
9
10
/**
11
 * Compiler pass to register bridge loaders into loader form bridge.
12
 */
13
class LoaderBridgeFormCompilerPass implements CompilerPassInterface
14
{
15
    /**
16
     * {@inheritdoc}
17
     *
18
     * Processes "majora.loader_bridge.form" tags
19
     */
20
    public function process(ContainerBuilder $container)
21
    {
22
        $entityCollectionType = $container->getDefinition('majora.loader.bridge.form.type.entity_collection');
23
        $loaders = $container->findTaggedServiceIds('majora.loader_bridge.form');
24
25
        foreach ($loaders as $loaderId => $tags) {
26
            $loaderDefinition = $container->getDefinition($loaderId);
27
            $loaderReflection = new \ReflectionClass($loaderDefinition->getClass());
28
29
            if (!$loaderReflection->implementsInterface(LoaderBridgeInterface::class)) {
30
                throw new \RuntimeException(sprintf('The loader "%s" have to implement "%s" because it is tagged as "majora.loader_bridge.form".', $loaderDefinition->getClass(), LoaderBridgeInterface::class));
31
            }
32
33
            foreach ($tags as $attributes) {
34
                if (!isset($attributes['alias'])) {
35
                    throw new \RuntimeException('Alias required for "majora.loader_bridge.form" tag.');
36
                }
37
38
                $entityCollectionType->addMethodCall(
39
                    'registerLoader',
40
                    array($attributes['alias'], new Reference($loaderId))
41
                );
42
            }
43
        }
44
    }
45
}
46