Completed
Push — master ( 05d46d...8ebb9e )
by Karel
21:31 queued 14:42
created

MappingBuilder   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 95.45%

Importance

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

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 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
     * Builds mappings for an entire index.
21
     *
22
     * @param IndexConfig $indexConfig
23
     *
24
     * @return array
25
     */
26 5
    public function buildIndexMapping(IndexConfig $indexConfig)
27
    {
28 5
        $typeMappings = array();
29 5
        foreach ($indexConfig->getTypes() as $typeConfig) {
30 5
            $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
31
        }
32
33 5
        $mapping = array();
34 5
        if (!empty($typeMappings)) {
35 5
            $mapping['mappings'] = $typeMappings;
36
        }
37
        // 'warmers' => $indexConfig->getWarmers(),
38
39 5
        $settings = $indexConfig->getSettings();
40 5
        if (!empty($settings)) {
41 2
            $mapping['settings'] = $settings;
42
        }
43
44 5
        return $mapping;
45
    }
46
47
    /**
48
     * Builds mappings for a single type.
49
     *
50
     * @param TypeConfig $typeConfig
51
     *
52
     * @return array
53
     */
54 6
    public function buildTypeMapping(TypeConfig $typeConfig)
55
    {
56 6
        $mapping = $typeConfig->getMapping();
57
58 6
        if (null !== $typeConfig->getDynamicDateFormats()) {
59 5
            $mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats();
60
        }
61
62 6
        if (null !== $typeConfig->getDateDetection()) {
63 2
            $mapping['date_detection'] = $typeConfig->getDateDetection();
64
        }
65
66 6
        if (null !== $typeConfig->getNumericDetection()) {
67 2
            $mapping['numeric_detection'] = $typeConfig->getNumericDetection();
68
        }
69
70 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...
71
            $mapping['analyzer'] = $typeConfig->getAnalyzer();
72
        }
73
74 6
        if ($typeConfig->getDynamic() !== null) {
75 2
            $mapping['dynamic'] = $typeConfig->getDynamic();
76
        }
77
78 6
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
79 5
            unset($mapping['dynamic_templates']);
80
        }
81
82 6
        $this->fixProperties($mapping['properties']);
83 6
        if (!$mapping['properties']) {
84 3
            unset($mapping['properties']);
85
        }
86
87 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...
88 3
            $mapping['_meta']['model'] = $typeConfig->getModel();
89
        }
90
91 6
        unset($mapping['_parent']['identifier'], $mapping['_parent']['property']);
92
93 6
        if (empty($mapping)) {
94
            // Empty mapping, we want it encoded as a {} instead of a []
95
            $mapping = new \ArrayObject();
96
        }
97
98 6
        return $mapping;
99
    }
100
101
    /**
102
     * Fixes any properties and applies basic defaults for any field that does not have
103
     * required options.
104
     *
105
     * @param $properties
106
     */
107 6
    private function fixProperties(&$properties)
108
    {
109 6
        foreach ($properties as $name => &$property) {
110 6
            unset($property['property_path']);
111
112 6
            if (!isset($property['type'])) {
113 5
                $property['type'] = 'text';
114
            }
115 6
            if (isset($property['fields'])) {
116 2
                $this->fixProperties($property['fields']);
117
            }
118 6
            if (isset($property['properties'])) {
119 6
                $this->fixProperties($property['properties']);
120
            }
121
        }
122 6
    }
123
}
124