Completed
Push — master ( c8edce...0342c6 )
by Peter
14s queued 12s
created

Configuration::getRootNode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 4
cts 5
cp 0.8
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3.072
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Bundle\DomainEvent\DependencyInjection;
11
12
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
13
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
14
use Symfony\Component\Config\Definition\ConfigurationInterface;
15
16
class Configuration implements ConfigurationInterface
17
{
18
    /**
19
     * Config tree builder.
20
     *
21
     * Example config:
22
     *
23
     * gpslab_domain_event:
24
     *     bus: 'listener_located'
25
     *     queue: 'pull_memory'
26
     *     locator: 'symfony'
27
     *
28
     * @return TreeBuilder
29
     */
30 5
    public function getConfigTreeBuilder()
31
    {
32 5
        $tree_builder = $this->createTreeBuilder('gpslab_domain_event');
33 5
        $root = $this->getRootNode($tree_builder, 'gpslab_domain_event');
34
35 5
        $bus = $root->children()->scalarNode('bus');
36 5
        $bus->cannotBeEmpty()->defaultValue('listener_located');
37
38 5
        $queue = $root->children()->scalarNode('queue');
39 5
        $queue->cannotBeEmpty()->defaultValue('pull_memory');
40
41 5
        $locator = $root->children()->scalarNode('locator');
42 5
        $locator->cannotBeEmpty()->defaultValue('symfony');
43
44 5
        $publish_on_flush = $root->children()->booleanNode('publish_on_flush');
45 5
        $publish_on_flush->defaultValue(false);
46
47 5
        return $tree_builder;
48
    }
49
50
    /**
51
     * @param string $name
52
     *
53
     * @return TreeBuilder
54
     */
55 5
    private function createTreeBuilder($name)
56
    {
57
        // Symfony 4.2 +
58 5
        if (method_exists(TreeBuilder::class, '__construct')) {
59 5
            return new TreeBuilder($name);
60
        }
61
62
        // Symfony 4.1 and below
63
        return new TreeBuilder();
64
    }
65
66
    /**
67
     * @param TreeBuilder $tree_builder
68
     * @param string      $name
69
     *
70
     * @return ArrayNodeDefinition
71
     */
72 5
    private function getRootNode(TreeBuilder $tree_builder, $name)
73
    {
74 5
        if (method_exists($tree_builder, 'getRootNode')) {
75
            // Symfony 4.2 +
76 5
            $root = $tree_builder->getRootNode();
77
        } else {
78
            // Symfony 4.1 and below
79
            $root = $tree_builder->root($name);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Config...der\TreeBuilder::root() has been deprecated with message: since Symfony 4.3, pass the root name to the constructor instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
80
        }
81
82
        // @codeCoverageIgnoreStart
83
        if (!($root instanceof ArrayNodeDefinition)) { // should be always false
84
            throw new \RuntimeException(sprintf('The root node should be instance of %s, got %s instead.', ArrayNodeDefinition::class, get_class($root)));
85
        }
86
        // @codeCoverageIgnoreEnd
87
88 5
        return $root;
89
    }
90
}
91