Completed
Pull Request — master (#1343)
by Dmitry
06:23
created

MappingBuilder   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 121
ccs 46
cts 48
cp 0.9583
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildIndexMapping() 0 20 4
F buildTypeMapping() 0 46 11
A fixProperties() 0 16 5
A buildIndexTemplateMapping() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\ElasticaBundle\Index;
13
14
use FOS\ElasticaBundle\Configuration\IndexConfigAbstract;
15
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig;
16
use FOS\ElasticaBundle\Configuration\TypeConfig;
17
18
class MappingBuilder
19
{
20
    /**
21
     * Builds mappings for an entire index.
22
     *
23
     * @param IndexConfigAbstract $indexConfig
24
     *
25
     * @return array
26
     */
27 9
    public function buildIndexMapping(IndexConfigAbstract $indexConfig)
28
    {
29 9
        $typeMappings = [];
30 9
        foreach ($indexConfig->getTypes() as $typeConfig) {
31 9
            $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
32
        }
33
34 9
        $mapping = [];
35 9
        if (!empty($typeMappings)) {
36 9
            $mapping['mappings'] = $typeMappings;
37
        }
38
        // 'warmers' => $indexConfig->getWarmers(),
39
40 9
        $settings = $indexConfig->getSettings();
41 9
        if (!empty($settings)) {
42 6
            $mapping['settings'] = $settings;
43
        }
44
45 9
        return $mapping;
46
    }
47
48
    /**
49
     * Builds mappings for an entire index template.
50
     *
51
     * @param IndexTemplateConfig $indexTemplateConfig
52
     *
53
     * @return array
54
     */
55 4
    public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig)
56
    {
57 4
        $mapping = $this->buildIndexMapping($indexTemplateConfig);
58 4
        $mapping['template'] = $indexTemplateConfig->getTemplate();
59 4
        return $mapping;
60
    }
61
62
    /**
63
     * Builds mappings for a single type.
64
     *
65
     * @param TypeConfig $typeConfig
66
     *
67
     * @return array
68
     */
69 10
    public function buildTypeMapping(TypeConfig $typeConfig)
70
    {
71 10
        $mapping = $typeConfig->getMapping();
72
73 10
        if (null !== $typeConfig->getDynamicDateFormats()) {
74 9
            $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats();
75
        }
76
77 10
        if (null !== $typeConfig->getDateDetection()) {
78 2
            $mapping['date_detection'] = $typeConfig->getDateDetection();
79
        }
80
81 10
        if (null !== $typeConfig->getNumericDetection()) {
82 2
            $mapping['numeric_detection'] = $typeConfig->getNumericDetection();
83
        }
84
85 10
        if ($typeConfig->getAnalyzer()) {
86
            $mapping['analyzer'] = $typeConfig->getAnalyzer();
87
        }
88
89 10
        if (null !== $typeConfig->getDynamic()) {
90 2
            $mapping['dynamic'] = $typeConfig->getDynamic();
91
        }
92
93 10
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
94 9
            unset($mapping['dynamic_templates']);
95
        }
96
97 10
        $this->fixProperties($mapping['properties']);
98 10
        if (!$mapping['properties']) {
99 3
            unset($mapping['properties']);
100
        }
101
102 10
        if ($typeConfig->getModel()) {
103 3
            $mapping['_meta']['model'] = $typeConfig->getModel();
104
        }
105
106 10
        unset($mapping['_parent']['identifier'], $mapping['_parent']['property']);
107
108 10
        if (empty($mapping)) {
109
            // Empty mapping, we want it encoded as a {} instead of a []
110
            $mapping = new \ArrayObject();
111
        }
112
113 10
        return $mapping;
114
    }
115
116
    /**
117
     * Fixes any properties and applies basic defaults for any field that does not have
118
     * required options.
119
     *
120
     * @param $properties
121
     */
122 10
    private function fixProperties(&$properties)
123
    {
124 10
        foreach ($properties as $name => &$property) {
125 10
            unset($property['property_path']);
126
127 10
            if (!isset($property['type'])) {
128 5
                $property['type'] = 'text';
129
            }
130 10
            if (isset($property['fields'])) {
131 2
                $this->fixProperties($property['fields']);
132
            }
133 10
            if (isset($property['properties'])) {
134 10
                $this->fixProperties($property['properties']);
135
            }
136
        }
137 10
    }
138
}
139