Code Duplication    Length = 75-77 lines in 2 locations

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

@@ 15-89 (lines=75) @@
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
        $embObject = $this->getEmbObjectOrClass($this->accessor->getValue($object));
69
70
        $this->accessor->setValue($object, $denormalizer->denormalize($embObject, $value, $context, $path));
71
    }
72
73
    /**
74
     * @param object|null $existEmbObject
75
     *
76
     * @return string
77
     */
78
    private function getEmbObjectOrClass($existEmbObject)
79
    {
80
        if (null === $existEmbObject) {
81
            return $this->class;
82
        }
83
84
        if ($existEmbObject instanceof Proxy && !$existEmbObject->__isInitialized()) {
85
            $existEmbObject->__load();
86
        }
87
88
        return $existEmbObject;
89
    }
90
}
91

src/Denormalizer/Relation/EmbedOneFieldDenormalizer.php 1 location

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