Completed
Push — master ( 09f154...ad635e )
by Dominik
04:49
created

EmbedManyFieldDenormalizer::denormalizeField()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 42
Code Lines 25

Duplication

Lines 15
Ratio 35.71 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 0
Metric Value
dl 15
loc 42
ccs 20
cts 20
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 25
nc 7
nop 5
crap 7
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
    /**
27
     * @param string            $class
28
     * @param AccessorInterface $accessor
29
     */
30 6
    public function __construct(string $class, AccessorInterface $accessor)
31
    {
32 6
        $this->class = $class;
33 6
        $this->accessor = $accessor;
34 6
    }
35
36
    /**
37
     * @param string                       $path
38
     * @param object                       $object
39
     * @param mixed                        $value
40
     * @param DenormalizerContextInterface $context
41
     * @param DenormalizerInterface|null   $denormalizer
42
     *
43
     * @throws DeserializerLogicException
44
     * @throws DeserializerRuntimeException
45
     */
46 6
    public function denormalizeField(
47
        string $path,
48
        $object,
49
        $value,
50
        DenormalizerContextInterface $context,
51
        DenormalizerInterface $denormalizer = null
52
    ) {
53 6
        if (null === $value) {
54 1
            $this->accessor->setValue($object, $value);
55
56 1
            return;
57
        }
58
59 5
        if (null === $denormalizer) {
60 1
            throw DeserializerLogicException::createMissingDenormalizer($path);
61
        }
62
63 4
        if (!is_array($value)) {
64 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
65
        }
66
67 3
        $existingChildObjects = $this->accessor->getValue($object) ?? [];
68
69 3
        $newChildObjects = [];
70 3 View Code Duplication
        foreach ($value as $i => $subValue) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71 3
            $subPath = $path.'['.$i.']';
72
73 3
            if (!is_array($subValue)) {
74 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array');
75
            }
76
77 2
            if (isset($existingChildObjects[$i])) {
78 1
                $newChildObject = $existingChildObjects[$i];
79
            } else {
80 1
                $newChildObject = $this->class;
81
            }
82
83 2
            $newChildObjects[$i] = $denormalizer->denormalize($newChildObject, $subValue, $context, $subPath);
84
        }
85
86 2
        $this->accessor->setValue($object, $newChildObjects);
87 2
    }
88
}
89