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

ReferenceFieldNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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