Passed
Push — master ( a50590...b54c62 )
by Dominik
03:00
created

ReferenceManyFieldDenormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 10.26 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 8
loc 78
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B denormalizeField() 0 36 5
B resolveProxy() 8 8 5

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;
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
final class ReferenceManyFieldDenormalizer implements FieldDenormalizerInterface
16
{
17
    /**
18
     * @var callable
19
     */
20
    private $repository;
21
22
    /**
23
     * @var AccessorInterface
24
     */
25
    private $accessor;
26
27
    /**
28
     * @param callable          $repository
29
     * @param AccessorInterface $accessor
30
     */
31 5
    public function __construct(callable $repository, AccessorInterface $accessor)
32
    {
33 5
        $this->repository = $repository;
34 5
        $this->accessor = $accessor;
35 5
    }
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 5
    public function denormalizeField(
48
        string $path,
49
        $object,
50
        $value,
51
        DenormalizerContextInterface $context,
52
        DenormalizerInterface $denormalizer = null
53
    ) {
54 5
        if (null === $value) {
55 1
            $this->accessor->setValue($object, $value);
56
57 1
            return;
58
        }
59
60 4
        if (!is_array($value)) {
61 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
62
        }
63
64 3
        $repository = $this->repository;
65
66 3
        $refObjects = [];
67 3
        foreach ($value as $i => $subValue) {
68 3
            $subPath = $path.'['.$i.']';
69
70 3
            if (!is_string($subValue)) {
71 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string');
72
            }
73
74 2
            $refObject = $repository($subValue);
75
76 2
            $this->resolveProxy($refObject);
77
78 2
            $refObjects[$i] = $refObject;
79
        }
80
81 2
        $this->accessor->setValue($object, $refObjects);
82 2
    }
83
84 2 View Code Duplication
    private function resolveProxy($refObject)
0 ignored issues
show
Duplication introduced by
This method 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...
85
    {
86 2
        if (null !== $refObject && interface_exists('Doctrine\Common\Persistence\Proxy')
87 2
            && $refObject instanceof Proxy && !$refObject->__isInitialized()
88
        ) {
89 1
            $refObject->__load();
90
        }
91 2
    }
92
}
93