Completed
Push — master ( 9f4c87...8a0b06 )
by Karel
07:12
created

MappingBuilder::buildTypeMapping()   F

Complexity

Conditions 11
Paths 512

Size

Total Lines 46
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 12.2432

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
ccs 18
cts 23
cp 0.7826
rs 3.1764
cc 11
eloc 23
nc 512
nop 1
crap 12.2432

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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