Passed
Push — feature/initial-implementation ( 79751f...3b7c72 )
by Fike
01:53
created

Converter::renderPropertyMapping()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 17
nc 16
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping\Conversion;
6
7
use AmaTeam\ElasticSearch\API\Entity\Mapping\ClassMappingInterface;
8
use AmaTeam\ElasticSearch\API\Entity\ProviderInterface;
9
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface;
10
use AmaTeam\ElasticSearch\API\Entity\Mapping\PropertyMappingInterface;
11
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ConverterInterface;
12
use AmaTeam\ElasticSearch\API\Mapping\Conversion\ContextInterface;
13
use AmaTeam\ElasticSearch\API\Mapping\Conversion\DefaultContext;
14
use AmaTeam\ElasticSearch\Entity\Mapping\ClassMappingView;
15
use AmaTeam\ElasticSearch\Entity\Provider;
16
use AmaTeam\ElasticSearch\Mapping\Mapping;
17
use AmaTeam\ElasticSearch\Entity\Mapping\PropertyMappingView;
18
19
class Converter implements ConverterInterface
20
{
21
    /**
22
     * @var ProviderInterface
23
     */
24
    private $provider;
25
26
    /**
27
     * @param ProviderInterface $provider
28
     */
29
    public function __construct(ProviderInterface $provider = null)
30
    {
31
        $this->provider = $provider ?? new Provider();
32
    }
33
34
    public function convert(ClassMappingInterface $source, ContextInterface $context = null): MappingInterface
35
    {
36
        $context = $context ?? new DefaultContext();
37
        return $this->renderDocumentMapping($source, $context->getViews());
38
    }
39
40
    private function renderDocumentMapping(ClassMappingInterface $document, array $viewNames): MappingInterface
41
    {
42
        $views = array_map([$document, 'getView'], $viewNames);
43
        array_unshift($views, $document->getDefaultView());
44
        $view = ClassMappingView::merge(...array_filter($views));
45
        $mapping = new Mapping();
46
        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...
47
            $mapping->setType($view->getType());
48
        }
49
        $properties = [];
50
        foreach ($document->getProperties() as $property => $definition) {
51
            $properties[$property] = $this->renderPropertyMapping($definition, $viewNames);
52
        }
53
        $mapping->setProperties($properties);
54
        $mapping->setParameters($view->getParameters());
55
        return $mapping;
56
    }
57
58
    private function renderPropertyMapping(PropertyMappingInterface $property, array $viewNames): MappingInterface
59
    {
60
        $views = array_map([$property, 'getView'], $viewNames);
61
        array_unshift($views, $property->getDefaultView());
62
        $view = PropertyMappingView::merge(...array_filter($views));
63
        $mapping = new Mapping();
64
        if ($view->getTargetClass()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getTargetClass() 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...
65
            $localViewNames = $viewNames;
66
            if (!empty($property->getForcedViewNames())) {
67
                $forcedViews = $property->getForcedViewNames();
68
                $append = $property->getAppendForcedViews();
69
                $localViewNames = $append ? array_merge($localViewNames, $forcedViews) : $forcedViews;
70
            }
71
            $source = $this->provider->get($view->getTargetClass());
72
            $mapping = $this->renderDocumentMapping($source->getMapping(), $localViewNames);
73
        }
74
        foreach ($view->getParameters() as $parameter => $value) {
75
            $mapping->setParameter($parameter, $value);
76
        }
77
        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...
78
            $mapping->setType($view->getType());
79
        }
80
        return $mapping;
81
    }
82
}
83