Configuration   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 90.48%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 75
ccs 19
cts 21
cp 0.9048
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigTreeBuilder() 0 19 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 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();
0 ignored issues
show
Bug introduced by
The call to TreeBuilder::__construct() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
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
Bug introduced by
The method root() does not seem to exist on object<Symfony\Component...on\Builder\TreeBuilder>.

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...
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