Passed
Push — master ( 9fe88a...fb2e5e )
by Jakub
31s
created

Req2CmdConfiguration::addExtractorNode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\DependencyInjection;
5
6
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
7
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8
use Symfony\Component\Config\Definition\ConfigurationInterface;
9
10
final class Req2CmdConfiguration implements ConfigurationInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     * @throws \RuntimeException
15
     */
16
    public function getConfigTreeBuilder(): TreeBuilder
17
    {
18
        $builder = new TreeBuilder();
19
20
        $root = $builder->root('req2cmd');
21
        $root
22
            ->children()
23
                ->append($this->addExtractorNode())
24
                ->append($this->addCommandBusNode())
25
                ->append($this->addEventListenersNode())
26
            ->end();
27
28
        return $builder;
29
    }
30
31
    private function addExtractorNode(): NodeDefinition
32
    {
33
        $builder = new TreeBuilder();
34
        $root = $builder->root('extractor');
35
        $root
36
            ->addDefaultsIfNotSet()
37
            ->beforeNormalization()
38
                ->ifString()
39
                ->then(function ($extractorName) {
40
                    return ['service_id' => 'eps.req2cmd.extractor.' . $extractorName];
41
                })
42
            ->end()
43
            ->children()
44
                ->scalarNode('service_id')
45
                    ->cannotBeEmpty()
46
                    ->defaultValue('eps.req2cmd.extractor.serializer')
47
                ->end()
48
                ->scalarNode('use_cmd_denormalizer')
49
                    ->cannotBeEmpty()
50
                    ->defaultTrue()
51
                ->end()
52
            ->end();
53
54
        return $root;
55
    }
56
57
    private function addCommandBusNode(): NodeDefinition
58
    {
59
        $builder = new TreeBuilder();
60
        $node = $builder->root('command_bus');
61
        $node
62
            ->addDefaultsIfNotSet()
63
            ->beforeNormalization()
64
                ->ifString()
65
                ->then(function (string $svcId) {
66
                    return ['service_id' => 'eps.req2cmd.command_bus.' . $svcId];
67
                })
68
            ->end()
69
            ->children()
70
                ->scalarNode('service_id')
71
                    ->cannotBeEmpty()
72
                    ->defaultValue('eps.req2cmd.command_bus.tactician')
73
                ->end()
74
                ->scalarNode('name')
75
                    ->cannotBeEmpty()
76
                    ->defaultValue('default')
77
                ->end()
78
            ->end()
79
            ->validate()
80
                ->ifTrue(function ($config) {
81
                    return $config['service_id'] !== 'eps.req2cmd.command_bus.tactician';
82
                })
83
                ->then(function ($config) {
84
                    unset($config['name']);
85
                    return $config;
86
                })
87
            ->end();
88
89
        return $node;
90
    }
91
92
    private function addEventListenersNode(): NodeDefinition
93
    {
94
        $builder = new TreeBuilder();
95
        $node = $builder->root('listeners');
96
97
        $node
98
            ->addDefaultsIfNotSet()
99
            ->children()
100
                ->arrayNode('extractor')
101
                    ->addDefaultsIfNotSet()
102
                    ->beforeNormalization()
103
                        ->ifTrue(function ($enabled) { return is_bool($enabled); })
104
                        ->then(function ($isEnabled) {
105
                            return ['enabled' => $isEnabled];
106
                        })
107
                    ->end()
108
                    ->children()
109
                        ->booleanNode('enabled')
110
                            ->defaultValue(true)
111
                        ->end()
112
                        ->integerNode('priority')
113
                            ->defaultValue(0)
114
                        ->end()
115
                    ->end()
116
                ->end()
117
            ->end();
118
119
        return $node;
120
    }
121
}
122