Completed
Pull Request — master (#1569)
by
unknown
03:33
created

MappingBuilder::buildMapping()   F

Complexity

Conditions 11
Paths 512

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.307

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 19
cts 22
cp 0.8636
rs 3.8277
c 0
b 0
f 0
cc 11
nc 512
nop 2
crap 11.307

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