Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

Converter::computePropertyViews()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity\Mapping;
4
5
use AmaTeam\ElasticSearch\API\Entity\Descriptor\ManagerInterface;
6
use AmaTeam\ElasticSearch\API\Entity\Mapping\Conversion\ContextInterface;
7
use AmaTeam\ElasticSearch\API\Entity\Mapping\Conversion\DefaultContext;
8
use AmaTeam\ElasticSearch\API\Entity\Mapping\ConverterInterface;
9
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\MappingInterface as PropertyMappingInterface;
10
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\ViewInterface as PropertyViewInterface;
11
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\MappingInterface as StructureMappingInterface;
12
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\ViewInterface as StructureViewInterface;
13
use AmaTeam\ElasticSearch\API\MappingInterface;
14
use AmaTeam\ElasticSearch\API\Mapping;
15
use AmaTeam\ElasticSearch\Entity\Mapping\Structure\ViewOperations as StructureViews;
16
use AmaTeam\ElasticSearch\Entity\Mapping\Property\ViewOperations as PropertyViews;
17
use AmaTeam\ElasticSearch\Mapping\Type\RootType;
18
19
class Converter implements ConverterInterface
20
{
21
    /**
22
     * @var ManagerInterface
23
     */
24
    private $manager;
25
26
    /**
27
     * @param ManagerInterface $manager
28
     */
29
    public function __construct(ManagerInterface $manager)
30
    {
31
        $this->manager = $manager;
32
    }
33
34
    public function convert(StructureMappingInterface $mapping, ContextInterface $context = null): MappingInterface
35
    {
36
        return $this->convertStructureMapping($mapping, $context ?? new DefaultContext());
37
    }
38
    
39
    private function convertStructureMapping(StructureMappingInterface $mapping, ContextInterface $context): Mapping
40
    {
41
        $target = new Mapping();
42
        $views = static::computeStructureViews($mapping, $context);
43
        $view = StructureViews::merge(...$views);
44
        $type = $context->isRootLevelMapping() ? RootType::ID : $view->getType();
45
        if ($type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type 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...
46
            $target->setType($type);
47
        }
48
        foreach ($view->getParameters() as $parameter => $value) {
49
            $target->setParameter($parameter, $value);
50
        }
51
        foreach ($mapping->getProperties() as $name => $property) {
52
            if (in_array($name, $view->getIgnoredProperties(), true)) {
53
                continue;
54
            }
55
            $innerContext = DefaultContext::from($context)->setRootLevelMapping(false);
56
            $target->setProperty($name, $this->convertPropertyMapping($property, $innerContext));
57
        }
58
        return $target;
59
    }
60
    
61
    private function convertPropertyMapping(PropertyMappingInterface $mapping, ContextInterface $context): Mapping
62
    {
63
        $views = static::computePropertyViews($mapping, $context);
64
        $view = PropertyViews::merge(...$views);
65
        $target = new Mapping();
66
        if ($view->getTargetEntity()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getTargetEntity() 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...
67
            $descriptor = $this->manager->get($view->getTargetEntity());
68
            $viewNames = static::computeChildViews($view, $context);
69
            $innerContext = DefaultContext::from($context)
70
                ->setViews($viewNames)
71
                ->setRootLevelMapping(false);
72
            $target = $this->convertStructureMapping($descriptor->getMapping(), $innerContext);
73
        }
74
        if ($view->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getType() 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...
75
            $target->setType($view->getType());
76
        }
77
        foreach ($view->getParameters() as $parameter => $value) {
78
            $target->setParameter($parameter, $value);
79
        }
80
        return $target;
81
    }
82
83
    /**
84
     * @param StructureMappingInterface $mapping
85
     * @param ContextInterface $context
86
     * @return StructureViewInterface[]
87
     */
88 View Code Duplication
    private static function computeStructureViews(StructureMappingInterface $mapping, ContextInterface $context): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
89
    {
90
        $result = [$mapping->getDefaultView()];
91
        $sources = $mapping->getViews();
92
        foreach ($context->getViews() as $name) {
93
            if (isset($sources[$name])) {
94
                $result[] = $sources[$name];
95
            }
96
        }
97
        return $result;
98
    }
99
100
    /**
101
     * @param PropertyMappingInterface $mapping
102
     * @param ContextInterface $context
103
     * @return PropertyViewInterface[]
104
     */
105 View Code Duplication
    private static function computePropertyViews(PropertyMappingInterface $mapping, ContextInterface $context): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
106
    {
107
        $result = [$mapping->getDefaultView()];
108
        $sources = $mapping->getViews();
109
        foreach ($context->getViews() as $name) {
110
            if (isset($sources[$name])) {
111
                $result[] = $sources[$name];
112
            }
113
        }
114
        return $result;
115
    }
116
117
    private static function computeChildViews(PropertyViewInterface $view, ContextInterface $context): array
118
    {
119
        if (!$view->shouldAppendChildViews()) {
120
            return $view->getChildViews() ?? [];
121
        }
122
        return array_unique(array_merge($context->getViews(), $view->getChildViews()));
123
    }
124
}
125