Code Duplication    Length = 94-96 lines in 2 locations

src/Denormalizer/Relation/Doctrine/EmbedManyFieldDenormalizer.php 1 location

@@ 16-109 (lines=94) @@
13
use Doctrine\Common\Collections\ArrayCollection;
14
use Doctrine\Common\Persistence\Proxy;
15
16
final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface
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

src/Denormalizer/Relation/EmbedManyFieldDenormalizer.php 1 location

@@ 18-113 (lines=96) @@
15
/**
16
 * @deprecated use Basic or Doctrine EmbedManyFieldDenormalizer
17
 */
18
final class EmbedManyFieldDenormalizer implements FieldDenormalizerInterface
19
{
20
    /**
21
     * @var string
22
     */
23
    private $class;
24
25
    /**
26
     * @var AccessorInterface
27
     */
28
    private $accessor;
29
30
    /**
31
     * @param string            $class
32
     * @param AccessorInterface $accessor
33
     */
34
    public function __construct(string $class, AccessorInterface $accessor)
35
    {
36
        $this->class = $class;
37
        $this->accessor = $accessor;
38
    }
39
40
    /**
41
     * @param string                       $path
42
     * @param object                       $object
43
     * @param mixed                        $value
44
     * @param DenormalizerContextInterface $context
45
     * @param DenormalizerInterface|null   $denormalizer
46
     *
47
     * @throws DeserializerLogicException
48
     * @throws DeserializerRuntimeException
49
     */
50
    public function denormalizeField(
51
        string $path,
52
        $object,
53
        $value,
54
        DenormalizerContextInterface $context,
55
        DenormalizerInterface $denormalizer = null
56
    ) {
57
        if (null === $value) {
58
            $this->accessor->setValue($object, $value);
59
60
            return;
61
        }
62
63
        if (null === $denormalizer) {
64
            throw DeserializerLogicException::createMissingDenormalizer($path);
65
        }
66
67
        if (!is_array($value)) {
68
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
69
        }
70
71
        $existEmbObjects = $this->accessor->getValue($object);
72
73
        $embObjects = [];
74
        foreach ($value as $i => $subValue) {
75
            $subPath = $path.'['.$i.']';
76
77
            if (!is_array($subValue)) {
78
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array');
79
            }
80
81
            $embObject = $this->getEmbObjectOrClass($existEmbObjects[$i] ?? null);
82
83
            $embObjects[$i] = $denormalizer->denormalize($embObject, $subValue, $context, $subPath);
84
        }
85
86
        $this->accessor->setValue($object, $embObjects);
87
    }
88
89
    /**
90
     * @param object|null $existEmbObject
91
     *
92
     * @return string
93
     */
94
    private function getEmbObjectOrClass($existEmbObject)
95
    {
96
        if (null === $existEmbObject) {
97
            return $this->class;
98
        }
99
100
        $this->resolveProxy($existEmbObject);
101
102
        return $existEmbObject;
103
    }
104
105
    private function resolveProxy($refObject)
106
    {
107
        if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy')
108
            && $refObject instanceof Proxy && !$refObject->__isInitialized()
109
        ) {
110
            $refObject->__load();
111
        }
112
    }
113
}
114