Completed
Push — master ( e2173c...0d60f0 )
by Simonas
61:10
created

Configuration::buildFilterTree()   D

Complexity

Conditions 19
Paths 26

Size

Total Lines 207
Code Lines 187

Duplication

Lines 44
Ratio 21.26 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 44
loc 207
rs 4.764
cc 19
eloc 187
nc 26
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\FilterManagerBundle\DependencyInjection;
13
14
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation;
15
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
16
use Symfony\Component\Config\Definition\Builder\ParentNodeDefinitionInterface;
17
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
18
use Symfony\Component\Config\Definition\ConfigurationInterface;
19
20
/**
21
 * This is the class that validates and merges configuration from app/config files.
22
 */
23
class Configuration implements ConfigurationInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function getConfigTreeBuilder()
29
    {
30
        $treeBuilder = new TreeBuilder();
31
        $rootNode = $treeBuilder->root('ongr_filter_manager');
32
33
        $this->addManagersSection($rootNode);
34
        $this->addFiltersSection($rootNode);
35
36
        return $treeBuilder;
37
    }
38
39
    /**
40
     * @param ArrayNodeDefinition $rootNode
41
     */
42
    private function addManagersSection(ArrayNodeDefinition $rootNode)
43
    {
44
        $rootNode
45
            ->children()
46
                ->arrayNode('managers')
47
                    ->requiresAtLeastOneElement()
48
                    ->useAttributeAsKey('name')
49
                    ->prototype('array')
50
                        ->children()
51
                            ->scalarNode('name')
52
                                ->info('Filter manager name')
53
                            ->end()
54
                            ->scalarNode('repository')
55
                                ->isRequired()
56
                                ->info('ElasticsearchBundle repository used for fetching data.')
57
                            ->end()
58
                            ->arrayNode('filters')
59
                                ->info('Filter names to include in manager.')
60
                                ->prototype('scalar')->end()
61
                            ->end()
62
                        ->end()
63
                    ->end()
64
                ->end()
65
            ->end();
66
    }
67
68
    /**
69
     * @param ArrayNodeDefinition $rootNode
70
     */
71
    private function addFiltersSection(ArrayNodeDefinition $rootNode)
72
    {
73
        $rootNode
74
            ->children()
75
                ->arrayNode('filters')
76
                    ->useAttributeAsKey('name')
77
                    ->prototype('array')
78
                        ->children()
79
                            ->scalarNode('name')->info('Filter name')->end()
80
                            ->scalarNode('type')->end()
81
                            ->scalarNode('document_field')->end()
82
                            ->scalarNode('request_field')->end()
83
                            ->arrayNode('tags')
84
                                ->prototype('scalar')->end()
85
                            ->end()
86
                            ->arrayNode('relations')
87
                                ->children()
88
                                    ->append($this->buildRelationsTree('search'))
89
                                    ->append($this->buildRelationsTree('reset'))
90
                                ->end()
91
                            ->end()
92
                            ->arrayNode('options')
93
                                ->prototype('variable')->end()
94
                            ->end()
95
                        ->end()
96
                    ->end()
97
                ->end()
98
            ->end();
99
    }
100
101
    /**
102
     * Builds relations config tree for given relation name.
103
     *
104
     * @param string $relationType
105
     *
106
     * @return ArrayNodeDefinition
107
     */
108
    private function buildRelationsTree($relationType)
109
    {
110
        $filter = new ArrayNodeDefinition($relationType);
111
112
        $filter
113
            ->validate()
114
                ->ifTrue(
115
                    function ($v) {
116
                        return empty($v['include']) && empty($v['exclude']);
117
                    }
118
                )
119
                ->thenInvalid('Relation must have "include" or "exclude" fields specified.')
120
            ->end()
121
            ->validate()
122
                ->ifTrue(
123
                    function ($v) {
124
                        return !empty($v['include']) && !empty($v['exclude']);
125
                    }
126
                )
127
                ->thenInvalid('Relation must have only "include" or "exclude" fields specified.')
128
            ->end()
129
            ->children()
130
                ->arrayNode('include')
131
                    ->beforeNormalization()
132
                        ->ifString()
133
                        ->then(
134
                            function ($v) {
135
                                return [$v];
136
                            }
137
                        )->end()
138
                    ->prototype('scalar')->end()
139
                ->end()
140
                ->arrayNode('exclude')
141
                    ->beforeNormalization()
142
                        ->ifString()
143
                        ->then(
144
                            function ($v) {
145
                                return [$v];
146
                            }
147
                        )
148
                    ->end()
149
                    ->prototype('scalar')->end()
150
                ->end()
151
            ->end();
152
153
        return $filter;
154
    }
155
}
156