SpecElementPass::process()   C
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 10
nop 1
dl 0
loc 39
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\DI\Compiler;
4
5
use Sugarcrm\UpgradeSpec\Data\DataAwareInterface;
6
use Sugarcrm\UpgradeSpec\Template\RendererAwareInterface;
7
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Reference;
10
11
class SpecElementPass implements CompilerPassInterface
12
{
13
    /**
14
     * @param ContainerBuilder $container
15
     */
16
    public function process(ContainerBuilder $container)
17
    {
18
        if (!$container->has('element.provider')) {
19
            return;
20
        }
21
22
        $providerDefinition = $container->findDefinition('element.provider');
23
24
        $elements = [];
25
        foreach (array_keys($container->findTaggedServiceIds('element.section')) as $id) {
26
            if (!$container->has($id)) {
27
                return;
28
            }
29
30
            $elementDefinition = $container->findDefinition($id);
31
            $elementClass = $elementDefinition->getClass();
32
33
            $reflectionClass = new \ReflectionClass($elementClass);
34
            if ($reflectionClass->implementsInterface(RendererAwareInterface::class)) {
35
                if (!$container->has('template.renderer')) {
36
                    return;
37
                }
38
39
                $elementDefinition->addMethodCall('setRenderer', [new Reference('template.renderer')]);
40
            }
41
42
            if ($reflectionClass->implementsInterface(DataAwareInterface::class)) {
43
                if (!$container->has('data.manager')) {
44
                    return;
45
                }
46
47
                $elementDefinition->addMethodCall('setDataManager', [new Reference('data.manager')]);
48
            }
49
50
            $elements[] = new Reference($id);
51
        }
52
53
        $providerDefinition->addMethodCall('addElements', [$elements]);
54
    }
55
}
56