Test Failed
Push — master ( a3ebe4...1f8fc2 )
by Dominik
02:17
created

ReferenceFieldNormalizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 38
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalizeField() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Normalizer;
6
7
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8
use Chubbyphp\Serialization\SerializerLogicException;
9
10
final class ReferenceFieldNormalizer implements FieldNormalizerInterface
11
{
12
    /**
13
     * @var AccessorInterface
14
     */
15
    private $accessor;
16
17
    /**
18
     * @param AccessorInterface $accessor
19
     */
20 3
    public function __construct(AccessorInterface $accessor)
21
    {
22 3
        $this->accessor = $accessor;
23 3
    }
24
25
    /**
26
     * @param string                     $path
27
     * @param object                     $object
28
     * @param NormalizerContextInterface $context
29
     * @param NormalizerInterface|null   $normalizer
30
     */
31 3
    public function normalizeField(
32
        string $path,
33
        $object,
34
        NormalizerContextInterface $context,
35
        NormalizerInterface $normalizer = null
36
    ) {
37 3
        if (null === $normalizer) {
38 1
            throw SerializerLogicException::createMissingNormalizer($path);
39
        }
40
41 2
        if (null === $value = $this->accessor->getValue($object)) {
42 1
            return null;
43
        }
44
45 1
        return $normalizer->normalize($value, $context, $path);
46
    }
47
}
48