Passed
Push — master ( 6d18bb...d7a1a2 )
by Dominik
50s
created

ReferenceManyFieldDenormalizer::createCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2
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
     * @var callable|null
28
     */
29
    private $collectionFactory;
30
31
    /**
32
     * @param callable          $repository
33
     * @param AccessorInterface $accessor
34
     * @param callable|null     $collectionFactory
35
     */
36 5
    public function __construct(callable $repository, AccessorInterface $accessor, callable $collectionFactory = null)
37
    {
38 5
        $this->repository = $repository;
39 5
        $this->accessor = $accessor;
40 5
        $this->collectionFactory = $collectionFactory;
41 5
    }
42
43
    /**
44
     * @param string                       $path
45
     * @param object                       $object
46
     * @param mixed                        $value
47
     * @param DenormalizerContextInterface $context
48
     * @param DenormalizerInterface|null   $denormalizer
49
     *
50
     * @throws DeserializerLogicException
51
     * @throws DeserializerRuntimeException
52
     */
53 5
    public function denormalizeField(
54
        string $path,
55
        $object,
56
        $value,
57
        DenormalizerContextInterface $context,
58
        DenormalizerInterface $denormalizer = null
59
    ) {
60 5
        if (null === $value) {
61 1
            $this->accessor->setValue($object, $value);
62
63 1
            return;
64
        }
65
66 4
        if (!is_array($value)) {
67 1
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
68
        }
69
70 3
        $repository = $this->repository;
71
72 3
        $relatedObjects = $this->createCollection();
73 3
        foreach ($value as $i => $subValue) {
74 3
            $subPath = $path.'['.$i.']';
75
76 3
            if (!is_string($subValue)) {
77 1
                throw DeserializerRuntimeException::createInvalidDataType($subPath, gettype($subValue), 'string');
78
            }
79
80 2
            $relatedObjects[$i] = $repository($subValue);
81
        }
82
83 2
        $this->accessor->setValue($object, $relatedObjects);
84 2
    }
85
86
    /**
87
     * @return array|\Traversable|\ArrayAccess
88
     */
89 3
    private function createCollection()
90
    {
91 3
        if (null === $this->collectionFactory) {
92 2
            return [];
93
        }
94
95 1
        $collectionFactory = $this->collectionFactory;
96
97 1
        return $collectionFactory();
98
    }
99
}
100