EmbedManyFieldDenormalizer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 26
c 2
b 0
f 1
dl 0
loc 89
ccs 23
cts 23
cp 1
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A cleanRelatedObjects() 0 9 2
A denormalizeField() 0 25 4
A __construct() 0 4 1
A assignRelatedObjects() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer\Relation;
6
7
use Chubbyphp\Deserialization\Accessor\AccessorInterface;
8
use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface;
9
use Chubbyphp\Deserialization\Denormalizer\DenormalizerInterface;
10
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
11
use Chubbyphp\Deserialization\DeserializerLogicException;
12
use Chubbyphp\Deserialization\DeserializerRuntimeException;
13
14
final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $class;
20
21
    /**
22
     * @var AccessorInterface
23
     */
24
    private $accessor;
25
26
    public function __construct(string $class, AccessorInterface $accessor)
27
    {
28
        $this->class = $class;
29
        $this->accessor = $accessor;
30 8
    }
31
32 8
    /**
33 8
     * @param object $object
34 8
     * @param mixed  $value
35
     *
36
     * @throws DeserializerLogicException
37
     * @throws DeserializerRuntimeException
38
     */
39
    public function denormalizeField(
40
        string $path,
41
        $object,
42
        $value,
43
        DenormalizerContextInterface $context,
44
        DenormalizerInterface $denormalizer = null
45
    ): void {
46 8
        if (null === $value) {
47
            $value = [];
48
        }
49
50
        if (null === $denormalizer) {
51
            throw DeserializerLogicException::createMissingDenormalizer($path);
52
        }
53 8
54 1
        if (!is_array($value)) {
55
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
56
        }
57 8
58 1
        $relatedObjects = $this->accessor->getValue($object) ?? [];
59
60
        $existEmbObjects = $this->cleanRelatedObjects($relatedObjects);
61 7
        $this->assignRelatedObjects($path, $value, $relatedObjects, $existEmbObjects, $context, $denormalizer);
62 1
63
        $this->accessor->setValue($object, $relatedObjects);
64
    }
65 6
66
    /**
67 6
     * @param iterable $relatedObjects
68 6
     *
69
     * @return array
70 5
     */
71 5
    private function cleanRelatedObjects(&$relatedObjects)
72
    {
73
        $existEmbObjects = [];
74
        foreach ($relatedObjects as $key => $existEmbObject) {
75
            $existEmbObjects[$key] = $existEmbObject;
76
            unset($relatedObjects[$key]);
77
        }
78 6
79
        return $existEmbObjects;
80 6
    }
81 6
82 2
    /**
83 2
     * @param iterable $relatedObjects
84
     */
85
    private function assignRelatedObjects(
86 6
        string $path,
87
        array $value,
88
        &$relatedObjects,
89
        array $existEmbObjects,
90
        DenormalizerContextInterface $context,
91
        DenormalizerInterface $denormalizer
92
    ): void {
93
        foreach ($value as $key => $subValue) {
94
            $subPath = $path.'['.$key.']';
95
96
            if (!is_array($subValue)) {
97 6
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array');
98
            }
99
100
            $relatedObject = $existEmbObjects[$key] ?? $this->class;
101
102
            $relatedObjects[$key] = $denormalizer->denormalize($relatedObject, $subValue, $context, $subPath);
103
        }
104
    }
105
}
106