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

ReferenceManyFieldDenormalizer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 76
loc 76
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
B denormalizeField() 36 36 5
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 ReferenceManyFieldDenormalizer 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 callable
20
     */
21
    private $repository;
22
23
    /**
24
     * @var AccessorInterface
25
     */
26
    private $accessor;
27
28
    /**
29
     * @param callable          $repository
30
     * @param AccessorInterface $accessor
31
     */
32 5
    public function __construct(callable $repository, AccessorInterface $accessor)
33
    {
34 5
        $this->repository = $repository;
35 5
        $this->accessor = $accessor;
36 5
    }
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 5
    public function denormalizeField(
49
        string $path,
50
        $object,
51
        $value,
52
        DenormalizerContextInterface $context,
53
        DenormalizerInterface $denormalizer = null
54
    ) {
55 5
        if (null === $value) {
56 1
            $this->accessor->setValue($object, $value);
57
58 1
            return;
59
        }
60
61 4
        if (!is_array($value)) {
62 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
63
        }
64
65 3
        $repository = $this->repository;
66
67 3
        $relatedObjects = new ArrayCollection();
68 3
        foreach ($value as $i => $subValue) {
69 3
            $subPath = $path.'['.$i.']';
70
71 3
            if (!is_string($subValue)) {
72 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string');
73
            }
74
75 2
            $relatedObject = $repository($subValue);
76
77 2
            $this->resolveProxy($relatedObject);
78
79 2
            $relatedObjects[$i] = $relatedObject;
80
        }
81
82 2
        $this->accessor->setValue($object, $relatedObjects);
83 2
    }
84
85 2
    private function resolveProxy($relatedObject)
86
    {
87 2
        if (null !== $relatedObject && $relatedObject instanceof Proxy && !$relatedObject->__isInitialized()) {
88 1
            $relatedObject->__load();
89
        }
90 2
    }
91
}
92