Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

MappingOperations::extend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity\Mapping\Structure;
4
5
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\Mapping;
6
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\MappingInterface;
7
use AmaTeam\ElasticSearch\Entity\Mapping\Property\MappingOperations as PropertyMappingOperations;
8
9
class MappingOperations
10
{
11
    public static function merge(MappingInterface... $mappings): Mapping
12
    {
13
        $target = new Mapping();
14
        foreach ($mappings as $source) {
15
            if ($source->getIgnoredParentProperties() !== null) {
16
                $target->setIgnoredParentProperties($source->getIgnoredParentProperties());
17
            }
18
            $defaultView = ViewOperations::merge($target->getDefaultView(), $source->getDefaultView());
19
            $target->setDefaultView($defaultView);
20 View Code Duplication
            foreach ($source->getViews() as $name => $view) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
                $current = $target->getView($name);
22
                $pool = $current ? [$current, $view] : [$view];
23
                $target->setView($name, ViewOperations::merge(...$pool));
24
            }
25
            foreach ($source->getProperties() as $name => $property) {
26
                $current = $target->getProperty($name);
27
                $pool = $current ? [$current, $property] : [$property];
28
                $target->setProperty($name, PropertyMappingOperations::merge(...$pool));
29
            }
30
        }
31
        return $target;
32
    }
33
34
    public static function from(MappingInterface $mapping): Mapping
35
    {
36
        return static::merge($mapping);
37
    }
38
39
    public static function toMutable(MappingInterface $mapping): Mapping
40
    {
41
        if ($mapping instanceof Mapping) {
42
            return $mapping;
43
        }
44
        return static::from($mapping);
45
    }
46
47
    public static function extend(MappingInterface $parent, MappingInterface $child): Mapping
48
    {
49
        $target = static::from($parent);
50
        $ignoredProperties = $child->getIgnoredParentProperties() ?? [];
51
        foreach ($ignoredProperties as $property) {
52
            $target->removeProperty($property);
53
        }
54
        return static::merge($target, $child);
55
    }
56
}
57