Passed
Pull Request — master (#10)
by Dominik
02:09
created

assignRelatedObjects()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 3
crap 3
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
14
final class ReferenceManyFieldDenormalizer implements FieldDenormalizerInterface
15
{
16
    /**
17
     * @var callable
18
     */
19
    private $repository;
20
21
    /**
22
     * @var AccessorInterface
23
     */
24
    private $accessor;
25
26
    /**
27
     * @param callable          $repository
28
     * @param AccessorInterface $accessor
29
     * @param callable|null     $collectionFactory
0 ignored issues
show
Bug introduced by
There is no parameter named $collectionFactory. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
30
     */
31 7
    public function __construct(callable $repository, AccessorInterface $accessor)
32
    {
33 7
        $this->repository = $repository;
34 7
        $this->accessor = $accessor;
35 7
    }
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 7
    public function denormalizeField(
48
        string $path,
49
        $object,
50
        $value,
51
        DenormalizerContextInterface $context,
52
        DenormalizerInterface $denormalizer = null
53
    ) {
54 7
        if (null === $value) {
55 1
            $this->accessor->setValue($object, $value);
56
57 1
            return;
58
        }
59
60 6
        if (!is_array($value)) {
61 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
62
        }
63
64 5
        $relatedObjects = $this->accessor->getValue($object) ?? [];
65
66 5
        $this->cleanRelatedObjects($relatedObjects);
67 5
        $this->assignRelatedObjects($path, $value, $relatedObjects);
68
69 4
        $this->accessor->setValue($object, $relatedObjects);
70 4
    }
71
72
    /**
73
     * @param array|\Traversable|\ArrayAccess $relatedObjects
74
     */
75 5
    private function cleanRelatedObjects(&$relatedObjects)
76
    {
77 5
        foreach ($relatedObjects as $i => $existEmbObject) {
78 2
            unset($relatedObjects[$i]);
79
        }
80 5
    }
81
82
    /**
83
     * @param string                          $path
84
     * @param array                           $value
85
     * @param array|\Traversable|\ArrayAccess $relatedObjects
86
     */
87 5
    private function assignRelatedObjects(string $path, array $value, &$relatedObjects)
88
    {
89 5
        $repository = $this->repository;
90
91 5
        foreach ($value as $i => $subValue) {
92 5
            $subPath = $path.'['.$i.']';
93
94 5
            if (!is_string($subValue)) {
95 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string');
96
            }
97
98 4
            $relatedObjects[$i] = $repository($subValue);
99
        }
100 4
    }
101
}
102