Passed
Push — feature/initial-implementation ( 77fab8...e556a2 )
by Fike
01:48
created

Normalizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 37
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 7 2
A normalizeArray() 0 16 4
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping;
6
7
use AmaTeam\ElasticSearch\Utility\Strings;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
10
use Symfony\Component\Serializer\Serializer;
11
12
/**
13
 * This class is responsible for mapping conversion from internal
14
 * representation to ready-to-apply array
15
 */
16
class Normalizer
17
{
18
    /**
19
     * @var NormalizerInterface
20
     */
21
    private $serializer;
22
23
    public function __construct()
24
    {
25
        $this->serializer = new Serializer([new ObjectNormalizer()]);
26
    }
27
28
    public function normalize(Mapping $mapping): array
29
    {
30
        $normalized = $this->normalizeArray($this->serializer->normalize($mapping));
0 ignored issues
show
Bug introduced by
It seems like $this->serializer->normalize($mapping) can also be of type integer and string and boolean and double; however, parameter $mapping of AmaTeam\ElasticSearch\Ma...lizer::normalizeArray() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

30
        $normalized = $this->normalizeArray(/** @scrutinizer ignore-type */ $this->serializer->normalize($mapping));
Loading history...
31
        if ($normalized['type'] === TypeEnum::ROOT) {
32
            unset($normalized['type']);
33
        }
34
        return $normalized;
35
    }
36
37
    private function normalizeArray(array $mapping): array
38
    {
39
        $type = $mapping['type'];
40
        $target = ['type' => Strings::camelToSnake($type)];
41
        foreach (TypeEnum::getParameters($type) as $parameter) {
42
            if (isset($mapping[$parameter])) {
43
                $target[Strings::camelToSnake($parameter)] = $mapping[$parameter];
44
            }
45
        }
46
        $key = 'properties';
47
        if (!empty($target[$key])) {
48
            $target[$key] = array_map([$this, 'normalizeArray'], $target[$key]);
49
        } else {
50
            unset($target[$key]);
51
        }
52
        return $target;
53
    }
54
}
55