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

Normalizer::normalizeMapping()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity;
4
5
use AmaTeam\ElasticSearch\API\Entity\Normalization\Context;
6
use AmaTeam\ElasticSearch\API\Entity\Normalization\ContextInterface;
7
use AmaTeam\ElasticSearch\API\Indexing\Normalization\Context as IndexingContext;
8
use AmaTeam\ElasticSearch\API\Indexing\NormalizerInterface as IndexingNormalizerInterface;
9
use AmaTeam\ElasticSearch\API\IndexingInterface;
10
use AmaTeam\ElasticSearch\API\Mapping\Normalization\Context as MappingContext;
11
use AmaTeam\ElasticSearch\API\Mapping\NormalizerInterface as MappingNormalizerInterface;
12
use AmaTeam\ElasticSearch\API\Entity\NormalizerInterface;
13
use AmaTeam\ElasticSearch\API\EntityInterface;
14
use AmaTeam\ElasticSearch\API\MappingInterface;
15
use AmaTeam\ElasticSearch\Indexing\Normalizer as IndexingNormalizer;
16
use AmaTeam\ElasticSearch\Mapping\Normalizer as MappingNormalizer;
17
18
class Normalizer implements NormalizerInterface
19
{
20
    /**
21
     * @var IndexingNormalizerInterface
22
     */
23
    private $indexingNormalizer;
24
    /**
25
     * @var MappingNormalizerInterface
26
     */
27
    private $mappingNormalizer;
28
29
    public function __construct()
30
    {
31
        $this->indexingNormalizer = new IndexingNormalizer();
32
        $this->mappingNormalizer = new MappingNormalizer();
33
    }
34
35
    public function normalize(EntityInterface $entity, ContextInterface $context = null): EntityInterface
36
    {
37
        $context = $context ?? new Context();
38
        $entity = Operations::from($entity)
39
            ->setIndexing($this->normalizeIndexing($entity->getIndexing(), $context))
40
            ->setMapping($this->normalizeMapping($entity->getMapping(), $context));
41
        return $entity;
42
    }
43
44
    private function normalizeIndexing(IndexingInterface $indexing, ContextInterface $context): IndexingInterface
45
    {
46
        $configuration = (new IndexingContext())
47
            ->setPreserveUnknownOptions($context->shouldPreserveUnknownEntries())
48
            ->setPreservedOptions($context->getPreservedIndexingOptions());
49
        return $this->indexingNormalizer->normalize($indexing, $configuration);
50
    }
51
52
    private function normalizeMapping(MappingInterface $mapping, ContextInterface $context): MappingInterface
53
    {
54
        $configuration = (new MappingContext())
55
            ->setRootMapping(true)
56
            ->setPreserveUnknownParameters($context->shouldPreserveUnknownEntries())
57
            ->setPreservedParameters($context->getPreservedMappingParameters());
58
        return $this->mappingNormalizer->normalize($mapping, $configuration);
59
    }
60
}
61