Code Duplication    Length = 81-83 lines in 2 locations

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

@@ 15-95 (lines=81) @@
12
use Chubbyphp\Deserialization\DeserializerRuntimeException;
13
use Doctrine\Common\Persistence\Proxy;
14
15
final class EmbedOneFieldDenormalizer implements FieldDenormalizerInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    private $class;
21
22
    /**
23
     * @var AccessorInterface
24
     */
25
    private $accessor;
26
27
    /**
28
     * @param string            $class
29
     * @param AccessorInterface $accessor
30
     */
31
    public function __construct($class, AccessorInterface $accessor)
32
    {
33
        $this->class = $class;
34
        $this->accessor = $accessor;
35
    }
36
37
    /**
38
     * @param string                       $path
39
     * @param object                       $object
40
     * @param mixed                        $value
41
     * @param DenormalizerContextInterface $context
42
     * @param DenormalizerInterface|null   $denormalizer
43
     *
44
     * @throws DeserializerLogicException
45
     * @throws DeserializerRuntimeException
46
     */
47
    public function denormalizeField(
48
        string $path,
49
        $object,
50
        $value,
51
        DenormalizerContextInterface $context,
52
        DenormalizerInterface $denormalizer = null
53
    ) {
54
        if (null === $value) {
55
            $this->accessor->setValue($object, $value);
56
57
            return;
58
        }
59
60
        if (null === $denormalizer) {
61
            throw DeserializerLogicException::createMissingDenormalizer($path);
62
        }
63
64
        if (!is_array($value)) {
65
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
66
        }
67
68
        $relatedObject = $this->getRelatedObjectOrClass($this->accessor->getValue($object));
69
70
        $this->accessor->setValue($object, $denormalizer->denormalize($relatedObject, $value, $context, $path));
71
    }
72
73
    /**
74
     * @param object|null $existEmbObject
75
     *
76
     * @return string
77
     */
78
    private function getRelatedObjectOrClass($existEmbObject)
79
    {
80
        if (null === $existEmbObject) {
81
            return $this->class;
82
        }
83
84
        $this->resolveProxy($existEmbObject);
85
86
        return $existEmbObject;
87
    }
88
89
    private function resolveProxy($relatedObject)
90
    {
91
        if (null !== $relatedObject && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()) {
92
            $relatedObject->__load();
93
        }
94
    }
95
}
96

src/Denormalizer/Relation/EmbedOneFieldDenormalizer.php 1 location

@@ 18-100 (lines=83) @@
15
/**
16
 * @deprecated use Basic or Doctrine EmbedOneFieldDenormalizer
17
 */
18
final class EmbedOneFieldDenormalizer 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($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
        $relatedObject = $this->getRelatedObjectOrClass($this->accessor->getValue($object));
72
73
        $this->accessor->setValue($object, $denormalizer->denormalize($relatedObject, $value, $context, $path));
74
    }
75
76
    /**
77
     * @param object|null $existEmbObject
78
     *
79
     * @return string
80
     */
81
    private function getRelatedObjectOrClass($existEmbObject)
82
    {
83
        if (null === $existEmbObject) {
84
            return $this->class;
85
        }
86
87
        $this->resolveProxy($existEmbObject);
88
89
        return $existEmbObject;
90
    }
91
92
    private function resolveProxy($relatedObject)
93
    {
94
        if (null !== $relatedObject && interface_exists('Doctrine\Common\Persistence\Proxy')
95
            && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()
96
        ) {
97
            $relatedObject->__load();
98
        }
99
    }
100
}
101