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

Configuration   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 39.29%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 81
ccs 11
cts 28
cp 0.3929
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 25 1
A createTreeBuilder() 0 10 2
A getRootNode() 0 18 3
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