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

LoaderBridgeFormCompilerPass::process()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 25
rs 8.439
cc 5
eloc 14
nc 5
nop 1
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