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

Normalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 46
c 0
b 0
f 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A normalize() 0 9 1
B normalizeOptions() 0 14 5
A normalizeAnalysis() 0 4 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Indexing;
4
5
use AmaTeam\ElasticSearch\API\Indexing;
6
use AmaTeam\ElasticSearch\API\Indexing\AnalysisInterface;
7
use AmaTeam\ElasticSearch\API\Indexing\Normalization\Context;
8
use AmaTeam\ElasticSearch\API\Indexing\Normalization\ContextInterface;
9
use AmaTeam\ElasticSearch\API\Indexing\NormalizerInterface;
10
use AmaTeam\ElasticSearch\API\IndexingInterface;
11
use AmaTeam\ElasticSearch\Indexing\Option\Infrastructure\Registry;
12
13
class Normalizer implements NormalizerInterface
14
{
15
    /**
16
     * @var Registry
17
     */
18
    private $optionRegistry;
19
20
    /**
21
     * @param Registry $optionRegistry
22
     */
23
    public function __construct(Registry $optionRegistry = null)
24
    {
25
        $this->optionRegistry = $optionRegistry ?? Registry::getInstance();
26
    }
27
28
    public function normalize(IndexingInterface $indexing, ContextInterface $context = null): IndexingInterface
29
    {
30
        $context = $context ?? new Context();
31
        return (new Indexing())
32
            ->setType($indexing->getType())
33
            ->setWriteIndices(array_unique($indexing->getWriteIndices()))
34
            ->setReadIndices(array_unique($indexing->getReadIndices()))
35
            ->setAnalysis($this->normalizeAnalysis($indexing->getAnalysis(), $context))
36
            ->setOptions($this->normalizeOptions($indexing->getOptions(), $context));
37
    }
38
39
    private function normalizeAnalysis(AnalysisInterface $analysis, ContextInterface $context): AnalysisInterface
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

39
    private function normalizeAnalysis(AnalysisInterface $analysis, /** @scrutinizer ignore-unused */ ContextInterface $context): AnalysisInterface

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
        // TODO
42
        return $analysis;
43
    }
44
45
    private function normalizeOptions(array $options, ContextInterface $context): array
46
    {
47
        $result = [];
48
        foreach ($options as $name => $value) {
49
            $option = $this->optionRegistry->find($name);
50
            if ($option) {
51
                $result[$option->getId()] = $value;
52
                continue;
53
            }
54
            if ($context->shouldPreserveUnknownOptions() || in_array($name, $context->getPreservedOptions())) {
55
                $result[$name] = $value;
56
            }
57
        }
58
        return $result;
59
    }
60
}
61