Passed
Pull Request — master (#7)
by Dominik
02:47
created

ReferenceManyFieldDenormalizer::denormalizeField()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 36
Code Lines 21

Duplication

Lines 36
Ratio 100 %

Code Coverage

Tests 17
CRAP Score 5

Importance

Changes 0
Metric Value
dl 36
loc 36
ccs 17
cts 17
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
nc 5
nop 5
crap 5
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
        $refObjects = 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
            $refObject = $repository($subValue);
76
77 2
            $this->resolveProxy($refObject);
78
79 2
            $refObjects[$i] = $refObject;
80
        }
81
82 2
        $this->accessor->setValue($object, $refObjects);
83 2
    }
84
85 2
    private function resolveProxy($refObject)
86
    {
87 2
        if (null !== $refObject && $refObject instanceof Proxy && !$refObject->__isInitialized()) {
88 1
            $refObject->__load();
89
        }
90 2
    }
91
}
92