Completed
Pull Request — master (#1171)
by Alessandro
09:43
created

MappingBuilder   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.48%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 121
ccs 43
cts 46
cp 0.9348
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildIndexMapping() 0 20 4
F buildTypeMapping() 0 46 11
C fixProperties() 0 23 7
1
<?php
2
3
/**
4
 * This file is part of the FOSElasticaBundle project.
5
 *
6
 * (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
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\IndexConfig;
15
use FOS\ElasticaBundle\Configuration\TypeConfig;
16
17
class MappingBuilder
18
{
19
    /**
20
     * Skip adding default information to certain fields.
21
     *
22
     * @var array
23
     */
24
    private $skipTypes = array('completion');
25
26
    /**
27
     * Builds mappings for an entire index.
28
     *
29
     * @param IndexConfig $indexConfig
30
     *
31
     * @return array
32
     */
33 5
    public function buildIndexMapping(IndexConfig $indexConfig)
34
    {
35 5
        $typeMappings = array();
36 5
        foreach ($indexConfig->getTypes() as $typeConfig) {
37 5
            $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
38
        }
39
40 5
        $mapping = array();
41 5
        if (!empty($typeMappings)) {
42 5
            $mapping['mappings'] = $typeMappings;
43
        }
44
        // 'warmers' => $indexConfig->getWarmers(),
45
46 5
        $settings = $indexConfig->getSettings();
47 5
        if (!empty($settings)) {
48 2
            $mapping['settings'] = $settings;
49
        }
50
51 5
        return $mapping;
52
    }
53
54
    /**
55
     * Builds mappings for a single type.
56
     *
57
     * @param TypeConfig $typeConfig
58
     *
59
     * @return array
60
     */
61 6
    public function buildTypeMapping(TypeConfig $typeConfig)
62
    {
63 6
        $mapping = $typeConfig->getMapping();
64
65 6
        if (null !== $typeConfig->getDynamicDateFormats()) {
66 5
            $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats();
67
        }
68
69 6
        if (null !== $typeConfig->getDateDetection()) {
70 2
            $mapping['date_detection'] = $typeConfig->getDateDetection();
71
        }
72
73 6
        if (null !== $typeConfig->getNumericDetection()) {
74 2
            $mapping['numeric_detection'] = $typeConfig->getNumericDetection();
75
        }
76
77 6
        if ($typeConfig->getAnalyzer()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $typeConfig->getAnalyzer() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
78
            $mapping['analyzer'] = $typeConfig->getAnalyzer();
79
        }
80
81 6
        if ($typeConfig->getDynamic() !== null) {
82 2
            $mapping['dynamic'] = $typeConfig->getDynamic();
83
        }
84
85 6
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
86 5
            unset($mapping['dynamic_templates']);
87
        }
88
89 6
        $this->fixProperties($mapping['properties']);
90 6
        if (!$mapping['properties']) {
91 3
            unset($mapping['properties']);
92
        }
93
94 6
        if ($typeConfig->getModel()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $typeConfig->getModel() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
95 3
            $mapping['_meta']['model'] = $typeConfig->getModel();
96
        }
97
98 6
        unset($mapping['_parent']['identifier'], $mapping['_parent']['property']);
99
100 6
        if (empty($mapping)) {
101
            // Empty mapping, we want it encoded as a {} instead of a []
102
            $mapping = new \ArrayObject();
103
        }
104
105 6
        return $mapping;
106
    }
107
108
    /**
109
     * Fixes any properties and applies basic defaults for any field that does not have
110
     * required options.
111
     *
112
     * @param $properties
113
     */
114 6
    private function fixProperties(&$properties)
115
    {
116 6
        foreach ($properties as $name => &$property) {
117 6
            unset($property['property_path']);
118
119 6
            if (!isset($property['type'])) {
120 5
                $property['type'] = 'text';
121
            }
122
            /**
123
             * @todo multi_field has been removed
124
             * @todo fields has been removed
125
             */
126 6
            if ($property['type'] == 'multi_field' && isset($property['fields'])) {
127
                $this->fixProperties($property['fields']);
128
            }
129 6
            if (isset($property['properties'])) {
130 2
                $this->fixProperties($property['properties']);
131
            }
132 6
            if (in_array($property['type'], $this->skipTypes)) {
133 6
                continue;
134
            }
135
        }
136 6
    }
137
}
138