Completed
Pull Request — master (#201)
by Simonas
61:07
created

Configuration::addFiltersSection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 38
rs 8.8571
c 1
b 0
f 1
cc 1
eloc 32
nc 1
nop 1
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
                    ->validate()
77
                        ->ifTrue(
78
                            function ($v) {
79
                                $v = array_filter($v);
80
81
                                return empty($v);
82
                            }
83
                        )
84
                        ->thenInvalid('At least single filter must be configured.')
85
                    ->end()
86
                    ->prototype('array')
87
                        ->children()
88
//                            ->scalarNode('name')->end()
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
                            ->scalarNode('type')->end()
90
                            ->scalarNode('document_field')->end()
91
                            ->scalarNode('request_field')->end()
92
                            ->arrayNode('tags')
93
                                ->prototype('scalar')->end()
94
                            ->end()
95
                            ->arrayNode('relations')
96
                                ->children()
97
                                    ->append($this->buildRelationsTree('search'))
98
                                    ->append($this->buildRelationsTree('reset'))
99
                                ->end()
100
                            ->end()
101
                            ->arrayNode('options')
102
                                ->prototype('variable')->end()
103
                            ->end()
104
                        ->end()
105
                    ->end()
106
                ->end()
107
            ->end();
108
    }
109
110
    /**
111
     * Builds relations config tree for given relation name.
112
     *
113
     * @param string $relationType
114
     *
115
     * @return ArrayNodeDefinition
116
     */
117
    private function buildRelationsTree($relationType)
118
    {
119
        $filter = new ArrayNodeDefinition($relationType);
120
121
        $filter
122
            ->validate()
123
                ->ifTrue(
124
                    function ($v) {
125
                        return empty($v['include']) && empty($v['exclude']);
126
                    }
127
                )
128
                ->thenInvalid('Relation must have "include" or "exclude" fields specified.')
129
            ->end()
130
            ->validate()
131
                ->ifTrue(
132
                    function ($v) {
133
                        return !empty($v['include']) && !empty($v['exclude']);
134
                    }
135
                )
136
                ->thenInvalid('Relation must have only "include" or "exclude" fields specified.')
137
            ->end()
138
            ->children()
139
                ->arrayNode('include')
140
                    ->beforeNormalization()
141
                        ->ifString()
142
                        ->then(
143
                            function ($v) {
144
                                return [$v];
145
                            }
146
                        )->end()
147
                    ->prototype('scalar')->end()
148
                ->end()
149
                ->arrayNode('exclude')
150
                    ->beforeNormalization()
151
                        ->ifString()
152
                        ->then(
153
                            function ($v) {
154
                                return [$v];
155
                            }
156
                        )
157
                    ->end()
158
                    ->prototype('scalar')->end()
159
                ->end()
160
            ->end();
161
162
        return $filter;
163
    }
164
}
165