Passed
Pull Request — master (#6)
by Dominik
05:36
created

ReferenceManyFieldDenormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
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
     * @var string|null
29
     */
30
    private $collectionClass;
31
32
    /**
33
     * @param callable          $repository
34
     * @param AccessorInterface $accessor
35
     * @param string|null       $collectionClass
36
     */
37 6
    public function __construct(callable $repository, AccessorInterface $accessor, string $collectionClass = null)
38
    {
39 6
        $this->repository = $repository;
40 6
        $this->accessor = $accessor;
41 6
        $this->collectionClass = $collectionClass;
42 6
    }
43
44
    /**
45
     * @param string                       $path
46
     * @param object                       $object
47
     * @param mixed                        $value
48
     * @param DenormalizerContextInterface $context
49
     * @param DenormalizerInterface|null   $denormalizer
50
     *
51
     * @throws DeserializerLogicException
52
     * @throws DeserializerRuntimeException
53
     */
54 6
    public function denormalizeField(
55
        string $path,
56
        $object,
57
        $value,
58
        DenormalizerContextInterface $context,
59
        DenormalizerInterface $denormalizer = null
60
    ) {
61 6
        if (null === $value) {
62 1
            $this->accessor->setValue($object, $value);
63
64 1
            return;
65
        }
66
67 5
        if (!is_array($value)) {
68 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
69
        }
70
71 4
        $repository = $this->repository;
72
73 4
        $referencedObjects = $this->newCollection();
74 4
        foreach ($value as $i => $subValue) {
75 4
            $subPath = $path.'['.$i.']';
76
77 4
            if (!is_string($subValue)) {
78 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string');
79
            }
80
81 3
            $referencedObject = $repository($subValue);
82
83 3 View Code Duplication
            if (null !== $referencedObject) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84 3
                if (interface_exists('Doctrine\Common\Persistence\Proxy')
85 3
                    && $referencedObject instanceof Proxy && !$referencedObject->__isInitialized()
86
                ) {
87 1
                    $referencedObject->__load();
88
                }
89
            }
90
91 3
            $referencedObjects[$i] = $referencedObject;
92
        }
93
94 3
        $this->accessor->setValue($object, $referencedObjects);
95 3
    }
96
97
    /**
98
     * @return array|\ArrayAccess|\Traversable
99
     */
100 4
    private function newCollection()
101
    {
102 4
        if (null === $this->collectionClass) {
103 3
            return [];
104
        }
105
106 1
        $collectionClass = $this->collectionClass;
107
108 1
        return new $collectionClass();
109
    }
110
}
111