Passed
Pull Request — master (#7)
by Dominik
03:06 queued 46s
created

EmbedManyFieldDenormalizer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 94
loc 94
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B denormalizeField() 38 38 6
A getEmbObjectOrClass() 10 10 2
A resolveProxy() 6 6 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 7
    public function __construct(string $class, AccessorInterface $accessor)
33
    {
34 7
        $this->class = $class;
35 7
        $this->accessor = $accessor;
36 7
    }
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 7
    public function denormalizeField(
49
        string $path,
50
        $object,
51
        $value,
52
        DenormalizerContextInterface $context,
53
        DenormalizerInterface $denormalizer = null
54
    ) {
55 7
        if (null === $value) {
56 1
            $this->accessor->setValue($object, $value);
57
58 1
            return;
59
        }
60
61 6
        if (null === $denormalizer) {
62 1
            throw DeserializerLogicException::createMissingDenormalizer($path);
63
        }
64
65 5
        if (!is_array($value)) {
66 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
67
        }
68
69 4
        $existEmbObjects = $this->accessor->getValue($object);
70
71 4
        $embObjects = new ArrayCollection();
72 4
        foreach ($value as $i => $subValue) {
73 4
            $subPath = $path.'['.$i.']';
74
75 4
            if (!is_array($subValue)) {
76 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'array');
77
            }
78
79 3
            $embObject = $this->getEmbObjectOrClass($existEmbObjects[$i] ?? null);
80
81 3
            $embObjects[$i] = $denormalizer->denormalize($embObject, $subValue, $context, $subPath);
82
        }
83
84 3
        $this->accessor->setValue($object, $embObjects);
85 3
    }
86
87
    /**
88
     * @param object|null $existEmbObject
89
     *
90
     * @return string
91
     */
92 3
    private function getEmbObjectOrClass($existEmbObject)
93
    {
94 3
        if (null === $existEmbObject) {
95 1
            return $this->class;
96
        }
97
98 2
        $this->resolveProxy($existEmbObject);
99
100 2
        return $existEmbObject;
101
    }
102
103 2
    private function resolveProxy($refObject)
104
    {
105 2
        if (null !== $refObject && $refObject instanceof Proxy && !$refObject->__isInitialized()) {
106 1
            $refObject->__load();
107
        }
108 2
    }
109
}
110