Passed
Pull Request — master (#7)
by Dominik
04:34 queued 24s
created

EmbedOneFieldDenormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 58
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B denormalizeField() 0 25 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer\Relation\Basic;
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 EmbedOneFieldDenormalizer implements FieldDenormalizerInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $class;
20
21
    /**
22
     * @var AccessorInterface
23
     */
24
    private $accessor;
25
26
    /**
27
     * @param string            $class
28
     * @param AccessorInterface $accessor
29
     */
30
    public function __construct($class, AccessorInterface $accessor)
31
    {
32
        $this->class = $class;
33
        $this->accessor = $accessor;
34
    }
35
36
    /**
37
     * @param string                       $path
38
     * @param object                       $object
39
     * @param mixed                        $value
40
     * @param DenormalizerContextInterface $context
41
     * @param DenormalizerInterface|null   $denormalizer
42
     *
43
     * @throws DeserializerLogicException
44
     * @throws DeserializerRuntimeException
45
     */
46
    public function denormalizeField(
47
        string $path,
48
        $object,
49
        $value,
50
        DenormalizerContextInterface $context,
51
        DenormalizerInterface $denormalizer = null
52
    ) {
53
        if (null === $value) {
54
            $this->accessor->setValue($object, $value);
55
56
            return;
57
        }
58
59
        if (null === $denormalizer) {
60
            throw DeserializerLogicException::createMissingDenormalizer($path);
61
        }
62
63
        if (!is_array($value)) {
64
            throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'array');
65
        }
66
67
        $embObject = $this->accessor->getValue($object) ?? $this->class;
68
69
        $this->accessor->setValue($object, $denormalizer->denormalize($embObject, $value, $context, $path));
70
    }
71
}
72