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

ReferenceManyFieldDenormalizer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 88
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B denormalizeField() 0 24 3
A cleanRelatedObjects() 0 6 2
A assignRelatedObjects() 0 14 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