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

EmbedOneFieldDenormalizer::denormalizeField()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 15

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 25
loc 25
ccs 11
cts 11
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 5
crap 4
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
use Doctrine\Common\Persistence\Proxy;
14
15
/**
16
 * @deprecated use Basic or Doctrine EmbedOneFieldDenormalizer
17
 */
18 View Code Duplication
final class EmbedOneFieldDenormalizer 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...
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 6
    public function __construct($class, AccessorInterface $accessor)
35
    {
36 6
        $this->class = $class;
37 6
        $this->accessor = $accessor;
38 6
    }
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 6
    public function denormalizeField(
51
        string $path,
52
        $object,
53
        $value,
54
        DenormalizerContextInterface $context,
55
        DenormalizerInterface $denormalizer = null
56
    ) {
57 6
        if (null === $value) {
58 1
            $this->accessor->setValue($object, $value);
59
60 1
            return;
61
        }
62
63 5
        if (null === $denormalizer) {
64 1
            throw DeserializerLogicException::createMissingDenormalizer($path);
65
        }
66
67 4
        if (!is_array($value)) {
68 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
69
        }
70
71 3
        $embObject = $this->getEmbObjectOrClass($this->accessor->getValue($object));
72
73 3
        $this->accessor->setValue($object, $denormalizer->denormalize($embObject, $value, $context, $path));
74 3
    }
75
76
    /**
77
     * @param object|null $existEmbObject
78
     *
79
     * @return string
80
     */
81 3
    private function getEmbObjectOrClass($existEmbObject)
82
    {
83 3
        if (null === $existEmbObject) {
84 1
            return $this->class;
85
        }
86
87 2
        $this->resolveProxy($existEmbObject);
88
89 2
        return $existEmbObject;
90
    }
91
92 2
    private function resolveProxy($refObject)
93
    {
94 2
        if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy')
95 2
            && $refObject instanceof Proxy && !$refObject->__isInitialized()
96
        ) {
97 1
            $refObject->__load();
98
        }
99 2
    }
100
}
101