Passed
Pull Request — master (#7)
by Dominik
02:32 queued 10s
created

EmbedOneFieldDenormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 81
loc 81
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

4 Methods

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

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\Persistence\Proxy;
14
15 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...
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 6
    public function __construct($class, AccessorInterface $accessor)
32
    {
33 6
        $this->class = $class;
34 6
        $this->accessor = $accessor;
35 6
    }
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 6
    public function denormalizeField(
48
        string $path,
49
        $object,
50
        $value,
51
        DenormalizerContextInterface $context,
52
        DenormalizerInterface $denormalizer = null
53
    ) {
54 6
        if (null === $value) {
55 1
            $this->accessor->setValue($object, $value);
56
57 1
            return;
58
        }
59
60 5
        if (null === $denormalizer) {
61 1
            throw DeserializerLogicException::createMissingDenormalizer($path);
62
        }
63
64 4
        if (!is_array($value)) {
65 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
66
        }
67
68 3
        $relatedObject = $this->getRelatedObjectOrClass($this->accessor->getValue($object));
69
70 3
        $this->accessor->setValue($object, $denormalizer->denormalize($relatedObject, $value, $context, $path));
71 3
    }
72
73
    /**
74
     * @param object|null $existEmbObject
75
     *
76
     * @return string
77
     */
78 3
    private function getRelatedObjectOrClass($existEmbObject)
79
    {
80 3
        if (null === $existEmbObject) {
81 1
            return $this->class;
82
        }
83
84 2
        $this->resolveProxy($existEmbObject);
85
86 2
        return $existEmbObject;
87
    }
88
89 2
    private function resolveProxy($relatedObject)
90
    {
91 2
        if (null !== $relatedObject && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()) {
92 1
            $relatedObject->__load();
93
        }
94 2
    }
95
}
96