Passed
Pull Request — master (#7)
by Dominik
03:31
created

EmbedManyFieldDenormalizer::denormalizeField()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 38
Code Lines 22

Duplication

Lines 38
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 38
loc 38
ccs 0
cts 29
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 22
nc 6
nop 5
crap 42
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer\Relation\Doctrine;
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
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Persistence\Proxy;
15
16 View Code Duplication
final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
17
{
18
    /**
19
     * @var string
20
     */
21
    private $class;
22
23
    /**
24
     * @var AccessorInterface
25
     */
26
    private $accessor;
27
28
    /**
29
     * @param string            $class
30
     * @param AccessorInterface $accessor
31
     */
32
    public function __construct(string $class, AccessorInterface $accessor)
33
    {
34
        $this->class = $class;
35
        $this->accessor = $accessor;
36
    }
37
38
    /**
39
     * @param string                       $path
40
     * @param object                       $object
41
     * @param mixed                        $value
42
     * @param DenormalizerContextInterface $context
43
     * @param DenormalizerInterface|null   $denormalizer
44
     *
45
     * @throws DeserializerLogicException
46
     * @throws DeserializerRuntimeException
47
     */
48
    public function denormalizeField(
49
        string $path,
50
        $object,
51
        $value,
52
        DenormalizerContextInterface $context,
53
        DenormalizerInterface $denormalizer = null
54
    ) {
55
        if (null === $value) {
56
            $this->accessor->setValue($object, $value);
57
58
            return;
59
        }
60
61
        if (null === $denormalizer) {
62
            throw DeserializerLogicException::createMissingDenormalizer($path);
63
        }
64
65
        if (!is_array($value)) {
66
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
67
        }
68
69
        $existEmbObjects = $this->accessor->getValue($object);
70
71
        $embObjects = new ArrayCollection();
72
        foreach ($value as $i => $subValue) {
73
            $subPath = $path.'['.$i.']';
74
75
            if (!is_array($subValue)) {
76
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array');
77
            }
78
79
            $embObject = $this->getEmbObjectOrClass($existEmbObjects[$i] ?? null);
80
81
            $embObjects[$i] = $denormalizer->denormalize($embObject, $subValue, $context, $subPath);
82
        }
83
84
        $this->accessor->setValue($object, $embObjects);
85
    }
86
87
    /**
88
     * @param object|null $existEmbObject
89
     *
90
     * @return string
91
     */
92
    private function getEmbObjectOrClass($existEmbObject)
93
    {
94
        if (null === $existEmbObject) {
95
            return $this->class;
96
        }
97
98
        $this->resolveProxy($existEmbObject);
99
100
        return $existEmbObject;
101
    }
102
103
    private function resolveProxy($refObject)
104
    {
105
        if (null !== $refObject && $refObject instanceof Proxy && !$refObject->__isInitialized()) {
106
            $refObject->__load();
107
        }
108
    }
109
}
110