Completed
Pull Request — master (#10)
by Peter
08:44
created

Configuration::createTreeBuilder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 3
cts 4
cp 0.75
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.0625
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
        $root
0 ignored issues
show
Bug introduced by
The method scalarNode() does not seem to exist on object<Symfony\Component...er\ArrayNodeDefinition>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36 5
            ->scalarNode('bus')
37
                ->cannotBeEmpty()
38
                ->defaultValue('listener_located')
39
            ->end()
40
            ->scalarNode('queue')
41
                ->cannotBeEmpty()
42
                ->defaultValue('pull_memory')
43
            ->end()
44
            ->scalarNode('locator')
45
                ->cannotBeEmpty()
46
                ->defaultValue('symfony')
47
            ->end()
48
            ->booleanNode('publish_on_flush')
49
                ->defaultValue(false)
50
            ->end()
51
        ;
52
53
        return $tree_builder;
54
    }
55
56
    /**
57
     * @param string $name
58
     *
59
     * @return TreeBuilder
60
     */
61 5
    private function createTreeBuilder($name)
62
    {
63
        // Symfony 4.2 +
64 5
        if (method_exists(TreeBuilder::class, '__construct')) {
65 5
            return new TreeBuilder($name);
66
        }
67
68
        // Symfony 4.1 and below
69
        return new TreeBuilder();
70
    }
71
72
    /**
73
     * @param TreeBuilder $tree_builder
74
     * @param string      $name
75
     *
76
     * @return ArrayNodeDefinition
77
     */
78 5
    private function getRootNode(TreeBuilder $tree_builder, $name)
79
    {
80 5
        if (method_exists($tree_builder, 'getRootNode')) {
81
            // Symfony 4.2 +
82 5
            $root = $tree_builder->getRootNode();
83
        } else {
84
            // Symfony 4.1 and below
85
            $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...
86
        }
87
88
        // @codeCoverageIgnoreStart
89
        if (!($root instanceof ArrayNodeDefinition)) { // should be always false
90
            throw new \RuntimeException(sprintf('The root node should be instance of %s, got %s instead.', ArrayNodeDefinition::class, get_class($root)));
91
        }
92
        // @codeCoverageIgnoreEnd
93
94 5
        return $root;
95
    }
96
}
97