Completed
Pull Request — master (#1315)
by Mathieu
03:54
created

MappingBuilder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 84.09%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
lcom 0
cbo 2
dl 0
loc 107
ccs 37
cts 44
cp 0.8409
rs 10
c 1
b 0
f 0

3 Methods

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