Completed
Push — 5.0 ( f4c5f6...b6d267 )
by Ruud
37:21
created

Configuration::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Kunstmaan\NodeSearchBundle\DependencyInjection;
4
5
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7
use Symfony\Component\Config\Definition\ConfigurationInterface;
8
use Kunstmaan\NodeSearchBundle\Helper\ElasticSearchUtil;
9
10
/**
11
 * This is the class that validates and merges configuration from your app/config files
12
 *
13
 * To learn more see {@link
14
 * http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
15
 */
16
class Configuration implements ConfigurationInterface
17
{
18
    /**
19
     * @var bool
20
     */
21
    private $useElasticSearchVersion6;
22
23
24
    /**
25
     * Configuration constructor.
26
     * @param bool $useElasticSearchVersion6
27
     */
28
    public function __construct($useElasticSearchVersion6)
29
    {
30
        $this->useElasticSearchVersion6 = $useElasticSearchVersion6;
31
    }
32
33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function getConfigTreeBuilder()
37
    {
38
        $treeBuilder = new TreeBuilder();
39
        $rootNode    = $treeBuilder->root('kunstmaan_node_search');
40
41
        $rootNode->children()->booleanNode('enable_update_listener')->defaultTrue();
42
        $rootNode->children()->booleanNode('use_match_query_for_title')->defaultFalse();
43
44
        /** @var ArrayNodeDefinition $properties */
45
        $properties = $rootNode->children()->arrayNode('mapping')->useAttributeAsKey('name')->prototype('array');
46
47
        $types = [
48
            'token_count', 'text', 'keyword',
49
            'float', 'double', 'byte', 'short', 'integer', 'long',
50
            'date',
51
            'boolean',
52
            'binary',
53
        ];
54
        if (!$this->useElasticSearchVersion6) {
55
            $types[] = 'string';
56
        }
57
58
        $properties->children()->scalarNode('type')->beforeNormalization()->ifNotInArray($types)->thenInvalid('type must be one of: ' . implode(', ', $types));
59
60
        if ($this->useElasticSearchVersion6) {
61
            $properties->children()->booleanNode('fielddata');
62
            $properties->children()->booleanNode('doc_values');
63
            $properties->children()
64
                ->scalarNode('index')
65
                ->beforeNormalization()
66
                ->ifNotInArray(['true', 'false', true, false])
67
                ->thenInvalid("index must be one of: true, false");
68
        } else {
69
            $properties->children()
70
                ->scalarNode('index')
71
                ->beforeNormalization()
72
                ->ifNotInArray(['analyzed', 'not_analyzed', 'no'])
73
                ->thenInvalid("index must be one of: analyzed, not_analyzed, no");
74
            $properties->children()->booleanNode('include_in_all');
75
        }
76
77
        $properties->children()->booleanNode('store');
78
        $properties->children()->floatNode('boost');
79
        $properties->children()->scalarNode('null_value');
80
        $properties->children()->scalarNode('analyzer');
81
        $properties->children()->scalarNode('search_analyzer');
82
        $properties->children()->scalarNode('index_analyzer');
83
        $properties->children()->scalarNode('copy_to');
84
        $properties->children()->scalarNode('term_vector')->beforeNormalization()->ifNotInArray(['yes', 'no', 'with_positions', 'with_offsets', 'with_positions_offsets'])
85
            ->thenInvalid("term_vector must be one of: yes, no, with_positions, with_offsets, with_positions_offsets");
86
87
        $rootNode
88
            ->children()
89
                ->arrayNode('contexts')
90
                ->defaultValue([])
91
                ->prototype('scalar')->end()
92
            ->end();
93
94
        return $treeBuilder;
95
    }
96
}
97