anonymous//src/ThrusterDataModifierBundle.php$0   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%
Metric Value
wmc 5
lcom 0
cbo 3
dl 0
loc 41
ccs 0
cts 0
cp 0
rs 10
1
<?php
2
3
namespace Thruster\Bundle\DataModifierBundle;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Reference;
9
use Symfony\Component\HttpKernel\Bundle\Bundle;
10
use function Funct\arrayKeyNotExists;
11
use function Funct\Collection\get;
12
13
/**
14
 * Class ThrusterDataModifierBundle
15
 *
16
 * @package Thruster\Bundle\DataModifierBundle
17
 * @author  Aurimas Niekis <[email protected]>
18
 */
19
class ThrusterDataModifierBundle extends Bundle
20
{
21
    /**
22
     * @inheritDoc
23
     */
24 2
    public function build(ContainerBuilder $container)
25
    {
26 2
        parent::build($container);
27
28 2
        $container->addCompilerPass(
29
            new class implements CompilerPassInterface
30
            {
31
                /**
32
                 * @inheritDoc
33
                 */
34
                public function process(ContainerBuilder $container)
35
                {
36
                    $groupsId         = 'thruster_data_modifiers';
37
                    $groupsDefinition = new Definition('Thruster\Component\DataModifier\DataModifierGroups');
38
39
                    $container->setDefinition($groupsId, $groupsDefinition);
40
41
                    foreach ($container->findTaggedServiceIds('thruster_data_modifier') as $id => $tags) {
42
                        foreach ($tags as $tag) {
43
                            if (arrayKeyNotExists('group', $tag)) {
44
                                throw new \LogicException(
45
                                    sprintf(
46
                                        'DataModifier tag requires attribute "group" in definition "%s"',
47
                                        $id
48
                                    )
49
                                );
50
                            }
51
52
                            $groupName = $tag['group'];
53
                            $priority = get($tag, 'priority', 0);
54
55
                            $groupId = 'thruster_data_modifier.group.' . $groupName;
56
                            if ($container->hasDefinition($groupId)) {
57
                                $groupDefinition = $container->getDefinition($groupId);
58
                            } else {
59
                                $groupDefinition = new Definition('Thruster\Component\DataModifier\DataModifierGroup');
60
                                $container->setDefinition($groupId, $groupDefinition);
61
62
                                $groupsDefinition->addMethodCall('addGroup', [$groupName, new Reference($groupId)]);
63
                            }
64
65
                            $groupDefinition->addMethodCall('addModifier', [new Reference($id), $priority]);
66
                        }
67
                    }
68
                }
69
            }
70
        );
71 1
    }
72
}
73