Completed
Pull Request — master (#1134)
by Istvan
14:27
created

MappingBuilder   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 90.32%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 117
ccs 56
cts 62
cp 0.9032
rs 10
c 4
b 0
f 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildIndexMapping() 0 20 4
F buildTypeMapping() 0 46 11
B fixProperties() 0 19 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 5
        }
39
40 5
        $mapping = array();
41 5
        if (!empty($typeMappings)) {
42 5
            $mapping['mappings'] = $typeMappings;
43 5
        }
44
        // 'warmers' => $indexConfig->getWarmers(),
45
46 5
        $settings = $indexConfig->getSettings();
47 5
        if (!empty($settings)) {
48 2
            $mapping['settings'] = $settings;
49 2
        }
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 5
        }
68
69 6
        if (null !== $typeConfig->getDateDetection()) {
70 2
            $mapping['date_detection'] = $typeConfig->getDateDetection();
71 2
        }
72
73 6
        if (null !== $typeConfig->getNumericDetection()) {
74 2
            $mapping['numeric_detection'] = $typeConfig->getNumericDetection();
75 2
        }
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 2
        }
84
85 6
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
86 5
            unset($mapping['dynamic_templates']);
87 5
        }
88
89 6
        $this->fixProperties($mapping['properties']);
90 6
        if (!$mapping['properties']) {
91 3
            unset($mapping['properties']);
92 3
        }
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 3
        }
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'] = 'string';
121 5
            }
122 6
            if ($property['type'] == 'multi_field' && isset($property['fields'])) {
123
                $this->fixProperties($property['fields']);
124
            }
125 6
            if (isset($property['properties'])) {
126 2
                $this->fixProperties($property['properties']);
127 2
            }
128 6
            if (in_array($property['type'], $this->skipTypes)) {
129 2
                continue;
130
            }
131 6
        }
132 6
    }
133
}
134